3
votes

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?

1
try to use ipdb, just set trace point in place where you need interactive access to script with ipdb.set_trace() - Denis
I'll look into it. There is not really a straightforward way to implement what I described, right? I googled a lot and didn't get anywhere. - Martin Preusse
But looks like similiar, anyway I don't know how to help you with ipython notebook, sorry. - Denis
hey , did you success to connect to iPythone in a webpage template_? - ytsejam

1 Answers

0
votes

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)