Pasting in a screen session into ipython - ipython

When running ipython 5 and 6 in a gnu-screen session, pasting multiple lines does not work.
If I run ipython in a normal terminal session, I see the following when I paste 3 import lines separated by newlines:
In [1]: import datetime
...: import os
...: import glob
...:
...:
In [2]:
If I do the same into a gnu-screen session, I see:
In [1]: import datetime
In [2]:
i.e., the results are truncated at the first newline.
Is there any ipython or screen configuration item that can be used to work around this and achieve the same paste behavior in both terminals?
EDIT: This issue has apparently been reported somewhere on the ipython mailing list, as referenced on this issue.

I ran into this problem as well with Gnu screen. The workaround I'm using is that I switched to Tmux. Tmux seems to have similar functionality to screen, and has a handy status bar that lists open virtual windows at the bottom. The default prefix key is CTRL-b.
More importantly for this question, pasting into an ipython terminal within a Tmux session works as expected for me.

Related

Prevent ipython %paste from polluting shell previous commands

There seems to have been a recent change in ipython (I'm using 8.4.0) where %paste acts as if it were a literal paste (ctrl+v).
This is undesired because
I can already do direct pastes with ctrl+shift+v in my terminal
I like being able to quickly access previous manually typed commands instead of stuff pasted in.
I sometimes paste large dumps
Example
In both example I paste in a few commands and press the up arrow.
clipboard
Suppose I have the following in my clipboard
a=[1,2,3,4,5]
b=[4,5,6,7,8]
ipython 7.13.0 (desired behavior)
In [1]: %paste
a=[1,2,3,4,5]
b=[4,5,6,7,8]
## -- End pasted text --
In [2]: %paste
ipython 8.4.0 (new behavior)
In [1]: %paste
a=[1,2,3,4,5]
b=[4,5,6,7,8]
## -- End pasted text --
In [3]: a=[1,2,3,4,5]
...: b=[4,5,6,7,8]
conclusion
Are there any ipython configurations I can change to bring back the old paste magic behavior?

IPython: run script starting from a specific line

I am writing my script interactively with IPython. This is what I currently do:
write a chunk of code,
run in ipython with "run -i file_name.py".
make changes and repeat 2 until I think it is OK .
comment out the entire previous chunk.
write new chunk of code that is based on the previous one.
go back to step 2.
......
Is there more efficient way? Can I start a script from a specific line while using all the variables in current namespace?
Use ipdb ("pip install ipdb" on the command line to install it).
Suppose you want to run script "foo.py" from line 18 to 23.
You'll want to start like this:
ipdb foo.py
Now, let's jump to line 18 (i.e., ignore all the lines before the 18th):
ipdb> j 18
Next, we set a breakpoint at line 23 (we don't want to go further):
ipdb> b 23
Finally, let's execute:
ipdb> c
Job done :)
I'd personally also use the ipython notebook, but you call also use you favorite text editor and always copy out the chunk of code you want to run and use the magic command %paste to run that chunk in the ipython shell. It will take care of indentation for you.
Use the magic of %edit stuff.py (first use) and %ed -p (after the first use) and it will invoke your $EDITOR from inside of ipython. Upon exiting from the editor ipython will run the script (unless you called %ed -x). That is by far the fastest way I found to work in CLI-ipython. The notebooks are nice, but I like having a real editor for code.
(Based on lev's answer)
From the interactive shell:
%run -i -d foo.py
should then enter the debugger, and proceed with:
j <line_number>
c
etc.
EDIT: unfortunately, this seems to sort of break ipython's magic %debug command.
An IPython Notebook allows you to interactively run scripts line by line. It comes with IPython, just run:
ipython notebook
from the terminal to launch it. Its a web interface to IPython, where you can save the notebooks to *.py files by clicking save as in the settings.
Here's some more info from this video.
For something fast as well as flexible use http://qtconsole.readthedocs.io/en/stable/
It is similar to the Jupyter notebook based on your browsers (as pointed out by #agonti and #magellan88, but presumably much faster. It also has emacs style keybindings.
I use ipdb, ipython, comupled with tmux and vim and get almost IDE like features and much faster.

Simultaneously displaying and capturing standard out in IPython?

I'm interested in implementing a behavior in IPython that would be like a combination of ! and !!. I'm trying to use an IPython terminal as an adjunct to my (Windows) shell. For a long running command (e.g., a build script) I would like to be able to watch the output as it streams by as ! does. I would also like to capture the output of the command into the output history as !! does, but this defers printing anything until all output is available.
Does anyone have any suggestions as to how to implement something like this? I'm guessing that a IPython.utils.io.Tee() object would be useful here, but I don't know enough about IPython to hook this up properly.
Here is a snippet of code I just tried in iPython notebook v2.3, which seems to do what was requested:
import sys
import IPython.utils.io
outputstream = IPython.utils.io.Tee("outputfile.log", "w", channel="stdout")
outputstream.write("Hello worlds!\n")
outputstream.close()
logstream=open("outputfile.log", "r")
sys.stdout.write("Read back from log file:\n")
sys.stdout.write(logstream.read())
The log file is created in the same directory as the iPython notebook file, and the output from running this cell is displayed thus:
Hello worlds!
Read back from log file:
Hello worlds!
I haven't tried this in the iPython terminal, but see no reason it wouldn't work as well there.
(Researched and answered as part of the Oxford participation in http://aaronswartzhackathon.org)

Ipython notebook crash using rmagic extension

I am trying to run the rmagic functions extension example, but ipython crashes with console error message '\u used without hex digits in character string starting "c:\u". I suspect that this is an R error message caused by rmagic passing c:\path instead of c:\ or c:/.
There is probably an easy way to fix this (IPython or Notebook startup parameters?) , but, as a newbie to rmagic and rpy2, I need some expert help please.
The following simple snippet from the example causes the kernel to die:
import rpy2
%load_ext rmagic
%R x=1
I am a windows user...
Found a solution to this ipython/windows problem here
I am a Windows user. I went to my IPython extention folder which for me is: C:\Python27\Lib\site-packages\IPython\extensions
and opened rmagic.py for editing. Found the line
self.r('png("%s/Rplots%%03d.png",%s)' % (tmpd, png_args))
and replaced it with the line:
self.r('png("%s/Rplots%%03d.png",%s, type="cairo")' % (tmpd.replace('\\', '/'), png_args))
The reason you do this is described here
and here

Can't embed correctly Window in PyGTK with Socket

I'm following the the tutorial on PyGTK socket where you embed a xterm window to another application.
I downloaded the file from here: http://www.pygtk.org/pygtk2tutorial/examples/socket.py
When I run it following this: http://faq.pygtk.org/index.py?req=show&file=faq19.015.htp I get two different results:
On Ubuntu Karmic Koala (GTK 2.16) the xterm appears detached from the socket window and it not usable.
On Lucid Lynx (GTK 2.17) the xterm window disappears, and the socket window seems to get the background from the xterm, but nothing happens and of course, it's not usable.
I've modified the script to look like this:
#!/usr/bin/python
import pygtk
pygtk.require('2.0')
import gtk
from subprocess import Popen
def plugged_event(widget):
print "Inserted a widget"
window = gtk.Window()
window.connect("destroy", gtk.main_quit)
window.show()
socket = gtk.Socket()
window.add(socket)
socket.connect("plug-added", plugged_event)
sock_id = str(socket.get_id())
cmd = ["xterm", "-into", sock_id]
Popen(cmd)
socket.show()
gtk.main()
Everything seems to work fine, the xterm is embedded to the socket window, but I can't use the keyboard on it and the cursor appears empty. This is the same in either versions of GTK. So, does any one know if this can be fixed or if this is a Bug?
On first glance that seems like a bug in the xterm plug/socketing. I can reproduce on Lucid. If you have vim-gtk installed, replace your cmd with:
cmd = ["gvim", "--socketid", sock_id]
And your script runs fine to embed vim.