Skip to content

Fix stream message handling in notebook kernel #1658

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions altimate_notebook_kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,23 +59,22 @@ def execute(self, code, user_expressions=None):
while True:
try:
msg = self.kernel_client.get_iopub_msg(timeout=1)

def datetime_converter(o):
if isinstance(o, datetime):
return o.__str__()

# Print formatted JSON

# print("msg", msg)

if msg['msg_type'] == 'stream':
# for stdout
output.append({'mime': 'text/plain', 'value': msg['content']['text']})
break
elif msg['msg_type'] == 'comm_open':
state = msg['content']['data']['state']
state['model_id'] = msg['content']['comm_id']
# Handle comm_open messages
output.append({'mime': 'application/vnd.jupyter.widget-view+json', 'value': state})
elif msg['msg_type'] == 'execute_result' or msg['msg_type'] == 'display_data':
elif msg['msg_type'] in ['execute_result', 'display_data']:
# Flag to check if any key other than 'text/plain' exists
other_keys_exist = False

Expand All @@ -88,12 +87,10 @@ def datetime_converter(o):
other_keys_exist = True

# If no other keys exist, add the 'text/plain' value
if not other_keys_exist:
if not other_keys_exist and 'text/plain' in msg['content']['data']:
Copy link
Preview

Copilot AI May 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] It would be beneficial to add a brief comment explaining the rationale behind checking for 'text/plain' in msg['content']['data'] to clarify that this condition intentionally filters messages with solely plain text output.

Copilot uses AI. Check for mistakes.

output.append({'mime': 'text/plain', 'value': msg['content']['data']['text/plain']})
break
elif msg['msg_type'] == 'error':
output.append({'mime': 'text/plain', 'value': '\n'.join(msg['content']['traceback'])})
break
elif msg['msg_type'] == 'status' and msg['content']['execution_state'] == 'idle':
break
except queue.Empty:
Expand Down
Loading