Skip to content

Instantly share code, notes, and snippets.

@andrewssobral
Created September 23, 2018 00:29
Show Gist options
  • Save andrewssobral/bd0f0d384c0e7aada66a6454767076f3 to your computer and use it in GitHub Desktop.
Save andrewssobral/bd0f0d384c0e7aada66a6454767076f3 to your computer and use it in GitHub Desktop.
Jupyter Client (Python)
from jupyter_client.kernelspec import NATIVE_KERNEL_NAME
from jupyter_client.manager import start_new_kernel
km, kc = start_new_kernel(kernel_name=NATIVE_KERNEL_NAME)
def run_cell(client, code, timeout=15):
print("\nrunning: ", code)
reply = client.execute_interactive(code, timeout=timeout)
status = reply['content']['status']
if status == 'ok':
print('succeeded!')
elif status == 'error':
print('failed!')
for line in reply['content']['traceback']:
print(line)
run_cell(kc, "result='Hello world!'")
run_cell(kc, "print(result)")
kc.shutdown()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment