Skip to content

Fix %%python display() routing to wrong kernel#371

Merged
blink1073 merged 3 commits intoCalysto:mainfrom
blink1073:fix-187
Mar 13, 2026
Merged

Fix %%python display() routing to wrong kernel#371
blink1073 merged 3 commits intoCalysto:mainfrom
blink1073:fix-187

Conversation

@blink1073
Copy link
Copy Markdown
Contributor

@blink1073 blink1073 commented Mar 13, 2026

Closes #187.

Summary

  • MetaKernel.meta_kernel was only assigned once — when the very first kernel instance was created. Any subsequent display() call inside a %%python cell silently routed to that stale kernel instance rather than the currently-executing one, causing rich display output (HTML, Markdown, etc.) to be dropped.
  • Fix: update MetaKernel.meta_kernel = self.kernel at the top of PythonMagic.eval() so display() always routes to the active kernel.

Test plan

  • Added capture_send_messages() context manager to tests/utils.py for intercepting send_response calls in tests.
  • Added test_python_magic_html_display covering:
    • HTML(...) as the last expression in %%pythonexecute_result with text/html
    • display(HTML(...)) explicit call → display_data with text/html
    • A user-defined function calling display(HTML(...)) internally → display_data with text/html (the pattern from the issue)

MetaKernel.meta_kernel was only set once at first-kernel creation,
so display() calls inside %%python silently routed to a stale kernel
instance instead of the currently-executing one. Fix by updating
meta_kernel in PythonMagic.eval() before each evaluation.

Adds capture_send_messages() test helper and test_python_magic_html_display
covering returning HTML objects, explicit display() calls, and display()
calls inside user-defined functions.
@blink1073 blink1073 added the bug label Mar 13, 2026
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Mar 13, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.99%. Comparing base (5b3d58d) to head (4a1e219).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #371   +/-   ##
=======================================
  Coverage   90.99%   90.99%           
=======================================
  Files          51       51           
  Lines        2909     2911    +2     
  Branches      408      408           
=======================================
+ Hits         2647     2649    +2     
  Misses        183      183           
  Partials       79       79           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

After %%python runs on a kernel, the globally monkey-patched
IPython.display.display must route output to that kernel (the parent),
not to a stale instance from a previous %%python call on a different kernel.
@blink1073 blink1073 merged commit a3042aa into Calysto:main Mar 13, 2026
25 checks passed
@blink1073 blink1073 deleted the fix-187 branch March 13, 2026 17:59
@blink1073 blink1073 mentioned this pull request Mar 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

how to display html produced by a %%python cell

2 participants