python - 在 Python 程序中启动 IPython,连接 IPython Notebook

标签 python ipython jupyter-notebook

我认为可以将 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/

相关文章:

python - 使用 AdamOptimizer 继续训练自定义 tf.Estimator

python - IPython 导入错误 : cannot import name Layout

python - 如何在 IPython 中完成 win32com 代码?

python - iPython:导入的模块找不到它的共享库

apache-spark - 来自边缘节点 Jupyter 笔记本的 spark jdbc 连接

python - 我的神经网络有什么问题,它给出了错误的预测

python - Colab的使用限制持续多长时间?

python - 在 Windows 上直接调用 Python 脚本时 sys.argv 中没有参数

python - Python 中的 getter 和 setter

python - 下采样 wav 音频文件