[IPython-dev] Embed wxIPython/WxController in own app: How to access variables?

Gael Varoquaux gael.varoquaux@normalesup....
Sun Sep 28 16:27:59 CDT 2008


Hi, 

Sorry for being slow to reply, I was travelling this week end.

On Thu, Sep 25, 2008 at 03:39:44PM +0200, Fabian Wenzel wrote:
> thanks for your fast reply - I tried the code you posted in your answer
> but still could not get access to variables. So I believe I did not
> understand all details of your answer, and that may very well be
> because you were not sure what I was trying to accomplish. So here is
> some "dummy" example code:

> ++++++++++++++++++++++++

> # Some class here...
> class Testclass():
>    def __init__(self):
>       self.member = "SOMETHING"

> # Some function here...
> def Testfunc(instance):
>    instance.member = "SOMETHINGELSE"

> # Main program
> if __name__ == "__main__":
>    import wx
>    from IPython.frontend.wx.wx_frontend import WxController
>    from IPython.kernel.core.interpreter import Interpreter

>    # These variable should be accessible from the shell below
>    testinstance = Testclass()

>    # Generate frame
>    app = wx.PySimpleApp()
>    frame = wx.Frame(None,wx.ID_ANY)

>    # Generate shell child
>    shell = WxController(frame,wx.ID_ANY)

>    frame.Show()
>    app.MainLoop()

> +++++++++++++++++++++++++++++++++++++++++++

> I did not check the code for errors, also some things are definitely missing
> (sizers etc., I just typed it into this e-mail), but the idea is that
> in the shell that I create above, I would like to type something like

> Testfunc(testinstance)

> as both, the function and the instance of Testclass() have been defined before. In PyCrust, this is possible.

> Maybe I just did not get the meaning of the namespace and dictionary?

:). I think you should try to learn a bit what a dictionnary is in
Python, and understand the concept of namespace to do these things, but
anyhow, I'll try to help you right away. I don't have the code installed
on this computer, so I'll just try and modify your code. I can't garanty
that the resulting code will actually run :).

++++++++++++++++++++++++

# Some class here...
class Testclass():
   def __init__(self):
      self.member = "SOMETHING"

# Some function here...
def Testfunc(instance):
   instance.member = "SOMETHINGELSE"

# Main program
if __name__ == "__main__":

   import wx
   from IPython.frontend.wx.wx_frontend import WxController
   from IPython.kernel.core.interpreter import Interpreter

   # This variable should be accessible from the shell below
   testinstance = Testclass()

   # Here is a dictionnary which will be the namespace in which our shell
   # runs. We bind the variables we are interested in accessing in it.
   namespace = dict()
   namespace['testinstance'] = testinstance
   namespace['Testfunc'] = Testfunc
   interp = Interpreter(user_ns=namespace)

   # Generate frame
   app = wx.PySimpleApp()
   frame = wx.Frame(None, wx.ID_ANY)

   # Generate shell child
   shell = WxController(frame, wx.ID_ANY, shell=interp)

   frame.Show()
   app.MainLoop()

+++++++++++++++++++++++++++++++++++++++++++

HTH,

Gaël


More information about the IPython-dev mailing list