Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 65308f0

Browse files
committed
fix-notebook-magic
1 parent 1699518 commit 65308f0

1 file changed

Lines changed: 64 additions & 24 deletions

File tree

IPython/core/magics/basic.py

Lines changed: 64 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -567,35 +567,75 @@ def notebook(self, s):
567567

568568
if(len(hist)<=1):
569569
raise ValueError('History is empty, cannot export')
570+
570571
for session, execution_count, source in hist[:-1]:
571572
cell = v4.new_code_cell(execution_count=execution_count, source=source)
573+
572574
for output in outputs[execution_count]:
573-
for mime_type, data in output.bundle.items():
574-
if output.output_type == "out_stream":
575-
text = data if isinstance(data, list) else [data]
576-
cell.outputs.append(v4.new_output("stream", text=text))
577-
elif output.output_type == "err_stream":
578-
text = data if isinstance(data, list) else [data]
579-
err_output = v4.new_output("stream", text=text)
580-
err_output.name = "stderr"
581-
cell.outputs.append(err_output)
582-
elif output.output_type == "execute_result":
583-
cell.outputs.append(
584-
v4.new_output(
585-
"execute_result",
586-
data={mime_type: data},
587-
execution_count=execution_count,
588-
)
575+
if output.output_type == "out_stream":
576+
text_data = []
577+
for mime_type, data in output.bundle.items():
578+
if isinstance(data, list):
579+
text_data.extend(data)
580+
else:
581+
text_data.append(data)
582+
full_text = "".join(text_data)
583+
# Replace literal \n with actual newlines
584+
full_text = full_text.replace("\\n", "\n")
585+
normalized_text = []
586+
lines = full_text.split("\n")
587+
for i, line in enumerate(lines):
588+
if i < len(lines) - 1:
589+
normalized_text.append(line + "\n")
590+
elif line: # Last line only if it's not empty
591+
normalized_text.append(line + "\n")
592+
cell.outputs.append(v4.new_output("stream", text=normalized_text))
593+
594+
elif output.output_type == "err_stream":
595+
text_data = []
596+
for mime_type, data in output.bundle.items():
597+
if isinstance(data, list):
598+
text_data.extend(data)
599+
else:
600+
text_data.append(data)
601+
full_text = "".join(text_data)
602+
full_text = full_text.replace("\\n", "\n")
603+
normalized_text = []
604+
lines = full_text.split("\n")
605+
for i, line in enumerate(lines):
606+
if i < len(lines) - 1:
607+
normalized_text.append(line + "\n")
608+
elif line:
609+
normalized_text.append(line + "\n")
610+
err_output = v4.new_output("stream", text=normalized_text)
611+
err_output.name = "stderr"
612+
cell.outputs.append(err_output)
613+
614+
elif output.output_type == "execute_result":
615+
data_dict = {}
616+
for mime_type, data in output.bundle.items():
617+
data_dict[mime_type] = data
618+
cell.outputs.append(
619+
v4.new_output(
620+
"execute_result",
621+
data=data_dict,
622+
execution_count=execution_count,
589623
)
590-
elif output.output_type == "display_data":
591-
cell.outputs.append(
592-
v4.new_output(
593-
"display_data",
594-
data={mime_type: data},
595-
)
624+
)
625+
626+
elif output.output_type == "display_data":
627+
# Collect all MIME types for this display_data into a single output
628+
data_dict = {}
629+
for mime_type, data in output.bundle.items():
630+
data_dict[mime_type] = data
631+
cell.outputs.append(
632+
v4.new_output(
633+
"display_data",
634+
data=data_dict,
596635
)
597-
else:
598-
raise ValueError(f"Unknown output type: {output.output_type}")
636+
)
637+
else:
638+
raise ValueError(f"Unknown output type: {output.output_type}")
599639

600640
# Check if this execution_count is in exceptions (current session)
601641
if execution_count in exceptions:

0 commit comments

Comments
 (0)