Forums

%paste magic fucntion in ipython?

Hi,

Thanks for the great application.

It seems %paste magic function is not working in ipython. Specifically, I get the following error:

In [18]: %paste

TryNext Traceback (most recent call last) /home/joon/<ipython-input-18-771cb64cec93> in <module>() ----> 1 get_ipython().magic(u"paste") /usr/local/lib/python2.7/site-packages/ipython-0.11-py2.7.egg/IPython/core/interactiveshell.pyc in magic(self, arg_s, next_input) 1892 self._magic_locals = sys._getframe(1).f_locals 1893 with self.builtin_trap: -> 1894 result = fn(magic_args) 1895 # Ensure we're not keeping object references around: 1896 self._magic_locals = {} /usr/local/lib/python2.7/site-packages/ipython-0.11-py2.7.egg/IPython/frontend/terminal/interactiveshell.pyc in magic_paste(self, parameter_s) 577 return 578 --> 579 text = self.shell.hooks.clipboard_get() 580 block = self._strip_pasted_lines_for_code(text.splitlines()) 581 /usr/local/lib/python2.7/site-packages/ipython-0.11-py2.7.egg/IPython/core/hooks.pyc in call(self, args, *kw) 139 kw = exc.kwargs 140 # if no function will accept it, raise TryNext up to the caller --> 141 raise TryNext 142 143 def str(self): TryNext:

Sorry for the indent break. Anyway, is this a known bug or am I doing something wrong?

-Joon

Hi Joon, thanks for the post! This isn't a problem we were aware of, but it makes sense.

It's happening because of a combination of reasons. The most important is the web browser security model; web apps like PythonAnywhere aren't given direct access to the clipboard (consider a case where you were copying and pasting a password while visiting a compromised website). Additionally, because the IPython interpreter is running on our server rather than your machine, it wouldn't have access to your clipboard even if we had access on the client side (a similar case would be if you were running IPython on a remote host via ssh).

It's possible that we might be able to work around this in the long run -- a signed Java applet (or perhaps something in Flash) could hack around the security model issue, and we could see if there was some way of keeping a server-side clipboard in sync with your client-side one. However, this might have problems of its own, especially security-wise. So unfortunately I can't promise it soon!

All the best,

Giles

FYI - Here's a few good writeups of how to access copy/paste from an application. They focus on a different problem, but the solutions might be of use (note: This isn't "easy" by any stretch of the imagination):

http://almaer.com/blog/supporting-the-system-clipboard-in-your-web-applications-what-a-pain http://stackoverflow.com/questions/127040/copy-put-text-on-the-clipboard-with-firefox-safari-and-chrome

Thanks!

OK, here's a slight threadjack: %cpaste /should/ work, in theory. It waits for you to paste (so of course you can use your browsers "paste" menu item), unlike %paste which grabs whatever is on the clipboard (in our case, "vacuum" :) ). Problem is, %cpaste doesn't do the magic formatting that it should, so everything goes in as a single line. "%edit pasted_block" works, so you can manually fix it, but that defeats the purpose to some extent. It's a weird bug in that %cpaste is designed to take (more-or-less) garbage from the paste operation and turn it into useable python.

That sounds like a known issue with pasting into our consoles; because behind the scenes the console uses a single-line HTML INPUT field to read the copy and paste messages, newlines are stripped. There must be some way to fix it, but we've not had much luck so far: unfortunately just replacing it with a multi-line text area didn't fix the problem.

I'll upvote the to-do list item to fix that, anyway.

Good explanation, thx. Also, thx for the upvote. Gotta say, for a free (for me, for now :) ) service, you guys have better, more responsive support than... well, I can't think of better at the moment. Cheers.

Thanks :-)

Hello everyone!

First of all, I second @quaestor you guys have excellent support! Second, sorry for necro bumping but it's the only thread I found about IPython's %paste magic.

I'm interested in knowing what came out of this, this is what I got on IPython2:

TclError: no display name and no $DISPLAY environment variable

And IPython3 when trying to use %paste:

ERROR: Getting text from the clipboard on this platform requires Tkinter.

Thanks in advance!

Cheers.

%paste isn't going to work, because web apps can't access your local clipboard, for security reasons.

But try %cpaste? That works reasonably well...