Skip to content

Instantly share code, notes, and snippets.

@manuzhang
Created March 13, 2019 07:19
Show Gist options
  • Save manuzhang/31a38fc09bdc9583d23019c9ec332218 to your computer and use it in GitHub Desktop.
Save manuzhang/31a38fc09bdc9583d23019c9ec332218 to your computer and use it in GitHub Desktop.
#!/usr/bin/python3.6
from jupyter_client import BlockingKernelClient
from jupyter_client import KernelManager
import codecs
import nbformat
nb = nbformat.read(codecs.open("./empty_cell.ipynb",'r',encoding='utf-8'),as_version=4)
km = KernelManager(kernel_name=nb.metadata.get('kernelspec', {}).get('name', 'python'))
km.start_kernel()
kc = km.blocking_client()
kc.start_channels()
kc.wait_for_ready(timeout=60)
def print_msg(msg):
if msg['msg_type'] == 'error':
print(msg['content'])
try:
for index,cell in enumerate(nb.cells):
if cell.cell_type == 'code':
code = cell.source
kc.execute_interactive(code, output_hook=print_msg)
finally:
kc.stop_channels()
km.shutdown_kernel()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment