@@ -29,7 +29,6 @@ def _default_outputs_path(self):
2929
3030 def _ensure_path (self , file_id , cell_id ):
3131 nested_dir = self .outputs_path / file_id / cell_id
32- self .log .info (f"Creating directory: { nested_dir } " )
3332 nested_dir .mkdir (parents = True , exist_ok = True )
3433
3534 def _build_path (self , file_id , cell_id = None , output_index = None ):
@@ -42,7 +41,6 @@ def _build_path(self, file_id, cell_id=None, output_index=None):
4241
4342 def get_output (self , file_id , cell_id , output_index ):
4443 """Get an outputs by file_id, cell_id, and output_index."""
45- self .log .info (f"OutputsManager.get: { file_id } { cell_id } { output_index } " )
4644 path = self ._build_path (file_id , cell_id , output_index )
4745 if not os .path .isfile (path ):
4846 raise FileNotFoundError (f"The output file doesn't exist: { path } " )
@@ -60,12 +58,15 @@ def get_stream(self, file_id, cell_id):
6058 return output
6159
6260 def write (self , file_id , cell_id , output ):
63- """Write a new output for file_id and cell_id."""
64- self .log .info (f"OutputsManager.write: { file_id } { cell_id } { output } " )
65- result = self .write_output (file_id , cell_id , output )
61+ """Write a new output for file_id and cell_id.
62+
63+ Returns a placeholder output (pycrdt.Map) or None if no placeholder
64+ output should be written to the ydoc.
65+ """
66+ placeholder = self .write_output (file_id , cell_id , output )
6667 if output ["output_type" ] == "stream" and self .stream_limit is not None :
67- result = self .write_stream (file_id , cell_id , output )
68- return result
68+ placeholder = self .write_stream (file_id , cell_id , output , placeholder )
69+ return placeholder
6970
7071 def write_output (self , file_id , cell_id , output ):
7172 self ._ensure_path (file_id , cell_id )
@@ -77,9 +78,10 @@ def write_output(self, file_id, cell_id, output):
7778 with open (path , "w" , encoding = "utf-8" ) as f :
7879 f .write (data )
7980 url = f"/api/outputs/{ file_id } /{ cell_id } /{ index } .output"
80- return Map ({})
81+ self .log .info (f"Wrote output: { url } " )
82+ return create_placeholder_output (output ["output_type" ], url )
8183
82- def write_stream (self , file_id , cell_id , output ) -> Map :
84+ def write_stream (self , file_id , cell_id , output , placeholder ) -> Map :
8385 # How many stream outputs have been written for this cell previously
8486 count = self ._stream_count .get (cell_id , 0 )
8587
@@ -91,15 +93,16 @@ def write_stream(self, file_id, cell_id, output) -> Map:
9193 with open (path , "a" , encoding = "utf-8" ) as f :
9294 f .write (text )
9395 url = f"/api/outputs/{ file_id } /{ cell_id } /stream"
96+ self .log .info (f"Wrote stream: { url } " )
9497
9598 # Increment the count
9699 count = count + 1
97100 self ._stream_count [cell_id ] = count
98101
99102 # Now create the placeholder output
100103 if count < self .stream_limit :
101- # Return the original if we haven't reached the limit
102- placeholder = Map ({})
104+ # Return the original placeholder if we haven't reached the limit
105+ placeholder = placeholder
103106 elif count == self .stream_limit :
104107 # Return a link to the full stream output
105108 placeholder = Map ({
@@ -109,6 +112,7 @@ def write_stream(self, file_id, cell_id, output) -> Map:
109112 }
110113 })
111114 elif count > self .stream_limit :
115+ # Return None to indicate that no placeholder should be written to the ydoc
112116 placeholder = None
113117 return placeholder
114118
@@ -123,4 +127,33 @@ def clear(self, file_id, cell_id=None):
123127 except KeyError :
124128 pass
125129 path = self ._build_path (file_id , cell_id )
126- shutil .rmtree (path )
130+ try :
131+ shutil .rmtree (path )
132+ except FileNotFoundError :
133+ pass
134+
135+
136+ def create_placeholder_output (output_type : str , url : str ):
137+ metadata = dict (url = url )
138+ if output_type == "stream" :
139+ output = Map ({
140+ "output_type" : "stream" ,
141+ "text" : "" ,
142+ "metadata" : metadata
143+ })
144+ elif output_type == "display_data" :
145+ output = Map ({
146+ "output_type" : "display_data" ,
147+ "metadata" : metadata
148+ })
149+ elif output_type == "execute_result" :
150+ output = Map ({
151+ "output_type" : "execute_result" ,
152+ "metadata" : metadata
153+ })
154+ elif output_type == "error" :
155+ output = Map ({
156+ "output_type" : "error" ,
157+ "metadata" : metadata
158+ })
159+ return output
0 commit comments