@@ -34,15 +34,6 @@ def _build_path(self, file_id, cell_id=None, output_index=None):
3434 path = path / f"{ output_index } .output"
3535 return path
3636
37- def _create_outputs_link (self , file_id : str , cell_id : str ):
38- url = f"/api/outputs/{ file_id } /{ cell_id } /stream"
39- return {
40- "output_type" : "display_data" ,
41- "data" : {
42- "text/html" : f'<a href="{ url } ">Click this link to see the full stream output</a>'
43- },
44- }
45-
4637 def get_output (self , file_id , cell_id , output_index ):
4738 """Get an output by file_id, cell_id, and output_index."""
4839 path = self ._build_path (file_id , cell_id , output_index )
@@ -72,7 +63,8 @@ def get_outputs(self, file_id, cell_id):
7263 outputs .append (output )
7364
7465 if has_more_files :
75- placeholder = self ._create_outputs_link (file_id , cell_id )
66+ url = create_output_url (file_id , cell_id )
67+ placeholder = create_placeholder_dict ("display_data" , url , full = True )
7668 outputs .append (json .dumps (placeholder ))
7769
7870 return outputs
@@ -106,10 +98,10 @@ def write_output(self, file_id, cell_id, output):
10698 data = json .dumps (output , ensure_ascii = False )
10799 with open (path , "w" , encoding = "utf-8" ) as f :
108100 f .write (data )
109- url = f"/api/outputs/ { file_id } / { cell_id } / { index } .output"
101+ url = create_output_url ( file_id , cell_id , index )
110102 self .log .info (f"Wrote output: { url } " )
111103 return create_placeholder_output (output ["output_type" ], url )
112-
104+
113105 def write_stream (self , file_id , cell_id , output , placeholder ) -> Map :
114106 # How many stream outputs have been written for this cell previously
115107 count = self ._stream_count .get (cell_id , 0 )
@@ -118,12 +110,10 @@ def write_stream(self, file_id, cell_id, output, placeholder) -> Map:
118110 self ._ensure_path (file_id , cell_id )
119111 path = self ._build_path (file_id , cell_id ) / "stream"
120112 text = output ["text" ]
121- mode = "a" if os .path .isfile (path ) else "w"
122113 with open (path , "a" , encoding = "utf-8" ) as f :
123114 f .write (text )
124- url = f"/api/outputs/ { file_id } / { cell_id } /stream"
115+ url = create_output_url ( file_id , cell_id )
125116 self .log .info (f"Wrote stream: { url } " )
126-
127117 # Increment the count
128118 count = count + 1
129119 self ._stream_count [cell_id ] = count
@@ -134,7 +124,7 @@ def write_stream(self, file_id, cell_id, output, placeholder) -> Map:
134124 placeholder = placeholder
135125 elif count == self .stream_limit :
136126 # Return a link to the full stream output
137- placeholder = Map ( self . _create_outputs_link ( file_id , cell_id ) )
127+ placeholder = create_placeholder_output ( "display_data" , url , full = True )
138128 elif count > self .stream_limit :
139129 # Return None to indicate that no placeholder should be written to the ydoc
140130 placeholder = None
@@ -157,14 +147,71 @@ def clear(self, file_id, cell_id=None):
157147 pass
158148
159149
160- def create_placeholder_output (output_type : str , url : str ):
150+ def create_output_url (self , file_id : str , cell_id : str , output_index : int = None ) -> str :
151+ """
152+ Create the URL for an output or stream.
153+
154+ Parameters:
155+ - file_id (str): The ID of the file.
156+ - cell_id (str): The ID of the cell.
157+ - output_index (int, optional): The index of the output. If None, returns the stream URL.
158+
159+ Returns:
160+ - str: The URL string for the output or stream.
161+ """
162+ if output_index is None :
163+ return f"/api/outputs/{ file_id } /{ cell_id } /stream"
164+ else :
165+ return f"/api/outputs/{ file_id } /{ cell_id } /{ output_index } .output"
166+
167+ def create_placeholder_dict (output_type : str , url : str , full : bool = False ):
168+ """
169+ Build a placeholder output dict for the given output_type and url.
170+ If full is True and output_type is "display_data", returns a display_data output
171+ with an HTML link to the full stream output.
172+
173+ Parameters:
174+ - output_type (str): The type of the output.
175+ - url (str): The URL associated with the output.
176+ - full (bool): Whether to create a full output placeholder with a link.
177+
178+ Returns:
179+ - dict: The placeholder output dictionary.
180+
181+ Raises:
182+ - ValueError: If the output_type is unknown.
183+ """
161184 metadata = dict (url = url )
185+ if full and output_type == "display_data" :
186+ return {
187+ "output_type" : "display_data" ,
188+ "data" : {
189+ "text/html" : f'<a href="{ url } ">Click this link to see the full stream output</a>'
190+ },
191+ }
162192 if output_type == "stream" :
163- output = Map ( {"output_type" : "stream" , "text" : "" , "metadata" : metadata })
193+ return {"output_type" : "stream" , "text" : "" , "metadata" : metadata }
164194 elif output_type == "display_data" :
165- output = Map ( {"output_type" : "display_data" , "metadata" : metadata })
195+ return {"output_type" : "display_data" , "metadata" : metadata }
166196 elif output_type == "execute_result" :
167- output = Map ( {"output_type" : "execute_result" , "metadata" : metadata })
197+ return {"output_type" : "execute_result" , "metadata" : metadata }
168198 elif output_type == "error" :
169- output = Map ({"output_type" : "error" , "metadata" : metadata })
170- return output
199+ return {"output_type" : "error" , "metadata" : metadata }
200+ else :
201+ raise ValueError (f"Unknown output_type: { output_type } " )
202+
203+ def create_placeholder_output (output_type : str , url : str , full : bool = False ):
204+ """
205+ Creates a placeholder output Map for the given output_type and url.
206+ If full is True and output_type is "display_data", creates a display_data output with an HTML link.
207+
208+ Parameters:
209+ - output_type (str): The type of the output.
210+ - url (str): The URL associated with the output.
211+ - full (bool): Whether to create a full output placeholder with a link.
212+
213+ Returns:
214+ - Map: The placeholder output `ycrdt.Map`.
215+ """
216+ output_dict = create_placeholder_dict (output_type , url , full = full )
217+ return Map (output_dict )
0 commit comments