[IPython-dev] embedding ipython, namespace question

Darren Dale dsdale24 at gmail.com
Sat Jan 10 10:30:30 EST 2015


In case anyone else may find it useful, attached is a small modification of
the qt in-process example that implements the behavior I was looking for.
The namespace gets updated each time the mouse enters the widget, so every
time you use the console, the environment appears to be the same as that of
the GUI.

On Sat Jan 10 2015 at 9:03:48 AM Darren Dale <dsdale24 at gmail.com> wrote:

> I found the start of a solution by digging into the IPython library,
> especially https://github.com/ipython/ipython/blob/master/IPython/
> kernel/zmq/ipkernel.py#L33 . After creating the in-process kernel, it is
> possible to reinitialize the console namespace with, for example,
> `kernel.user_ns = globals()`. Now its just a matter of wiring up the gui so
> the user_ns gets updated whenever the console widget receives focus.
> On Fri Jan 09 2015 at 11:38:33 AM Darren Dale <dsdale24 at gmail.com> wrote:
>
>> On Sat Jan 03 2015 at 10:45:42 AM Darren Dale <dsdale24 at gmail.com> wrote:
>>
>>> Hi Ray,
>>>
>>> On Sat Dec 27 2014 at 8:48:20 PM Osborn, Raymond <rosborn at anl.gov>
>>> wrote:
>>>
>>>>  I don’t really understand what you are trying to achieve, but the
>>>> ‘user_ns’ dictionary isn’t an isolated namespace - it’s the namespace that
>>>> is used by the console, and I would have thought you would have to do some
>>>> kind of injection to add other objects from within the application.
>>>>
>>>
>>> What I am trying to achieve is explicitly documented at
>>> http://ipython.org/ipython-doc/dev/interactive/reference.htm
>>> l#embedding-ipython :
>>>
>>> ---
>>>  It is also possible to embed an IPython shell in a namespace in your
>>> Python code. This allows you to evaluate dynamically the state of your
>>> code, operate with your variables, analyze them, etc. Note however that any
>>> changes you make to values while in the shell do not propagate back to the
>>> running code, so it is safe to modify your values because you won’t break
>>> your code in bizarre ways by doing so.
>>>
>>> Note
>>> At present, embedding IPython cannot be done from inside IPython. Run
>>> the code samples below outside IPython.
>>> [DD: I am not attempting to embed ipython from inside ipython]
>>>
>>> This feature allows you to easily have a fully functional python
>>> environment for doing object introspection anywhere in your code with a
>>> simple function call. In some cases a simple print statement is enough, but
>>> if you need to do more detailed analysis of a code fragment this feature
>>> can be very valuable.
>>>
>>> It can also be useful in scientific computing situations where it is
>>> common to need to do some automatic, computationally intensive part and
>>> then stop to look at data, plots, etc. Opening an IPython instance will
>>> give you full access to your data and functions, and you can resume program
>>> execution once you are done with the interactive part (perhaps to stop
>>> again later, as many times as needed).
>>> The following code snippet is the bare minimum you need to include in
>>> your Python programs for this to work (detailed examples follow later):
>>>
>>>    from IPython import embed
>>>    embed() # this call anywhere in your program will start IPython
>>>
>>> You can also embed an IPython kernel, for use with qtconsole, etc. via
>>> IPython.embed_kernel(). This should function work the same way, but you can
>>> connect an external frontend (ipython qtconsole or ipython console), rather
>>> than interacting with it in the terminal.
>>> ---
>>>
>>> This is impressively simple for the embed function:
>>>
>>> ---
>>> C:\Users\darren> python
>>> Python 2.7.8 |Continuum Analytics, Inc.| (default, Jul  2 2014,
>>> 15:12:11) [MSC v.1500 64 bit (AMD64)] on win32
>>> Type "help", "copyright", "credits" or "license" for more information.
>>> Anaconda is brought to you by Continuum Analytics.
>>> Please check out: http://continuum.io/thanks and https://binstar.org
>>> >>> from IPython import embed
>>> >>> a=1
>>> >>> embed()
>>> Python 2.7.8 |Continuum Analytics, Inc.| (default, Jul  2 2014,
>>> 15:12:11) [MSC v.1500 64 bit (AMD64)]
>>> Type "copyright", "credits" or "license" for more information.
>>>
>>> IPython 2.3.1 -- An enhanced Interactive Python.
>>> Anaconda is brought to you by Continuum Analytics.
>>> Please check out: http://continuum.io/thanks and https://binstar.org
>>> ?         -> Introduction and overview of IPython's features.
>>> %quickref -> Quick reference.
>>> help      -> Python's own help system.
>>> object?   -> Details about 'object', use 'object??' for extra details.
>>>
>>> In [1]: a
>>> Out[1]: 1
>>> ---
>>>
>>> But I have not been able to achieve the same behavior with the qt
>>> in-process console.
>>>
>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ipython-dev/attachments/20150110/699cf809/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: inprocess_qtconsole.py
Type: application/octet-stream
Size: 1457 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/ipython-dev/attachments/20150110/699cf809/attachment.obj>


More information about the IPython-dev mailing list