[IPython-dev] embedding ipython, namespace question

Darren Dale dsdale24 at gmail.com
Sat Dec 27 17:43:10 EST 2014


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:

try:
    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)
    kernel_manager.start_kernel()
    kernel = kernel_manager.kernel
    kernel.gui = 'qt4'

    kernel_client = kernel_manager.client()
    kernel_client.start_channels()
except ImportError:
    pass

and then when I initialize my QMainWindow, I call:

    def _load_ipython(self):
        def stop():
            kernel_client.stop_channels()
            kernel_manager.shutdown_kernel()

        control = RichIPythonWidget()
        self.ipythonDockWidget.setWidget(control)
        control.kernel_manager = kernel_manager
        control.kernel_client = kernel_client
        control.exit_requested.connect(stop)

That gets me almost all of the way there. I just need the console to share
the application's namespace, but don't know where or how to pass the
appropriate config arguments.

Darren


On Sat Dec 27 2014 at 4:42:38 PM Osborn, Raymond <rosborn at anl.gov> wrote:

>  We synchronize an IPython shell to a PySide QTreeView in NeXpy, an
> application for viewing and analyzing HDF5 files (
> http://nexpy.github.io/nexpy/). If it’s any help, you can find the source
> code at https://github.com/nexpy/nexpy, which was, I think, based on the
> same example you cite. The application is initialized in
> nexpy/gui/consoleapp.py, which subclasses IPythonConsoleApp, but the main
> PySide windows are defined in mainwindow.py.
>
>  Here are some of the critical lines in mainwindow.py:
>
>  class MainWindow(QtGui.QMainWindow):
>
>      def __init__(self, app, tree, config=None):
>
>          self.console = RichIPythonWidget(config=self.config,
> parent=rightpane)
>         self.console.kernel_manager = QtInProcessKernelManager(
> config=self.config)
>         self.console.kernel_manager.start_kernel()
>         self.console.kernel_manager.kernel.gui = 'qt4'
>         self.console.kernel_client = self.console.kernel_manager.client()
>         self.console.kernel_client.start_channels()
>
>          self.shell = self.console.kernel_manager.kernel.shell
>         self.user_ns = self.console.kernel_manager.kernel.shell.user_ns
>
>  The last line makes the dictionary of the IPython shell, i.e., user_ns,
> available to the rest of the GUI. That must be what the “pass a user_ns”
> refers to.
>
>  Best regards,
> Ray
>
>  On Dec 27, 2014, at 2:31 PM, Darren Dale <dsdale24 at gmail.com> wrote:
>
> Hello,
>
>  (sorry for double-posting on ipython-user. I was advised to post here
> instead.)
>
>  I'm working on embedding an ipython qtconsole in my applilcation as a
> dock widget. The https://github.com/ipython/ipython/blob/master/
> examples/Embedding/inprocess_qtconsole.py example was a helpful first
> step. As variables are created or updated in the main application, I'd like
> to be able to inspect them in the qtconsole. Is it possible for the
> qtconsole's namespace to be shared or linked with that of the main
> application? It seems like this should be possible, based on the
> documentation for http://ipython.org/ipython-doc/1/api/generated/
> IPython.terminal.embed.html#IPython.terminal.embed.InteractiveShellEmbed .
> I'm creating my kernel and client at the beginning of my module, and then
> wiring up the widget when my application's main window is about to show.
> But if I run `dir()` in the embedded ipython qt console, the list is
> identical to what I get if I run `dir()` in the standalone `ipython
> qtconsole` program.
>
>  I've seen some comments along the lines of "pass a user_ns", but I have
> no idea what this means, or specifically how to do it.
>
>  Thanks,
> Darren
>
> _______________________________________________
> IPython-dev mailing list
> IPython-dev at scipy.org
> http://mail.scipy.org/mailman/listinfo/ipython-dev
>
>
> --
> Ray Osborn, Senior Scientist
> Materials Science Division
> Argonne National Laboratory
> Argonne, IL 60439, USA
> Phone: +1 (630) 252-9011
> Email: ROsborn at anl.gov
>
>
>   _______________________________________________
> IPython-dev mailing list
> IPython-dev at scipy.org
> http://mail.scipy.org/mailman/listinfo/ipython-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ipython-dev/attachments/20141227/a4a2881d/attachment.html>


More information about the IPython-dev mailing list