Skip to content

Instantly share code, notes, and snippets.

@lesteve
Created March 22, 2013 00:17
Show Gist options
  • Save lesteve/5217994 to your computer and use it in GitHub Desktop.
Save lesteve/5217994 to your computer and use it in GitHub Desktop.
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 }
embed_kernel()
p.kill()
if __name__ == '__main__':
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 = 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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment