Skip to content

Commit

Permalink
fix(import): create molten cells for code with no output (#224)
Browse files Browse the repository at this point in the history
Co-authored-by: Ingo Maßen <ingo.massen2@vodafone.com>
  • Loading branch information
benlubas and Ingo Maßen authored Aug 19, 2024
1 parent 81aad33 commit 35c1941
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 23 deletions.
9 changes: 5 additions & 4 deletions rplugin/python3/molten/ipynb.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,15 @@ def import_outputs(nvim: Nvim, kernel: MoltenKernel, filepath: str):
continue

if nb_line >= len(nb_contents) - 1:
if len(cell["outputs"]) == 0:
buf_line += 1
break
# we're done. This is a match, we'll create the output
output = Output(cell["execution_count"])
output.old = True
output.success = True
if output.execution_count:
output.status = OutputStatus.DONE
else:
output.status = OutputStatus.NEW

for output_data in cell["outputs"]:
m_chunk, success = handle_output_types(nvim, output_data.get("output_type"), kernel, output_data)
output.chunks.append(m_chunk)
Expand Down Expand Up @@ -97,7 +99,6 @@ def import_outputs(nvim: Nvim, kernel: MoltenKernel, filepath: str):
kernel.extmark_namespace,
kernel.options,
)
output.status = OutputStatus.DONE
kernel.outputs[span].output = output
kernel.update_interface()
else:
Expand Down
34 changes: 19 additions & 15 deletions rplugin/python3/molten/outputbuffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,20 @@ def _get_header_text(self, output: Output) -> str:
else:
execution_count = str(output.execution_count)

if output.status == OutputStatus.HOLD:
status = "* On Hold"
elif output.status == OutputStatus.DONE:
if output.success:
status = "✓ Done"
else:
status = "✗ Failed"
elif output.status == OutputStatus.RUNNING:
status = "... Running"
else:
raise ValueError("bad output.status: %s" % output.status)
match output.status:
case OutputStatus.HOLD:
status = "* On Hold"
case OutputStatus.DONE:
if output.success:
status = "✓ Done"
else:
status = "✗ Failed"
case OutputStatus.RUNNING:
status = "... Running"
case OutputStatus.NEW:
status = ""
case _:
raise ValueError("bad output.status: %s" % output.status)

if output.old:
old = "[OLD] "
Expand Down Expand Up @@ -96,7 +99,10 @@ def _get_header_text(self, output: Output) -> str:
else:
time = ""

return f"{old}Out[{execution_count}]: {status} {time}".rstrip()
if output.status == OutputStatus.NEW:
return f"Out[_]: Never Run"
else:
return f"{old}Out[{execution_count}]: {status} {time}".rstrip()

def enter(self, anchor: Position) -> bool:
entered = False
Expand Down Expand Up @@ -274,9 +280,7 @@ def show_floating_win(self, anchor: Position) -> None:
offset = 0
if self.options.cover_empty_lines:
offset = self.calculate_offset(anchor)
win_row = (
self._buffer_to_window_lineno(anchor.lineno + offset) + 1
)
win_row = self._buffer_to_window_lineno(anchor.lineno + offset) + 1
else:
win_row = self._buffer_to_window_lineno(anchor.lineno + 1)

Expand Down
14 changes: 10 additions & 4 deletions rplugin/python3/molten/outputchunks.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def __init__(self, text: str):
self.output_type = "display_data"

def __repr__(self) -> str:
return f"TextOutputChunk(\"{self.text}\")"
return f'TextOutputChunk("{self.text}")'

def place(
self,
Expand Down Expand Up @@ -169,8 +169,13 @@ def place(

class OutputStatus(Enum):
HOLD = 0
"""Waiting to run this cell"""
RUNNING = 1
"""Currently running, waiting for code to finish running"""
DONE = 2
"""Code has already been run"""
NEW = 3
"""Cell was created, nothing run, no output"""


class Output:
Expand Down Expand Up @@ -201,16 +206,17 @@ def merge_text_chunks(self):
character, this is b/c outputs before a \r aren't shown, and so, should be deleted"""
if (
len(self.chunks) >= 2
and isinstance((c1 := self.chunks[-2]), TextOutputChunk)
and isinstance((c2 := self.chunks[-1]), TextOutputChunk)
and isinstance((c1 := self.chunks[-2]), TextOutputChunk)
and isinstance((c2 := self.chunks[-1]), TextOutputChunk)
):
c1.text += c2.text
c1.text = "\n".join([re.sub(r".*\r", "", x) for x in c1.text.split("\n")[:-1]])
c1.jupyter_data = { "text/plain": c1.text }
c1.jupyter_data = {"text/plain": c1.text}
self.chunks.pop()
elif len(self.chunks) > 0 and isinstance((c1 := self.chunks[0]), TextOutputChunk):
c1.text = "\n".join([re.sub(r".*\r", "", x) for x in c1.text.split("\n")[:-1]])


def to_outputchunk(
nvim: Nvim,
alloc_file: Callable[
Expand Down

0 comments on commit 35c1941

Please sign in to comment.