Skip to content

Commit

Permalink
fix: error io (quarto-dev#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
benlubas authored Jan 19, 2024
1 parent a3628fa commit 3883374
Showing 1 changed file with 31 additions and 21 deletions.
52 changes: 31 additions & 21 deletions rplugin/python3/molten/ipynb.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from molten.moltenbuffer import MoltenKernel
import os
from molten.outputbuffer import OutputBuffer
from molten.outputchunks import Output, OutputStatus, to_outputchunk
from molten.outputchunks import ErrorOutputChunk, Output, OutputStatus, to_outputchunk
from molten.position import DynamicPosition

from molten.utils import MoltenException, notify_error, notify_info, notify_warn
Expand Down Expand Up @@ -63,27 +63,12 @@ def import_outputs(nvim: Nvim, kernel: MoltenKernel, filepath: str):
# we're done. This is a match, we'll create the output
output = Output(cell["execution_count"])
output.old = True
output.success = True
for output_data in cell["outputs"]:
if output_data.get("output_type") == "stream":
output.chunks.append(
to_outputchunk(
nvim,
kernel.runtime._alloc_file,
{ "text/plain": output_data.get("text") },
output_data.get("metadata"),
kernel.options,
)
)
else:
output.chunks.append(
to_outputchunk(
nvim,
kernel.runtime._alloc_file,
output_data.get("data"),
output_data.get("metadata"),
kernel.options,
)
)
m_chunk, success = handle_output_types(nvim, output_data.get("output_type"), kernel, output_data)
output.chunks.append(m_chunk)
output.success &= success

start = DynamicPosition(
nvim,
kernel.extmark_namespace,
Expand Down Expand Up @@ -129,6 +114,31 @@ def import_outputs(nvim: Nvim, kernel: MoltenKernel, filepath: str):
nvim, f"Failed to load output for {failed} running cell that would be overridden"
)

def handle_output_types(nvim: Nvim, output_type: str, kernel: MoltenKernel, output_data):
chunk = None
success = True
match output_type:
case "stream":
chunk = to_outputchunk(
nvim,
kernel.runtime._alloc_file,
{ "text/plain": output_data.get("text") },
output_data.get("metadata"),
kernel.options,
)
case "error":
chunk = ErrorOutputChunk(output_data["ename"], output_data["evalue"], output_data["traceback"])
chunk.extras = output_data
success = False
case _:
chunk = to_outputchunk(
nvim,
kernel.runtime._alloc_file,
output_data.get("data"),
output_data.get("metadata"),
kernel.options,
)
return chunk, success

def export_outputs(nvim: Nvim, kernel: MoltenKernel, filepath: str, overwrite: bool):
"""Export outputs of the current file/kernel to a .ipynb file with the given name."""
Expand Down

0 comments on commit 3883374

Please sign in to comment.