Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save static vegalite plot to livemd #676

Merged
merged 12 commits into from
Nov 4, 2021
4 changes: 4 additions & 0 deletions lib/livebook/live_markdown/export.ex
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ defmodule Livebook.LiveMarkdown.Export do
[delimiter, "output\n", text, "\n", delimiter]
end

defp render_output({:vega_lite_static, vegalite_data}) do
["```", "vega_lite_static\n", Jason.encode!(vegalite_data), "\n", "```"]
end

defp render_output(_output), do: :ignored

defp get_elixir_cell_code(%{source: source, disable_formatting: true}),
Expand Down
12 changes: 11 additions & 1 deletion lib/livebook/live_markdown/import.ex
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,16 @@ defmodule Livebook.LiveMarkdown.Import do
take_outputs(ast, [output | outputs])
end

defp take_outputs(
[{"pre", _, [{"code", [{"class", "vega_lite_static"}], [output], %{}}], %{}} | ast],
outputs
) do
case Jason.decode(output) do
{:ok, data} -> take_outputs(ast, [{:vega_lite_static, data} | outputs])
jonatanklosko marked this conversation as resolved.
Show resolved Hide resolved
_ -> take_outputs(ast, outputs)
end
end

defp take_outputs(ast, outputs), do: {outputs, ast}

# Builds a notebook from the list of elements obtained in the previous step.
Expand All @@ -198,7 +208,7 @@ defmodule Livebook.LiveMarkdown.Import do
defp build_notebook([{:cell, :elixir, source, outputs} | elems], cells, sections, messages) do
{metadata, elems} = grab_metadata(elems)
attrs = cell_metadata_to_attrs(:elixir, metadata)
outputs = Enum.map(outputs, &{:text, &1})
outputs = Enum.map(outputs, fn output -> if is_tuple(output), do: output, else: {:text, output} end )
jonatanklosko marked this conversation as resolved.
Show resolved Hide resolved
cell = %{Notebook.Cell.new(:elixir) | source: source, outputs: outputs} |> Map.merge(attrs)
build_notebook(elems, [cell | cells], sections, messages)
end
Expand Down