mirror of
https://github.com/livebook-dev/livebook.git
synced 2025-10-10 13:38:09 +08:00
Include errors in exported notebook outputs (#3009)
This commit is contained in:
parent
02e0d3ccf9
commit
65c05bbde9
2 changed files with 64 additions and 6 deletions
|
@ -238,12 +238,11 @@ defmodule Livebook.LiveMarkdown.Export do
|
||||||
end
|
end
|
||||||
|
|
||||||
defp render_output(%{type: :terminal_text, text: text}, _ctx) do
|
defp render_output(%{type: :terminal_text, text: text}, _ctx) do
|
||||||
text = String.replace_suffix(text, "\n", "")
|
render_text_output(text)
|
||||||
delimiter = MarkdownHelpers.code_block_delimiter(text)
|
end
|
||||||
text = strip_ansi(text)
|
|
||||||
|
|
||||||
[delimiter, "\n", text, "\n", delimiter]
|
defp render_output(%{type: :error, message: message}, _ctx) do
|
||||||
|> prepend_metadata(%{output: true})
|
render_text_output(message)
|
||||||
end
|
end
|
||||||
|
|
||||||
defp render_output(%{type: :js, js_view: %{ref: ref}}, ctx) do
|
defp render_output(%{type: :js, js_view: %{ref: ref}}, ctx) do
|
||||||
|
@ -279,6 +278,14 @@ defmodule Livebook.LiveMarkdown.Export do
|
||||||
|
|
||||||
defp render_output(_output, _ctx), do: :ignored
|
defp render_output(_output, _ctx), do: :ignored
|
||||||
|
|
||||||
|
defp render_text_output(text) do
|
||||||
|
text = text |> strip_ansi() |> String.replace_suffix("\n", "")
|
||||||
|
delimiter = MarkdownHelpers.code_block_delimiter(text)
|
||||||
|
|
||||||
|
[delimiter, "\n", text, "\n", delimiter]
|
||||||
|
|> prepend_metadata(%{output: true})
|
||||||
|
end
|
||||||
|
|
||||||
defp encode_js_data(data) when is_binary(data), do: {:ok, data}
|
defp encode_js_data(data) when is_binary(data), do: {:ok, data}
|
||||||
|
|
||||||
defp encode_js_data(data) do
|
defp encode_js_data(data) do
|
||||||
|
@ -349,7 +356,7 @@ defmodule Livebook.LiveMarkdown.Export do
|
||||||
string
|
string
|
||||||
|> Livebook.Utils.ANSI.parse_ansi_string()
|
|> Livebook.Utils.ANSI.parse_ansi_string()
|
||||||
|> elem(0)
|
|> elem(0)
|
||||||
|> Enum.map(fn {_modifiers, string} -> string end)
|
|> Enum.map_join(fn {_modifiers, string} -> string end)
|
||||||
end
|
end
|
||||||
|
|
||||||
defp render_notebook_footer(_notebook, _notebook_source, _include_stamp? = false), do: {[], []}
|
defp render_notebook_footer(_notebook, _notebook_source, _include_stamp? = false), do: {[], []}
|
||||||
|
|
|
@ -947,6 +947,57 @@ defmodule Livebook.LiveMarkdown.ExportTest do
|
||||||
assert expected_document == document
|
assert expected_document == document
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "includes error outputs" do
|
||||||
|
notebook = %{
|
||||||
|
Notebook.new()
|
||||||
|
| name: "My Notebook",
|
||||||
|
sections: [
|
||||||
|
%{
|
||||||
|
Notebook.Section.new()
|
||||||
|
| name: "Section 1",
|
||||||
|
cells: [
|
||||||
|
%{
|
||||||
|
Notebook.Cell.new(:code)
|
||||||
|
| source: """
|
||||||
|
raise "hello"\
|
||||||
|
""",
|
||||||
|
outputs: [
|
||||||
|
{0,
|
||||||
|
%{
|
||||||
|
type: :error,
|
||||||
|
message:
|
||||||
|
"\e[31m** (RuntimeError) hello\e[0m\n\e[31m #cell:tlbdimkdsfldvwge:1: (file)\n\e[0m",
|
||||||
|
context: nil
|
||||||
|
}}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
expected_document = """
|
||||||
|
# My Notebook
|
||||||
|
|
||||||
|
## Section 1
|
||||||
|
|
||||||
|
```elixir
|
||||||
|
raise "hello"
|
||||||
|
```
|
||||||
|
|
||||||
|
<!-- livebook:{"output":true} -->
|
||||||
|
|
||||||
|
```
|
||||||
|
** (RuntimeError) hello
|
||||||
|
#cell:tlbdimkdsfldvwge:1: (file)
|
||||||
|
```
|
||||||
|
"""
|
||||||
|
|
||||||
|
{document, []} = Export.notebook_to_livemd(notebook, include_outputs: true)
|
||||||
|
|
||||||
|
assert expected_document == document
|
||||||
|
end
|
||||||
|
|
||||||
test "includes outputs when notebook has :persist_outputs set" do
|
test "includes outputs when notebook has :persist_outputs set" do
|
||||||
notebook = %{
|
notebook = %{
|
||||||
Notebook.new()
|
Notebook.new()
|
||||||
|
|
Loading…
Add table
Reference in a new issue