我认为可以将 IPython 实例嵌入到 Python 程序中并使用控制台/笔记本/qt 控制台“连接”到它们。我尝试了 IPython 文档,但没有找到它。
我想在“常规”Python 程序中启动 IPython,让它访问程序的命名空间,连接 IPython 笔记本并在我关闭/退出笔记本后继续 Python 程序。
dataframe = run_some_program_get_data()
# start IPython with access to 'dataframe'
# continue program with dataframe
dataframe.to_csv('file.txt')
如何在普通 Python 程序中启动 IPython 内核?以及如何设置 IPython 笔记本应该连接到哪个内核?
最佳答案
我正在运行最新的 IPython 2.1 和 Anaconda 2.0.1。
有一些directions on this page它可以帮助您设置一个 IPython 实例用于托管目的。以下是调整它以在本地运行的方法。
在终端/命令提示符下运行“ipython profile create nbserver
”
在刚刚创建的配置文件目录中,(通常是~/.ipython/profile_nbserver)编辑文件ipython_notebook_config.py。默认情况下,该文件的所有字段都已注释;您需要取消注释和编辑的最小集合如下:
c = get_config()
# Kernel config
c.IPKernelApp.pylab = 'inline' # if you want plotting support always
# Notebook config
c.NotebookApp.ip = '*' #or keep it 'localhost'
c.NotebookApp.open_browser = False
# It is a good idea to put it on a known, fixed port
c.NotebookApp.port = 9999
然后打开 2 个终端/command_prompts,这样你就可以检查它是否有效。在第一种类型中(不要在任何一行中放置下划线)
ipython console -i --profile nbserver
然后在该窗口中输入 a = 1
和第二个
ipython console -i --profile nbserver --existing
输入print(a)
是的...您可以将“console”替换为“notebook”或“qtconsole” (但对于 qt-console 去掉 -i 开关)
关于python - 在 Python 程序中启动 IPython,连接 IPython Notebook,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19254421/