-
-
Save anonymous/1ef39d31c16e3153f3e7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
def run_cell(km, code): | |
# now we can run code. This is done on the shell channel | |
shell = km.shell_channel | |
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='144.76.93.231') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment