3333from  IPython .utils .process  import  arg_split , system   # type:ignore[attr-defined] 
3434from  jupyter_client .session  import  Session , extract_header 
3535from  jupyter_core .paths  import  jupyter_runtime_dir 
36- from  traitlets  import  Any , CBool , CBytes , Dict , Instance , Type , default , observe 
36+ from  traitlets  import  Any , Bool ,  CBool , CBytes , Dict , Instance , Type , default , observe 
3737
3838from  ipykernel  import  connect_qtconsole , get_connection_file , get_connection_info 
3939from  ipykernel .displayhook  import  ZMQShellDisplayHook 
4040from  ipykernel .jsonutil  import  encode_images , json_clean 
4141
42+ try :
43+     from  IPython .core .history  import  HistoryOutput 
44+ except  ImportError :
45+     HistoryOutput  =  None   # type: ignore[assignment,misc] 
46+ 
4247# ----------------------------------------------------------------------------- 
4348# Functions and classes 
4449# ----------------------------------------------------------------------------- 
@@ -52,6 +57,11 @@ class ZMQDisplayPublisher(DisplayPublisher):
5257    parent_header  =  Dict ({})
5358    topic  =  CBytes (b"display_data" )
5459
60+     store_display_history  =  Bool (
61+         False ,
62+         help = "If set to True, store display outputs in the history manager. Default is False." ,
63+     ).tag (config = True )
64+ 
5565    # thread_local: 
5666    # An attribute used to ensure the correct output message 
5767    # is processed. See ipykernel Issue 113 for a discussion. 
@@ -100,6 +110,21 @@ def publish(
100110        update : bool, optional, keyword-only 
101111            If True, send an update_display_data message instead of display_data. 
102112        """ 
113+         if  (
114+             self .store_display_history 
115+             and  self .shell  is  not   None 
116+             and  hasattr (self .shell , "history_manager" )
117+             and  HistoryOutput  is  not   None 
118+         ):
119+             # Reference: github.com/ipython/ipython/pull/14998 
120+             exec_count  =  self .shell .execution_count 
121+             if  getattr (self .shell .display_pub , "_in_post_execute" , False ):
122+                 exec_count  -=  1 
123+             outputs  =  getattr (self .shell .history_manager , "outputs" , None )
124+             if  outputs  is  not   None :
125+                 outputs .setdefault (exec_count , []).append (
126+                     HistoryOutput (output_type = "display_data" , bundle = data )
127+                 )
103128        self ._flush_streams ()
104129        if  metadata  is  None :
105130            metadata  =  {}
0 commit comments