jupyter_client
jupyter_client copied to clipboard
Where/how can I get the execution results?
I just wanted to execute a simple code and get execution results using ipykernel. My code is:
import jupyter_client
km, kc = jupyter_client.manager.start_new_kernel()
kc.execute('print("Hello")', silent=False)
state = 'busy'
while state != 'idle' and kc.is_alive():
msg = kc.get_iopub_msg(timeout=100)
if 'content' not in msg:
continue
content = msg['content']
if 'execution_state' in content:
state = content['execution_state']
print("State:", state)
print(msg)
It prints out State: busy
several times and prints msg in json:
{
"buffers": [],
"content": {
"execution_state": "idle"
},
"header": {
"date": "2019-09-14 13:43:15.474096+00:00",
"msg_id": "3af15234-e916ebf72147aca0b8510300",
"msg_type": "status",
"session": "799a6df2-dc330c1b0c1f9c26e73f2f1c",
"username": "hunkim",
"version": "5.3"
},
"metadata": {},
"msg_id": "3af15234-e916ebf72147aca0b8510300",
"msg_type": "status",
"parent_header": {
"date": "2019-09-14 13:43:15.459643+00:00",
"msg_id": "63800e8e-7bb0e5adbd460293c78f1913",
"msg_type": "execute_request",
"session": "1d8720b9-9550df1c20706078496c5e56",
"username": "hunkim",
"version": "5.3"
}
}
Where can I find the execution result, in this case, "Hello"?
I think I figured:
import jupyter_client
km, kc = jupyter_client.manager.start_new_kernel()
kc.execute('print("Hello")', silent=False)
state = 'busy'
while state != 'idle' and kc.is_alive():
msg = kc.get_iopub_msg(timeout=100)
if 'content' not in msg:
continue
content = msg['content']
if 'execution_state' in content:
state = content['execution_state']
print("State:", state)
print(msg)
print("-----------")
I just moved the msg print inside of the loop, and it seems iopub_msg
returns many messages with different msg_types
.
State: busy
{
"buffers": [],
"content": {
"execution_state": "busy"
},
"header": {
"date": "2019-09-14 14:05:26.423761+00:00",
"msg_id": "19cb5e65-c34282e1b236b21829d97945",
"msg_type": "status",
"session": "b7d68e6f-f6bb1275c46bd30b0785adc9",
"username": "hunkim",
"version": "5.3"
},
"metadata": {},
"msg_id": "19cb5e65-c34282e1b236b21829d97945",
"msg_type": "status",
"parent_header": {
"date": "2019-09-14 14:05:26.421658+00:00",
"msg_id": "8f727fc4-4993d0fe523b711a0cd337bc",
"msg_type": "execute_request",
"session": "8ad0d17d-3ea420edc5ce7917caabfa3a",
"username": "hunkim",
"version": "5.3"
}
}
-----------
State: busy
{
"buffers": [],
"content": {
"code": "print(\"Hello\")",
"execution_count": 1
},
"header": {
"date": "2019-09-14 14:05:26.424613+00:00",
"msg_id": "163a2eac-6aae88015f485383c48ee4db",
"msg_type": "execute_input",
"session": "b7d68e6f-f6bb1275c46bd30b0785adc9",
"username": "hunkim",
"version": "5.3"
},
"metadata": {},
"msg_id": "163a2eac-6aae88015f485383c48ee4db",
"msg_type": "execute_input",
"parent_header": {
"date": "2019-09-14 14:05:26.421658+00:00",
"msg_id": "8f727fc4-4993d0fe523b711a0cd337bc",
"msg_type": "execute_request",
"session": "8ad0d17d-3ea420edc5ce7917caabfa3a",
"username": "hunkim",
"version": "5.3"
}
}
-----------
State: busy
{
"buffers": [],
"content": {
"name": "stdout",
"text": "Hello\n"
},
"header": {
"date": "2019-09-14 14:05:26.430333+00:00",
"msg_id": "9e1a7fb8-7a761db798753b972d9346d9",
"msg_type": "stream",
"session": "b7d68e6f-f6bb1275c46bd30b0785adc9",
"username": "hunkim",
"version": "5.3"
},
"metadata": {},
"msg_id": "9e1a7fb8-7a761db798753b972d9346d9",
"msg_type": "stream",
"parent_header": {
"date": "2019-09-14 14:05:26.421658+00:00",
"msg_id": "8f727fc4-4993d0fe523b711a0cd337bc",
"msg_type": "execute_request",
"session": "8ad0d17d-3ea420edc5ce7917caabfa3a",
"username": "hunkim",
"version": "5.3"
}
}
-----------
State: idle
{
"buffers": [],
"content": {
"execution_state": "idle"
},
"header": {
"date": "2019-09-14 14:05:26.433756+00:00",
"msg_id": "3c0c2acd-25af58d170262bc57fbf5a33",
"msg_type": "status",
"session": "b7d68e6f-f6bb1275c46bd30b0785adc9",
"username": "hunkim",
"version": "5.3"
},
"metadata": {},
"msg_id": "3c0c2acd-25af58d170262bc57fbf5a33",
"msg_type": "status",
"parent_header": {
"date": "2019-09-14 14:05:26.421658+00:00",
"msg_id": "8f727fc4-4993d0fe523b711a0cd337bc",
"msg_type": "execute_request",
"session": "8ad0d17d-3ea420edc5ce7917caabfa3a",
"username": "hunkim",
"version": "5.3"
}
}
-----------
Should I check all the messages and their message type to get execution results?