Skip to content

Commit 523e3e3

Browse files
committed
Moved placeholder to a separate function, ruff format.
1 parent 1ed888e commit 523e3e3

File tree

1 file changed

+26
-51
lines changed

1 file changed

+26
-51
lines changed

jupyter_server_documents/outputs/manager.py

Lines changed: 26 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import glob
21
import json
32
import os
43
from pathlib import Path, PurePath
@@ -7,17 +6,12 @@
76
from pycrdt import Map
87

98
from traitlets.config import LoggingConfigurable
10-
from traitlets import (
11-
Dict,
12-
Instance,
13-
Int,
14-
default
15-
)
9+
from traitlets import Dict, Instance, Int, default
1610

1711
from jupyter_core.paths import jupyter_runtime_dir
1812

19-
class OutputsManager(LoggingConfigurable):
2013

14+
class OutputsManager(LoggingConfigurable):
2115
_last_output_index = Dict(default_value={})
2216
_stream_count = Dict(default_value={})
2317

@@ -27,7 +21,7 @@ class OutputsManager(LoggingConfigurable):
2721
@default("outputs_path")
2822
def _default_outputs_path(self):
2923
return Path(jupyter_runtime_dir()) / "outputs"
30-
24+
3125
def _ensure_path(self, file_id, cell_id):
3226
nested_dir = self.outputs_path / file_id / cell_id
3327
nested_dir.mkdir(parents=True, exist_ok=True)
@@ -39,7 +33,16 @@ def _build_path(self, file_id, cell_id=None, output_index=None):
3933
if output_index is not None:
4034
path = path / f"{output_index}.output"
4135
return path
42-
36+
37+
def _create_outputs_placeholder(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+
4346
def get_output(self, file_id, cell_id, output_index):
4447
"""Get an output by file_id, cell_id, and output_index."""
4548
path = self._build_path(file_id, cell_id, output_index)
@@ -48,21 +51,18 @@ def get_output(self, file_id, cell_id, output_index):
4851
with open(path, "r", encoding="utf-8") as f:
4952
output = json.loads(f.read())
5053
return output
51-
54+
5255
def get_outputs(self, file_id, cell_id):
5356
"""Get all outputs by file_id, cell_id."""
5457
path = self._build_path(file_id, cell_id)
5558
if not os.path.isdir(path):
5659
raise FileNotFoundError(f"The output dir doesn't exist: {path}")
57-
60+
5861
outputs = []
5962

60-
output_files = [
61-
(f, int(f.stem))
62-
for f in path.glob("*.output")
63-
]
63+
output_files = [(f, int(f.stem)) for f in path.glob("*.output")]
6464
output_files.sort(key=lambda x: x[1])
65-
output_files = output_files[:self.stream_limit]
65+
output_files = output_files[: self.stream_limit]
6666
has_more_files = len(output_files) >= self.stream_limit
6767

6868
outputs = []
@@ -72,17 +72,10 @@ def get_outputs(self, file_id, cell_id):
7272
outputs.append(output)
7373

7474
if has_more_files:
75-
url = f"/api/outputs/{file_id}/{cell_id}/stream"
76-
placeholder = {
77-
"output_type": "display_data",
78-
"data": {
79-
'text/html': f'<a href="{url}">Click this link to see the full stream output</a>'
80-
}
81-
}
75+
placeholder = self._create_outputs_placeholder(file_id, cell_id)
8276
outputs.append(json.dumps(placeholder))
8377

8478
return outputs
85-
8679

8780
def get_stream(self, file_id, cell_id):
8881
"Get the stream output for a cell by file_id and cell_id."
@@ -95,7 +88,7 @@ def get_stream(self, file_id, cell_id):
9588

9689
def write(self, file_id, cell_id, output):
9790
"""Write a new output for file_id and cell_id.
98-
91+
9992
Returns a placeholder output (pycrdt.Map) or None if no placeholder
10093
output should be written to the ydoc.
10194
"""
@@ -125,7 +118,7 @@ def write_stream(self, file_id, cell_id, output, placeholder) -> Map:
125118
self._ensure_path(file_id, cell_id)
126119
path = self._build_path(file_id, cell_id) / "stream"
127120
text = output["text"]
128-
mode = 'a' if os.path.isfile(path) else 'w'
121+
mode = "a" if os.path.isfile(path) else "w"
129122
with open(path, "a", encoding="utf-8") as f:
130123
f.write(text)
131124
url = f"/api/outputs/{file_id}/{cell_id}/stream"
@@ -141,12 +134,7 @@ def write_stream(self, file_id, cell_id, output, placeholder) -> Map:
141134
placeholder = placeholder
142135
elif count == self.stream_limit:
143136
# Return a link to the full stream output
144-
placeholder = Map({
145-
"output_type": "display_data",
146-
"data": {
147-
'text/html': f'<a href="{url}">Click this link to see the full stream output</a>'
148-
}
149-
})
137+
placeholder = Map(self._create_outputs_placeholder(file_id, cell_id))
150138
elif count > self.stream_limit:
151139
# Return None to indicate that no placeholder should be written to the ydoc
152140
placeholder = None
@@ -172,24 +160,11 @@ def clear(self, file_id, cell_id=None):
172160
def create_placeholder_output(output_type: str, url: str):
173161
metadata = dict(url=url)
174162
if output_type == "stream":
175-
output = Map({
176-
"output_type": "stream",
177-
"text": "",
178-
"metadata": metadata
179-
})
163+
output = Map({"output_type": "stream", "text": "", "metadata": metadata})
180164
elif output_type == "display_data":
181-
output = Map({
182-
"output_type": "display_data",
183-
"metadata": metadata
184-
})
165+
output = Map({"output_type": "display_data", "metadata": metadata})
185166
elif output_type == "execute_result":
186-
output = Map({
187-
"output_type": "execute_result",
188-
"metadata": metadata
189-
})
167+
output = Map({"output_type": "execute_result", "metadata": metadata})
190168
elif output_type == "error":
191-
output = Map({
192-
"output_type": "error",
193-
"metadata": metadata
194-
})
195-
return output
169+
output = Map({"output_type": "error", "metadata": metadata})
170+
return output

0 commit comments

Comments
 (0)