[IPython-dev] embedding ipython, namespace question

Osborn, Raymond rosborn at anl.gov
Sat Dec 27 20:48:13 EST 2014

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. The following code is added shortly after the example I gave in the MainWindow class:

        self.user_ns['plotview'] = self.plotview
        self.user_ns['treeview'] = self.treeview
        self.user_ns['mainwindow'] = self

so that other parts of the application are accessible within the console. You could add all the items in the globals() dictionary if you wanted:

        for k, v in globals().items():
            self.user_ns[k] = v

which is presumably equivalent to what Matthias suggested, but you would still have to inject new variables as they are created.


On Dec 27, 2014, at 5:24 PM, Darren Dale <dsdale24 at gmail.com<mailto:dsdale24 at gmail.com>> wrote:

I passed None because http://ipython.org/ipython-doc/stable/api/generated/IPython.terminal.embed.html?highlight=embed#IPython.terminal.embed.InteractiveShellEmbed states: "If given as None, they are automatically taken from the scope where the shell was called, so that program variables become visible." That is precisely what I want, but can't figure out how to do it with the Qt console.

I don't want to inject variables to an isolated namespace, I want the console to share the application's namespace.

On Sat Dec 27 2014 at 6:02:54 PM Matthias Bussonnier <bussonniermatthias at gmail.com<mailto:bussonniermatthias at gmail.com>> wrote:

Le 27 déc. 2014 à 23:43, Darren Dale <dsdale24 at gmail.com<mailto:dsdale24 at gmail.com>> a écrit :

Thanks Ray. Unfortunately, I want the ipython console's namespace to reflect that of the application in which it is embedded, not a specific widget. There is an example that appears to do this at https://github.com/ipython/ipython/blob/master/examples/Embedding/embed_function.py , but I'm trying to do the same with the Qt widget. At the top of my main gui module, I do:

    from IPython.qt.console.rich_ipython_widget import RichIPythonWidget
    from IPython.qt.inprocess import QtInProcessKernelManager

    # Create an in-process kernel
    kernel_manager = QtInProcessKernelManager(user_ns=None)

Don't you want to just pass `globals()` instead of `None` here?
user_ns kwarg here is **the mutable namespace** (i.e. a dict) you want your kernel to work on.  If you pass None,
it will create a new empty object for you. If you pass `{"a":1}` then your kernel will see a variable `a` with a value of 1 (mutable)

So I guess what you want to pass here is either globals(), locals(), or a dict you constructed yourself,
from the bit you want available.

You can also do as raymond said, and inject things into user_ns after getting the reference to it.

IPython-dev mailing list
IPython-dev at scipy.org<mailto:IPython-dev at scipy.org>
IPython-dev mailing list
IPython-dev at scipy.org<mailto:IPython-dev at scipy.org>

Ray Osborn, Senior Scientist
Materials Science Division
Argonne National Laboratory
Argonne, IL 60439, USA
Phone: +1 (630) 252-9011
Email: ROsborn at anl.gov<mailto:ROsborn at anl.gov>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ipython-dev/attachments/20141228/eee46bbe/attachment.html>

More information about the IPython-dev mailing list