public

Embedding a kernel and starting a ipython notebook that knows how to connect to it. Kind of a work-around similar to ipython notebook --existing if it existed.

  • Download Gist
embed_kernel_and_connected_notebook.py
Python
1 2 3 4 5 6 7 8 9 10 11 12
from IPython import embed_kernel
 
import start_notebook
 
def main():
p = start_notebook.main()
localDict = { 'a':1, 'b':2 }
embed_kernel()
p.kill()
 
if __name__ == '__main__':
main()
start_notebook.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
import sys
import os
import json
import subprocess
 
from IPython.zmq import blockingkernelmanager
from IPython.utils.py3compat import str_to_bytes
from IPython.lib.kernel import find_connection_file
 
def set_session_key_from_connection_file(config, fname):
with open(fname) as f:
s = f.read()
cfg = json.loads(s)
if 'key' in cfg:
config.Session.key = str_to_bytes(cfg['key'])
 
# TODO: this has a few bugs/limitations e.g. can not restart the
# kernel at the moment
def my_kernel_manager(app, pid):
class Wrapped(blockingkernelmanager.BlockingKernelManager):
def start_kernel(self, **kwargs):
cf = find_connection_file(str(pid), profile=app.profile)
set_session_key_from_connection_file(app.config, cf)
self.connection_file = cf
self._launch_args = kwargs.copy()
self.load_connection_file()
 
return Wrapped
 
def start_notebook(pid):
from IPython.frontend.html.notebook.notebookapp import NotebookApp
app = NotebookApp.instance()
app.initialize()
 
KernelManagerClass = my_kernel_manager(app, pid)
sys.modules['__main__'].KernelManagerClass = KernelManagerClass
app.kernel_manager.kernel_manager_class = '__main__.KernelManagerClass'
app.start()
 
def main():
argsStr = str(os.getpid())
 
# imp.load_source only supports .py
pyOrPycFile = os.path.abspath(__file__)
pyFile = '{[0]}.py'.format(os.path.splitext(pyOrPycFile))
 
toRun = ';'.join([
'import imp',
"mod = imp.load_source('mod', r'{}')".format(pyFile),
'mod.start_notebook({})'.format(argsStr)
])
 
print toRun
 
p = subprocess.Popen([sys.executable, '-c', toRun])
return p
 
if __name__ == '__main__':
main()

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.