如何在将现有对象传递到该控制台的同时在我的应用程序中生成IPython qtconsole [英] How can I spawn an IPython qtconsole within my app while passing existing objects to that console

查看:265
本文介绍了如何在将现有对象传递到该控制台的同时在我的应用程序中生成IPython qtconsole的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的应用程序中嵌入一个IPython qtconsole,类似于我可以嵌入一个IPython文本控制台,因此:

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()

我可以使用

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

但是这无法访问现有对象。

我可以在我的应用程序中启动一个可以访问现有对象的内核,使用:

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

然后使用

ipython qtconsole --existing

或者,在另一个python脚本:

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()

但这些不是单片解决方案,都需要另外一个shell。内核在一个不产生的线程中运行,因此在内核存在之前不再执行代码。

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?

我尝试过使用线程和多处理,但由于 embed_kernel()永远不会返回,显然必须在 IPythonQTConsoleApp之前运行()或连接文件将丢失,我不知道如何管理它。

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.

PS如果有一个代表大于1000的人请添加一个qtconsole标签。

P.S. Will somebody with a rep of >1000 please add a qtconsole tag.

推荐答案

要嵌入内核而不阻塞,请看一下从IPython存储库此示例。 IPython知道一些巧妙的技巧,可以将自己与Qt事件循环集成,因此您可以同时运行控制台和应用程序。对于交互式调试这样的事情,这是可行的方法。

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.

如果您需要将控制台嵌入到您自己的应用程序中,我们刚刚合并一个进程内核。请查看此示例。在下一个版本之前,您需要使用IPython开发版本,但是进行一些早期测试会很棒。

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.

这篇关于如何在将现有对象传递到该控制台的同时在我的应用程序中生成IPython qtconsole的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆