1414class OutputsManager (LoggingConfigurable ):
1515 _last_output_index = Dict (default_value = {})
1616 _stream_count = Dict (default_value = {})
17+ _output_index_by_display_id = Dict (default_value = {})
1718
1819 outputs_path = Instance (PurePath , help = "The local runtime dir" )
1920 stream_limit = Int (default_value = 10 , config = True , allow_none = True )
@@ -34,6 +35,10 @@ def _build_path(self, file_id, cell_id=None, output_index=None):
3435 path = path / f"{ output_index } .output"
3536 return path
3637
38+ def get_output_index (self , display_id : str ):
39+ """Returns output index for a cell by display_id"""
40+ return self ._output_index_by_display_id .get (display_id )
41+
3742 def get_output (self , file_id , cell_id , output_index ):
3843 """Get an output by file_id, cell_id, and output_index."""
3944 path = self ._build_path (file_id , cell_id , output_index )
@@ -102,23 +107,34 @@ def get_stream(self, file_id, cell_id):
102107 with open (path , "r" , encoding = "utf-8" ) as f :
103108 output = f .read ()
104109 return output
105-
106- def write (self , file_id , cell_id , output ):
110+
111+ def write (self , file_id , cell_id , output , display_id = None ):
107112 """Write a new output for file_id and cell_id.
108113
109114 Returns a placeholder output (pycrdt.Map) or None if no placeholder
110115 output should be written to the ydoc.
111116 """
112- placeholder = self .write_output (file_id , cell_id , output )
117+ placeholder = self .write_output (file_id , cell_id , output , display_id )
113118 if output ["output_type" ] == "stream" and self .stream_limit is not None :
114119 placeholder = self .write_stream (file_id , cell_id , output , placeholder )
115120 return placeholder
116121
117- def write_output (self , file_id , cell_id , output ):
122+ def write_output (self , file_id , cell_id , output , display_id = None ):
123+ print (f"display_id:{ display_id } passed into write_output" )
118124 self ._ensure_path (file_id , cell_id )
119125 last_index = self ._last_output_index .get (cell_id , - 1 )
120- index = last_index + 1
121- self ._last_output_index [cell_id ] = index
126+ if display_id :
127+ index = self ._output_index_by_display_id .get (display_id )
128+ print (f"Found index: { index } for display_id { display_id } " )
129+ if index is None :
130+ index = last_index + 1
131+ self ._last_output_index [cell_id ] = index
132+ self ._output_index_by_display_id [display_id ] = index
133+ print (f"Generated new index: { index } for display_id: { display_id } " )
134+ else :
135+ index = last_index + 1
136+ self ._last_output_index [cell_id ] = index
137+
122138 path = self ._build_path (file_id , cell_id , index )
123139 data = json .dumps (output , ensure_ascii = False )
124140 with open (path , "w" , encoding = "utf-8" ) as f :
@@ -165,6 +181,10 @@ def clear(self, file_id, cell_id=None):
165181 del self ._stream_count [cell_id ]
166182 except KeyError :
167183 pass
184+ try :
185+ del self ._last_output_index [cell_id ]
186+ except KeyError :
187+ pass
168188 path = self ._build_path (file_id , cell_id )
169189 try :
170190 shutil .rmtree (path )
0 commit comments