Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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.
from IPython import embed_kernel
import start_notebook
def main():
p = start_notebook.main()
localDict = { 'a':1, 'b':2 }
if __name__ == '__main__':
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 =
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()
return Wrapped
def start_notebook(pid):
from IPython.frontend.html.notebook.notebookapp import NotebookApp
app = NotebookApp.instance()
KernelManagerClass = my_kernel_manager(app, pid)
sys.modules['__main__'].KernelManagerClass = KernelManagerClass
app.kernel_manager.kernel_manager_class = '__main__.KernelManagerClass'
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),
print toRun
p = subprocess.Popen([sys.executable, '-c', toRun])
return p
if __name__ == '__main__':
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment