Forked from lesteve/embed_kernel_and_connected_notebook.py
Created
April 23, 2014 15:11
-
-
Save lmatthieu/11219302 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 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() | |
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
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