Skip to content

Commit 1d4b0c9

Browse files
committed
add-displays-to-history
1 parent 5b8ac29 commit 1d4b0c9

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

ipykernel/zmqshell.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
from IPython.core.magic import Magics, line_magic, magics_class
3131
from IPython.core.magics import CodeMagics, MacroToEdit # type:ignore[attr-defined]
3232
from IPython.core.usage import default_banner
33+
from IPython.core.history import HistoryOutput
3334
from IPython.display import Javascript, display
3435
from IPython.utils import openpy
3536
from IPython.utils.process import arg_split, system # type:ignore[attr-defined]
@@ -115,6 +116,11 @@ def publish( # type:ignore[override]
115116
update : bool, optional, keyword-only
116117
If True, send an update_display_data message instead of display_data.
117118
"""
119+
if self.shell is not None and hasattr(self.shell, 'history_manager'):
120+
outputs = self.shell.history_manager.outputs
121+
outputs[self.shell.execution_count].append(
122+
HistoryOutput(output_type="display_data", bundle=data)
123+
)
118124
self._flush_streams()
119125
if metadata is None:
120126
metadata = {}

tests/test_zmq_shell.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,26 @@ def test_unregister_hook(self):
209209
second = self.disp_pub.unregister_hook(hook)
210210
assert not bool(second)
211211

212+
def test_display_stored_in_history(self):
213+
"""
214+
Test that published display data gets stored in shell history
215+
for %notebook magic support.
216+
"""
217+
# Mock shell with history manager
218+
mock_shell = MagicMock()
219+
mock_shell.execution_count = 1
220+
mock_shell.history_manager.outputs = {1: []}
221+
222+
self.disp_pub.shell = mock_shell
223+
224+
data = {'text/plain': 'test output'}
225+
self.disp_pub.publish(data)
226+
227+
# Check that output was stored in history
228+
stored_outputs = mock_shell.history_manager.outputs[1]
229+
assert len(stored_outputs) == 1
230+
assert stored_outputs[0].output_type == "display_data"
231+
assert stored_outputs[0].bundle == data
212232

213233
def test_magics(tmp_path):
214234
context = zmq.Context()

0 commit comments

Comments
 (0)