File tree Expand file tree Collapse file tree 2 files changed +26
-0
lines changed
Expand file tree Collapse file tree 2 files changed +26
-0
lines changed Original file line number Diff line number Diff line change 3030from IPython .core .magic import Magics , line_magic , magics_class
3131from IPython .core .magics import CodeMagics , MacroToEdit # type:ignore[attr-defined]
3232from IPython .core .usage import default_banner
33+ from IPython .core .history import HistoryOutput
3334from IPython .display import Javascript , display
3435from IPython .utils import openpy
3536from 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 = {}
Original file line number Diff line number Diff 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
213233def test_magics (tmp_path ):
214234 context = zmq .Context ()
You can’t perform that action at this time.
0 commit comments