简体   繁体   中英

Start IPython in a Python program, connect with IPython Notebook

I think it's possible to embed IPython instances in Python programs and 'connect' to them with a console/notebook/qt-console. I tried the IPython docs but didn't get to it.

I would like to start IPython in a 'regular' Python program, give it access to the program's namespace, connect with a IPython notebook and continue the Python program after I closed/quit the notebook.

dataframe = run_some_program_get_data()

# start IPython with access to 'dataframe'

# continue program with dataframe  
dataframe.to_csv('file.txt')

How can I start an IPython kernel in a normal Python program? And how can I set to which kernel an IPython notebook should connect?

I'm running the latest IPython 2.1 with Anaconda 2.0.1.

There are some directions on this page which help you set up a IPython instance for hosting purposes. Here is how to tweak this to run locally.

run " ipython profile create nbserver " at the terminal/command prompt

In the profile directory just created, (usually ~/.ipython/profile_nbserver) edit the file ipython_notebook_config.py. By default, the file has all fields commented; the minimum set you need to uncomment and edit is the following:

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

Then open 2 terminals/command_prompts so you can check if it works. In the first type (don't put an underscore in either line)

ipython console -i --profile nbserver

then type in that window a = 1

and the 2nd

ipython console -i --profile nbserver --existing

type print(a)

and yes... you can replace the word "console" with "notebook" or "qtconsole" (but for qt-console take out the -i switch)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM