4

I am trying to embed an IPython qtconsole in my application, in a similar way that I can embed an IPython text console, thus:

from IPython.frontend.terminal.embed import InteractiveShellEmbed
myobj={'jason':10}
shell = InteractiveShellEmbed()
shell.user_ns = myobj
shell()

I can start a QT console in my application by using

from IPython.frontend.qt.console.qtconsoleapp import IPythonQtConsoleApp
app = IPythonQtConsoleApp()
app.initialize()
app.start()

But this has no access to existing objects.

I can start a kernel in my application which has access to existing objects, using:

import IPython
myobj={'jason':10}
IPython.embed_kernel(local_ns=myobj)
#No more code executes

And then connect to it using

ipython qtconsole --existing

Or, in another python script:

from IPython.lib.kernel import find_connection_file
from IPython.frontend.qt.console.qtconsoleapp import IPythonQtConsoleApp

cf=find_connection_file("*")
app = IPythonQtConsoleApp(existing=cf[-1])
app.initialize()
app.start()

But these are not monolithic solutions, both require another shell. The kernel runs in a thread that does not yield, so no more code executes until the kernel exists.

So, my question is, how can I achieve the above in one script?

I've tried using threads and multiprocessing, but as embed_kernel() never returns and obviously must be run before IPythonQTConsoleApp() or the connection file would be missing, I don't see how to manage this.

user202729
  • 3,358
  • 3
  • 25
  • 36
Jay M
  • 3,736
  • 1
  • 24
  • 33
  • There's also [ipython - Provide remote shell for Python script - Stack Overflow](https://stackoverflow.com/questions/29148319/provide-remote-shell-for-python-script) (`background-zmq-ipython` Python package, runs in separate thread) // See also: [python - Embedding IPython Qt console in a PyQt application - Stack Overflow](https://stackoverflow.com/questions/11513132/embedding-ipython-qt-console-in-a-pyqt-application) – user202729 Dec 06 '21 at 15:24

1 Answers1

2

To embed a kernel without blocking, have a look at this example from the IPython repository. IPython knows some clever tricks for integrating itself with the Qt event loop, so you can run a console and your application at the same time. For things like interactive debugging, this is the way to go.

If you need to embed the console into your own application, we've just merged an in-process kernel. Have a look at this example. You'd need to use IPython development versions until the next release, but it would be great to get some early testing.

Thomas K
  • 39,200
  • 7
  • 84
  • 86
  • The example you point to has moved or changed or both. I see https://github.com/ipython/ipython/blob/master/examples/Embedding/ipkernel_qtapp.py and guess that that works for the latest releases, but I don't know how far back it works. It doesn't work in IPython 0.13.2 and any tips for that version would also be great in an updated answer. Especially if it works in pygtk. Thanks! – nealmcb May 31 '14 at 19:32
  • 1
    @nealmcb Thanks, I've updated those links. I don't know how far back they work, but you can check the [examples in the 0.13 branch](https://github.com/ipython/ipython/tree/0.13.x/docs/examples/lib). There are examples for Qt and Wx, so it should be possible to adapt it to Gtk. – Thomas K May 31 '14 at 19:37
  • Cool - thanks - that's working pretty well for me now. The remaining bit of magic seems to be how to cleanly shut down a gtk application in 0.13, after getting it going via ipkernel.start(). Calling gtk.main.quit() doesn't do the trick, nor does closing the window or even typing "quit" in the console, so I've resorted to sys.exit() for now.... – nealmcb Jun 01 '14 at 01:28
  • How would I embed an IPython kernel into an app that doesn't use a GUI event loop like Qt? For example, a WSGI application? – Marc Abramowitz Feb 06 '15 at 19:20
  • You would need to integrate the IPython event loop with whatever event loop is running in that application. I don't think we have any examples of integrating with a webserver event loop, but [this doc section](http://ipython.org/ipython-doc/dev/config/eventloops.html#event-loops-in-the-kernel) should be relevant. – Thomas K Feb 09 '15 at 01:41
  • Do include the code itself into the answer, otherwise the answer is *almost* link-only... (besides the link to the two examples are broken now) (yes, you're an IPython developer, but NAA is NAA) – user202729 Dec 06 '21 at 15:23