%connect_info
{ "stdin_port": 61593, "ip": "127.0.0.1", "control_port": 61594, "hb_port": 61595, "signature_scheme": "hmac-sha256", "key": "798a8515-07b4-43c3-8dab-da4108b900cf", "shell_port": 61591, "transport": "tcp", "iopub_port": 61592 } Paste the above JSON into a file, and connect with: $> ipython <app> --existing <file> or, if you are local, you can connect with just: $> ipython <app> --existing kernel-89295799-4f7a-41b2-9606-fae6cd6a3e2e.json or even just: $> ipython <app> --existing if this is the most recent IPython session you have started.
from IPython.config import Application
cf = Application.instance().connection_file
cf
u'/Users/epi/.ipython/profile_default/security/kernel-89295799-4f7a-41b2-9606-fae6cd6a3e2e.json'
#%loadpy ipytest.py
%%file ipytest.py
from IPython.lib.kernel import find_connection_file
from IPython.kernel import BlockingKernelClient
import os
import sqlite3
from flask import Flask, request, session, g, redirect, url_for, abort, \
render_template, flash, jsonify
# create our little application :)
app = Flask(__name__)
app.config.from_object(__name__)
@app.route('/ipython/<cf>')
def show_user_profile(cf):
#baseurl='/home/epilib/Envs/env1/.ipython/profile_default/security/'
#km=os.path.join(baseurl,'kernel-6ca68119-8070-4c42-be79-688b569f3b8c.json')
#km=os.path.join(baseurl,cf)
cf = cf.replace('&&','/')
km = BlockingKernelClient(connection_file=cf)
# load connection info and init communication
km.load_connection_file()
km.start_channels()
# show the user profile for that user
run_cell(km, 'a=5')
return 'connected to IPython notebook using : %s' % cf
@app.route('/')
def hello():
print 'hello'
def run_cell(km, code):
# now we can run code. This is done on the shell channel
shell = km.shell_channel
print
print "running:"
print code
# execution is immediate and async, returning a UUID
msg_id = shell.execute(code)
# get_msg can block for a reply
reply = shell.get_msg()
status = reply['content']['status']
if status == 'ok':
print 'succeeded!'
elif status == 'error':
print 'failed!'
for line in reply['content']['traceback']:
print line
@app.route('/test/')
def index():
params = dict(request.args.items())
cf = params['connectionfile']
dictname = params['mydict']
key = params['mykey']
km = BlockingKernelClient(connection_file=cf)
km.load_connection_file()
km.start_channels()
url=str('someurl')
run_cell(km, '%s={"%s": "%s"}' % (dictname,key,url))
return 'connected to IPython notebook using : %s' % cf
if __name__ == '__main__':
app.run(host='localhost')
Overwriting ipytest.py
run it on the same host where the iPython notebook is running python ipytest.py it should print in the shell something like :
running:
mydictname={"mykeyname": "someurl"}
succeeded!
127.0.0.1 - - [24/Oct/2014 14:09:18] "GET /test/?connectionfile=/Users/epi/.ipython/profile_default/security/kernel-89295799-4f7a-41b2-9606-fae6cd6a3e2e.json&mydict=mydictname&mykey=mykeyname HTTP/1.1" 200 -
INFO:werkzeug:127.0.0.1 - - [24/Oct/2014 14:09:18] "GET /test/?connectionfile=/Users/epi/.ipython/profile_default/security/kernel-89295799-4f7a-41b2-9606-fae6cd6a3e2e.json&mydict=mydictname&mykey=mykeyname HTTP/1.1" 200 -
from IPython.display import IFrame
IFrame('http://localhost:5000/test/?connectionfile=%s&mydict=mydictname&mykey=mykeyname' %cf ,600,100)
#import time
#time.sleep(4)
mydictname
{'mykeyname': 'someurl'}
have a look at the URL :
'http://localhost:5000/test/?connectionfile=%s&mydict=mydictname&mykey=mykeyname'
it take as input :
the connection file cf :
?connectionfile=%s
where %s = cf :
from IPython.config import Application
cf = Application.instance().connection_file
a string with the name to give to a dictionary that is generated in the flask app
mydict=mydictname
one more string with the name for a key in the previos generated dictionary
mykey=mykeyname
On the flask side, the code will generated the new dictionary, add the value "someurl" to the dict[key] and return the data to the notebook kernel
Note, a notebook like this will fail if you try to do "run all", because flask will find the kernel busy so it will wait for it to finish before to run the code, this means the dict will not be available at the time the cell [13] is executed.
Runnin the notebook cell by cells works just fine. an example like this canbe used to run "flsk widget" that are connected to the notebook in a non automated way ... (drop down menu, )