Fix %%python display() routing to wrong kernel#371
Merged
blink1073 merged 3 commits intoCalysto:mainfrom Mar 13, 2026
Merged
Conversation
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.
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #187.
Summary
MetaKernel.meta_kernelwas only assigned once — when the very first kernel instance was created. Any subsequentdisplay()call inside a%%pythoncell silently routed to that stale kernel instance rather than the currently-executing one, causing rich display output (HTML,Markdown, etc.) to be dropped.MetaKernel.meta_kernel = self.kernelat the top ofPythonMagic.eval()sodisplay()always routes to the active kernel.Test plan
capture_send_messages()context manager totests/utils.pyfor interceptingsend_responsecalls in tests.test_python_magic_html_displaycovering:HTML(...)as the last expression in%%python→execute_resultwithtext/htmldisplay(HTML(...))explicit call →display_datawithtext/htmldisplay(HTML(...))internally →display_datawithtext/html(the pattern from the issue)