[IPython-user] IPython embedding

Fernando Perez Fernando.Perez at colorado.edu
Tue Oct 18 12:13:30 CDT 2005

Dear Geza,

First, I'd like to continue this discussion on the ipython list, because it's 
something which may also benefit others, and we can also get insights from 
other users as well.  You can find the subscription info here:

http://scipy.net/mailman/listinfo/ipython-user - users list
http://scipy.net/mailman/listinfo/ipython-dev - developers list

I've copied your message verbatim here, for the list benefit.  My reply is below.

Geza Seriani wrote:
> Dear Fernando,
> in my group we are developing a pre-/post- processing tool for  
> numerical modeling and
> analysis of seismograms based on Python, wxPython and matplotlib for  
> graphics.
> We wanted the tool be interactive for the analysis phase of the   
> seismograms.
> For this reason we are using PyShell and Pycrust  that embed very  
> easily in the
> wx GUI interface.
> We discovered recently the power of IPython shell and so we wanted to  
> embed it
> in our tool, may be under Pycrust.
> But it does not work.
> Do you have any suggestion and help for us??
>   Below are the list of what done and of the errors we get.
> Thanks a lot for any help you could give us.
> Best regards,
> Geza Seriani
> -------------------------------------------------------
> Geza Seriani
> OGS - Ocean & Geophysics Natl. Institute
> Borgo Grotta Gigante 42/C
> 34010 Sgonico, Trieste (Italy)
> gseriani at ogs.trieste.it
> -------------------------------------------------------
> import wx.py
> from IPython.Shell import IPShellEmbed
> ....
> sh=IPShellEmbed()
> wx.py.shell.Shell(self.panel_transf, -1, InterpClass=sh)
> ....
> It seems that ipython does not support redirection of I/O on different
> devices from standard terminal.
> The error we get is:
> ------------------------------------------------------------------------ 
> ---
> exceptions.TypeError Traceback (most
> recent call last)
> /Users/franz/Sviluppo/SismoVi2/datavi.py
> 652
> 653 if __name__=='__main__':
> 654 app=wx.PySimpleApp()
> --> 655 main()
> 656 app.MainLoop()
> /Users/franz/Sviluppo/SismoVi2/datavi.py in main(parent, pos, size,  
> counter)
> 647 path=os.path.dirname(sys.argv[0])
> 648 if path!="": path += "/"
> --> 649 params=DataParamsDialog(parent, -1, "DataVi "+counter+" -
> input parameters", pos=pos)
> 650 params.SetMenu()
> 651 params.Show(True)
> /Users/franz/Sviluppo/SismoVi2/datavi.py in __init__(self, parent, id,
> title, pos)
> 129 self.list_transf = wx.ListBox(self.panel_transf, -1,
> size=(190, 300), choices=self.view_list, style=wx.LB_SINGLE)
> 130 sh=IPShellEmbed()
> --> 131 self.text_transf = wx.py.shell.Shell(self.panel_transf,
> -1, size=(300, 300), InterpClass=sh)
> 132 #self.text_transf = wx.TextCtrl(self.panel_transf, -1,
> "", size=(300, 300), style=wx.TE_MULTILINE)
> 133 self.button_select_transf =
> wx.Button(self.panel_transf, -1, "Select Operator")
> //Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site- 
> packages/wx-2.6-mac-unicode/wx/py/shell.py
> in __init__(self, parent, id, pos, size, style, introText, locals,
> InterpClass, *args, **kwds)
> 201 stdin=self.reader,
> 202
> stdout=PseudoFileOut(self.writeOut),
> --> 203
> stderr=PseudoFileErr(self.writeErr),
> 204 *args, **kwds)
> 205 # Set up the buffer.
> TypeError: __call__() got an unexpected keyword argument 'stdin'

========================= End Geza's message ======================

This same question popped up on the list just a few days ago, and my reply was 
the following:

If you want to embed ipython inside a WX app (like I said the Qt guys did),
you'll likely need to do so inside an empty WX widget, NOT in a full PyCrust.
   You can look at ipython's nbshell project for inspiration, which does run
inside a WX widget.  But please note that this code is still 100% experimental
(not even alpha), so use at your own risk.  It's not very documented at all
yet, and requires a few low-level tricks to make it work.



In addition, I should mention that today I got an email from a group who has 
successfully embedded ipython into a Qt app:

We embedded iPython in BrainVISA (http://brainvisa.info). BrainVISA was
written with PyQt before we discovered iPython. We simply added a --shell
option that is related to the following code:

if neuroConfig.shell:
   ipshell = IPython.Shell.IPShellQt( [ '-qthread' ] )

The rest of the code is unchanged since some PyQt functions are redefined when
'-qthread' is used with iPython (that was part of what we did with Fernando).
For example QApplication.exec_loop() is redefined to do nothing. I have not
personally test this on Windows or Mac yet but if you want more feedback, you
can try to post a message on the forum (http://brainvisa.info/forum). However
the iPython embedding is still experimental in BrainVISA and I think there is
not a many people that use it. Those who know iPython is embedded (including
me), really enjoy it. It is really helpfull in many situations.



To summarize the problem:

1.  I think you need to stay away from pycrust if you want to embed ipython. 
I suspect the two collide badly.  Using a simpler WX text widget which can 
handle input should make life easier (whatever pycrust is based on should do).

2. I would try something like:

import wx.py
from IPython.Shell import IPShellEmbed
# This will give you an embedded shell which is aware of wx threading:

# now, try to initialize this shell into your widget...

3. If this doesn't work, try instead:

ipshell = IPython.Shell.IPShellWX( [ '-wthread' ] )

in your widget of choice.  This is more like what the Qt guys did.

I'd appreciate it if you could report success/failure back to the list, as I'd 
like to document the necessary steps to do this on both WX and Qt.  There 
seems to be growing interest in this kind of functionality.



More information about the IPython-user mailing list