From 44f1b6e0116b5fca2a89e2d62e1bf86f22bf3fc7 Mon Sep 17 00:00:00 2001 From: Cocoa <89497197+cocoa-xu@users.noreply.github.com> Date: Thu, 4 Nov 2021 01:12:41 +0000 Subject: [PATCH 01/12] save static vegalite plot to livemd --- lib/livebook/live_markdown/export.ex | 4 ++++ lib/livebook/live_markdown/import.ex | 15 ++++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/livebook/live_markdown/export.ex b/lib/livebook/live_markdown/export.ex index d236b64974e..a8a0d2c6917 100644 --- a/lib/livebook/live_markdown/export.ex +++ b/lib/livebook/live_markdown/export.ex @@ -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}), diff --git a/lib/livebook/live_markdown/import.ex b/lib/livebook/live_markdown/import.ex index 81ee7312ddd..19c3a8a68eb 100644 --- a/lib/livebook/live_markdown/import.ex +++ b/lib/livebook/live_markdown/import.ex @@ -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]) + _ -> 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. @@ -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 ) cell = %{Notebook.Cell.new(:elixir) | source: source, outputs: outputs} |> Map.merge(attrs) build_notebook(elems, [cell | cells], sections, messages) end @@ -346,6 +356,9 @@ defmodule Livebook.LiveMarkdown.Import do {"reevaluate_automatically", reevaluate_automatically}, attrs -> Map.put(attrs, :reevaluate_automatically, reevaluate_automatically) + {"output_is_vega", output_is_vega}, attrs -> + Map.put(attrs, :output_is_vega, output_is_vega) + _entry, attrs -> attrs end) From 0b1ff0519c49042588aa36bbdf8db5b96c2a9532 Mon Sep 17 00:00:00 2001 From: Cocoa <89497197+cocoa-xu@users.noreply.github.com> Date: Thu, 4 Nov 2021 01:31:28 +0000 Subject: [PATCH 02/12] cleanup debug code --- lib/livebook/live_markdown/import.ex | 3 --- 1 file changed, 3 deletions(-) diff --git a/lib/livebook/live_markdown/import.ex b/lib/livebook/live_markdown/import.ex index 19c3a8a68eb..89e1645f0fa 100644 --- a/lib/livebook/live_markdown/import.ex +++ b/lib/livebook/live_markdown/import.ex @@ -356,9 +356,6 @@ defmodule Livebook.LiveMarkdown.Import do {"reevaluate_automatically", reevaluate_automatically}, attrs -> Map.put(attrs, :reevaluate_automatically, reevaluate_automatically) - {"output_is_vega", output_is_vega}, attrs -> - Map.put(attrs, :output_is_vega, output_is_vega) - _entry, attrs -> attrs end) From c2cf061f88ce32aff98ad567eea860682c07ff0a Mon Sep 17 00:00:00 2001 From: Cocoa <89497197+cocoa-xu@users.noreply.github.com> Date: Thu, 4 Nov 2021 09:52:45 +0000 Subject: [PATCH 03/12] using `vega-lite` as the type in the fenced code block --- lib/livebook/live_markdown/export.ex | 2 +- lib/livebook/live_markdown/import.ex | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/livebook/live_markdown/export.ex b/lib/livebook/live_markdown/export.ex index a8a0d2c6917..cce17ab940c 100644 --- a/lib/livebook/live_markdown/export.ex +++ b/lib/livebook/live_markdown/export.ex @@ -154,7 +154,7 @@ defmodule Livebook.LiveMarkdown.Export do end defp render_output({:vega_lite_static, vegalite_data}) do - ["```", "vega_lite_static\n", Jason.encode!(vegalite_data), "\n", "```"] + ["```", "vega-lite\n", Jason.encode!(vegalite_data), "\n", "```"] end defp render_output(_output), do: :ignored diff --git a/lib/livebook/live_markdown/import.ex b/lib/livebook/live_markdown/import.ex index 89e1645f0fa..bc27b2a01fb 100644 --- a/lib/livebook/live_markdown/import.ex +++ b/lib/livebook/live_markdown/import.ex @@ -188,7 +188,7 @@ defmodule Livebook.LiveMarkdown.Import do end defp take_outputs( - [{"pre", _, [{"code", [{"class", "vega_lite_static"}], [output], %{}}], %{}} | ast], + [{"pre", _, [{"code", [{"class", "vega-lite"}], [output], %{}}], %{}} | ast], outputs ) do case Jason.decode(output) do From f4b3858a2af0c0638918ea3d1662e9f766ef0e73 Mon Sep 17 00:00:00 2001 From: Cocoa <89497197+cocoa-xu@users.noreply.github.com> Date: Thu, 4 Nov 2021 09:56:02 +0000 Subject: [PATCH 04/12] wrap the text output in `{:text, output}` in take_outputs/2 --- lib/livebook/live_markdown/import.ex | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/livebook/live_markdown/import.ex b/lib/livebook/live_markdown/import.ex index bc27b2a01fb..843ac177985 100644 --- a/lib/livebook/live_markdown/import.ex +++ b/lib/livebook/live_markdown/import.ex @@ -184,7 +184,7 @@ defmodule Livebook.LiveMarkdown.Import do [{"pre", _, [{"code", [{"class", "output"}], [output], %{}}], %{}} | ast], outputs ) do - take_outputs(ast, [output | outputs]) + take_outputs(ast, [{:text, output} | outputs]) end defp take_outputs( @@ -208,7 +208,6 @@ 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, fn output -> if is_tuple(output), do: output, else: {:text, output} end ) cell = %{Notebook.Cell.new(:elixir) | source: source, outputs: outputs} |> Map.merge(attrs) build_notebook(elems, [cell | cells], sections, messages) end From 5ba6a327538dc388fc80418e9ff768c0357fc8e5 Mon Sep 17 00:00:00 2001 From: Cocoa <89497197+cocoa-xu@users.noreply.github.com> Date: Thu, 4 Nov 2021 10:39:30 +0000 Subject: [PATCH 05/12] ignore :vega_lite_static when it is empty --- lib/livebook/live_markdown/export.ex | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/livebook/live_markdown/export.ex b/lib/livebook/live_markdown/export.ex index cce17ab940c..041bb132b2c 100644 --- a/lib/livebook/live_markdown/export.ex +++ b/lib/livebook/live_markdown/export.ex @@ -153,6 +153,9 @@ defmodule Livebook.LiveMarkdown.Export do [delimiter, "output\n", text, "\n", delimiter] end + defp render_output({:vega_lite_static, vegalite_data}) when vegalite_data == %{}, + do: :ignored + defp render_output({:vega_lite_static, vegalite_data}) do ["```", "vega-lite\n", Jason.encode!(vegalite_data), "\n", "```"] end From d2049060c1f2dfa0c84245ed98f9a39708d40744 Mon Sep 17 00:00:00 2001 From: Cocoa <89497197+cocoa-xu@users.noreply.github.com> Date: Thu, 4 Nov 2021 10:39:51 +0000 Subject: [PATCH 06/12] add import and export tests --- test/livebook/live_markdown/export_test.exs | 103 ++++++++++++++++++- test/livebook/live_markdown/import_test.exs | 105 ++++++++++++++++++++ 2 files changed, 207 insertions(+), 1 deletion(-) diff --git a/test/livebook/live_markdown/export_test.exs b/test/livebook/live_markdown/export_test.exs index 88bd722d5ae..12a93069a54 100644 --- a/test/livebook/live_markdown/export_test.exs +++ b/test/livebook/live_markdown/export_test.exs @@ -650,7 +650,7 @@ defmodule Livebook.LiveMarkdown.ExportTest do | source: """ IO.puts("hey")\ """, - outputs: [{:vega_lite_static, %{}}, {:table_dynamic, self()}] + outputs: [{:table_dynamic, self()}] } ] } @@ -671,6 +671,107 @@ defmodule Livebook.LiveMarkdown.ExportTest do assert expected_document == document end + + test "includes empty vega_lite_static output" do + notebook = %{ + Notebook.new() + | name: "My Notebook", + sections: [ + %{ + Notebook.Section.new() + | name: "Section 1", + cells: [ + %{ + Notebook.Cell.new(:elixir) + | source: """ + IO.puts("hey")\ + """, + outputs: [{:vega_lite_static, %{}}] + } + ] + } + ] + } + + expected_document = """ + # My Notebook + + ## Section 1 + + ```elixir + IO.puts("hey") + ``` + """ + + document = Export.notebook_to_markdown(notebook, include_outputs: true) + + assert expected_document == document + end + + test "includes non-empty vega_lite_static output" do + notebook = %{ + Notebook.new() + | name: "My Notebook", + sections: [ + %{ + Notebook.Section.new() + | name: "Section 1", + cells: [ + %{ + Notebook.Cell.new(:elixir) + | source: """ + Vl.new(width: 500, height: 200) + |> Vl.data_from_series(in: [1, 2, 3, 4, 5], out: [1, 2, 3, 4, 5]) + |> Vl.mark(:line) + |> Vl.encode_field(:x, "in", type: :quantitative) + |> Vl.encode_field(:y, "out", type: :quantitative)\ + """, + outputs: [{:vega_lite_static, %{ + "$schema" => "https://vega.github.io/schema/vega-lite/v5.json", + "data" => %{ + "values" => [ + %{"in" => 1, "out" => 1}, + %{"in" => 2, "out" => 2}, + %{"in" => 3, "out" => 3}, + %{"in" => 4, "out" => 4}, + %{"in" => 5, "out" => 5}] + }, + "encoding" => %{ + "x" => %{"field" => "in", "type" => "quantitative"}, + "y" => %{"field" => "out", "type" => "quantitative"} + }, + "height" => 200, + "mark" => "line", + "width" => 500 + }}] + } + ] + } + ] + } + + expected_document = """ + # My Notebook + + ## Section 1 + + ```elixir + Vl.new(width: 500, height: 200) + |> Vl.data_from_series(in: [1, 2, 3, 4, 5], out: [1, 2, 3, 4, 5]) + |> Vl.mark(:line) + |> Vl.encode_field(:x, "in", type: :quantitative) + |> Vl.encode_field(:y, "out", type: :quantitative) + ``` + + ```vega-lite + {"$schema":"https://vega.github.io/schema/vega-lite/v5.json","data":{"values":[{"in":1,"out":1},{"in":2,"out":2},{"in":3,"out":3},{"in":4,"out":4},{"in":5,"out":5}]},"encoding":{"x":{"field":"in","type":"quantitative"},"y":{"field":"out","type":"quantitative"}},"height":200,"mark":"line","width":500} + ``` + """ + + document = Export.notebook_to_markdown(notebook, include_outputs: true) + + assert expected_document == document + end end test "includes outputs when notebook has :persist_outputs set" do diff --git a/test/livebook/live_markdown/import_test.exs b/test/livebook/live_markdown/import_test.exs index b803337a4be..1610d131927 100644 --- a/test/livebook/live_markdown/import_test.exs +++ b/test/livebook/live_markdown/import_test.exs @@ -582,6 +582,111 @@ defmodule Livebook.LiveMarkdown.ImportTest do assert %Notebook{name: "My Notebook", autosave_interval_s: 10} = notebook end + test "imports notebook with valid vega-lite output" do + markdown = """ + # My Notebook + + ## Section 1 + + ```elixir + Vl.new(width: 500, height: 200) + |> Vl.data_from_series(in: [1, 2, 3, 4, 5], out: [1, 2, 3, 4, 5]) + |> Vl.mark(:line) + |> Vl.encode_field(:x, "in", type: :quantitative) + |> Vl.encode_field(:y, "out", type: :quantitative) + ``` + + ```vega-lite + {"$schema":"https://vega.github.io/schema/vega-lite/v5.json","data":{"values":[{"in":1,"out":1},{"in":2,"out":2},{"in":3,"out":3},{"in":4,"out":4},{"in":5,"out":5}]},"encoding":{"x":{"field":"in","type":"quantitative"},"y":{"field":"out","type":"quantitative"}},"height":200,"mark":"line","width":500} + ``` + """ + + {notebook, []} = Import.notebook_from_markdown(markdown) + + assert %Notebook{ + name: "My Notebook", + sections: [ + %Notebook.Section{ + name: "Section 1", + cells: [ + %Cell.Elixir{ + source: """ + Vl.new(width: 500, height: 200) + |> Vl.data_from_series(in: [1, 2, 3, 4, 5], out: [1, 2, 3, 4, 5]) + |> Vl.mark(:line) + |> Vl.encode_field(:x, \"in\", type: :quantitative) + |> Vl.encode_field(:y, \"out\", type: :quantitative)\ + """, + outputs: [ + vega_lite_static: %{ + "$schema" => "https://vega.github.io/schema/vega-lite/v5.json", + "data" => %{ + "values" => [ + %{"in" => 1, "out" => 1}, + %{"in" => 2, "out" => 2}, + %{"in" => 3, "out" => 3}, + %{"in" => 4, "out" => 4}, + %{"in" => 5, "out" => 5}] + }, + "encoding" => %{ + "x" => %{"field" => "in", "type" => "quantitative"}, + "y" => %{"field" => "out", "type" => "quantitative"} + }, + "height" => 200, + "mark" => "line", + "width" => 500 + } + ] + } + ] + } + ] + } = notebook + end + + test "imports notebook with invalid vega-lite output" do + markdown = """ + # My Notebook + + ## Section 1 + + ```elixir + Vl.new(width: 500, height: 200) + |> Vl.data_from_series(in: [1, 2, 3, 4, 5], out: [1, 2, 3, 4, 5]) + |> Vl.mark(:line) + |> Vl.encode_field(:x, "in", type: :quantitative) + |> Vl.encode_field(:y, "out", type: :quantitative) + ``` + + ```vega-lite + "$schema":"https://vega.github.io/schema/vega-lite/v5.json","data":{"values":[{"in":1,"out":1},{"in":2,"out":2},{"in":3,"out":3},{"in":4,"out":4},{"in":5,"out":5}]},"encoding":{"x":{"field":"in","type":"quantitative"},"y":{"field":"out","type":"quantitative"}},"height":200,"mark":"line","width":500} + ``` + """ + + {notebook, []} = Import.notebook_from_markdown(markdown) + + assert %Notebook{ + name: "My Notebook", + sections: [ + %Notebook.Section{ + name: "Section 1", + cells: [ + %Cell.Elixir{ + source: """ + Vl.new(width: 500, height: 200) + |> Vl.data_from_series(in: [1, 2, 3, 4, 5], out: [1, 2, 3, 4, 5]) + |> Vl.mark(:line) + |> Vl.encode_field(:x, \"in\", type: :quantitative) + |> Vl.encode_field(:y, \"out\", type: :quantitative)\ + """, + outputs: [] + } + ] + } + ] + } = notebook + end + test "skips invalid input type and returns a message" do markdown = """ # My Notebook From 8a8d2a359873bd4409efc2b24ef948d45c0c4f78 Mon Sep 17 00:00:00 2001 From: Cocoa <89497197+cocoa-xu@users.noreply.github.com> Date: Thu, 4 Nov 2021 10:50:13 +0000 Subject: [PATCH 07/12] using `spec` --- lib/livebook/live_markdown/export.ex | 6 +++--- lib/livebook/live_markdown/import.ex | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/livebook/live_markdown/export.ex b/lib/livebook/live_markdown/export.ex index 041bb132b2c..4c1281aa256 100644 --- a/lib/livebook/live_markdown/export.ex +++ b/lib/livebook/live_markdown/export.ex @@ -153,11 +153,11 @@ defmodule Livebook.LiveMarkdown.Export do [delimiter, "output\n", text, "\n", delimiter] end - defp render_output({:vega_lite_static, vegalite_data}) when vegalite_data == %{}, + defp render_output({:vega_lite_static, spec}) when spec == %{}, do: :ignored - defp render_output({:vega_lite_static, vegalite_data}) do - ["```", "vega-lite\n", Jason.encode!(vegalite_data), "\n", "```"] + defp render_output({:vega_lite_static, spec}) do + ["```", "vega-lite\n", Jason.encode!(spec), "\n", "```"] end defp render_output(_output), do: :ignored diff --git a/lib/livebook/live_markdown/import.ex b/lib/livebook/live_markdown/import.ex index 843ac177985..8ac9431c91e 100644 --- a/lib/livebook/live_markdown/import.ex +++ b/lib/livebook/live_markdown/import.ex @@ -192,7 +192,7 @@ defmodule Livebook.LiveMarkdown.Import do outputs ) do case Jason.decode(output) do - {:ok, data} -> take_outputs(ast, [{:vega_lite_static, data} | outputs]) + {:ok, spec} -> take_outputs(ast, [{:vega_lite_static, spec} | outputs]) _ -> take_outputs(ast, outputs) end end From 09f08936507dfd5e34161b8b1a8ce1d62dd72317 Mon Sep 17 00:00:00 2001 From: Cocoa <89497197+cocoa-xu@users.noreply.github.com> Date: Thu, 4 Nov 2021 10:50:26 +0000 Subject: [PATCH 08/12] format code --- test/livebook/live_markdown/export_test.exs | 40 ++++--- test/livebook/live_markdown/import_test.exs | 115 ++++++++++---------- 2 files changed, 80 insertions(+), 75 deletions(-) diff --git a/test/livebook/live_markdown/export_test.exs b/test/livebook/live_markdown/export_test.exs index 12a93069a54..735b635f61c 100644 --- a/test/livebook/live_markdown/export_test.exs +++ b/test/livebook/live_markdown/export_test.exs @@ -726,24 +726,28 @@ defmodule Livebook.LiveMarkdown.ExportTest do |> Vl.encode_field(:x, "in", type: :quantitative) |> Vl.encode_field(:y, "out", type: :quantitative)\ """, - outputs: [{:vega_lite_static, %{ - "$schema" => "https://vega.github.io/schema/vega-lite/v5.json", - "data" => %{ - "values" => [ - %{"in" => 1, "out" => 1}, - %{"in" => 2, "out" => 2}, - %{"in" => 3, "out" => 3}, - %{"in" => 4, "out" => 4}, - %{"in" => 5, "out" => 5}] - }, - "encoding" => %{ - "x" => %{"field" => "in", "type" => "quantitative"}, - "y" => %{"field" => "out", "type" => "quantitative"} - }, - "height" => 200, - "mark" => "line", - "width" => 500 - }}] + outputs: [ + {:vega_lite_static, + %{ + "$schema" => "https://vega.github.io/schema/vega-lite/v5.json", + "data" => %{ + "values" => [ + %{"in" => 1, "out" => 1}, + %{"in" => 2, "out" => 2}, + %{"in" => 3, "out" => 3}, + %{"in" => 4, "out" => 4}, + %{"in" => 5, "out" => 5} + ] + }, + "encoding" => %{ + "x" => %{"field" => "in", "type" => "quantitative"}, + "y" => %{"field" => "out", "type" => "quantitative"} + }, + "height" => 200, + "mark" => "line", + "width" => 500 + }} + ] } ] } diff --git a/test/livebook/live_markdown/import_test.exs b/test/livebook/live_markdown/import_test.exs index 1610d131927..31c146d072a 100644 --- a/test/livebook/live_markdown/import_test.exs +++ b/test/livebook/live_markdown/import_test.exs @@ -604,44 +604,45 @@ defmodule Livebook.LiveMarkdown.ImportTest do {notebook, []} = Import.notebook_from_markdown(markdown) assert %Notebook{ - name: "My Notebook", - sections: [ - %Notebook.Section{ - name: "Section 1", - cells: [ - %Cell.Elixir{ - source: """ - Vl.new(width: 500, height: 200) - |> Vl.data_from_series(in: [1, 2, 3, 4, 5], out: [1, 2, 3, 4, 5]) - |> Vl.mark(:line) - |> Vl.encode_field(:x, \"in\", type: :quantitative) - |> Vl.encode_field(:y, \"out\", type: :quantitative)\ - """, - outputs: [ - vega_lite_static: %{ - "$schema" => "https://vega.github.io/schema/vega-lite/v5.json", - "data" => %{ - "values" => [ - %{"in" => 1, "out" => 1}, - %{"in" => 2, "out" => 2}, - %{"in" => 3, "out" => 3}, - %{"in" => 4, "out" => 4}, - %{"in" => 5, "out" => 5}] - }, - "encoding" => %{ - "x" => %{"field" => "in", "type" => "quantitative"}, - "y" => %{"field" => "out", "type" => "quantitative"} - }, - "height" => 200, - "mark" => "line", - "width" => 500 - } - ] - } - ] - } - ] - } = notebook + name: "My Notebook", + sections: [ + %Notebook.Section{ + name: "Section 1", + cells: [ + %Cell.Elixir{ + source: """ + Vl.new(width: 500, height: 200) + |> Vl.data_from_series(in: [1, 2, 3, 4, 5], out: [1, 2, 3, 4, 5]) + |> Vl.mark(:line) + |> Vl.encode_field(:x, \"in\", type: :quantitative) + |> Vl.encode_field(:y, \"out\", type: :quantitative)\ + """, + outputs: [ + vega_lite_static: %{ + "$schema" => "https://vega.github.io/schema/vega-lite/v5.json", + "data" => %{ + "values" => [ + %{"in" => 1, "out" => 1}, + %{"in" => 2, "out" => 2}, + %{"in" => 3, "out" => 3}, + %{"in" => 4, "out" => 4}, + %{"in" => 5, "out" => 5} + ] + }, + "encoding" => %{ + "x" => %{"field" => "in", "type" => "quantitative"}, + "y" => %{"field" => "out", "type" => "quantitative"} + }, + "height" => 200, + "mark" => "line", + "width" => 500 + } + ] + } + ] + } + ] + } = notebook end test "imports notebook with invalid vega-lite output" do @@ -666,25 +667,25 @@ defmodule Livebook.LiveMarkdown.ImportTest do {notebook, []} = Import.notebook_from_markdown(markdown) assert %Notebook{ - name: "My Notebook", - sections: [ - %Notebook.Section{ - name: "Section 1", - cells: [ - %Cell.Elixir{ - source: """ - Vl.new(width: 500, height: 200) - |> Vl.data_from_series(in: [1, 2, 3, 4, 5], out: [1, 2, 3, 4, 5]) - |> Vl.mark(:line) - |> Vl.encode_field(:x, \"in\", type: :quantitative) - |> Vl.encode_field(:y, \"out\", type: :quantitative)\ - """, - outputs: [] - } - ] - } - ] - } = notebook + name: "My Notebook", + sections: [ + %Notebook.Section{ + name: "Section 1", + cells: [ + %Cell.Elixir{ + source: """ + Vl.new(width: 500, height: 200) + |> Vl.data_from_series(in: [1, 2, 3, 4, 5], out: [1, 2, 3, 4, 5]) + |> Vl.mark(:line) + |> Vl.encode_field(:x, \"in\", type: :quantitative) + |> Vl.encode_field(:y, \"out\", type: :quantitative)\ + """, + outputs: [] + } + ] + } + ] + } = notebook end test "skips invalid input type and returns a message" do From 057dad75926a4b25209a60ac8f088c413167b6e6 Mon Sep 17 00:00:00 2001 From: Cocoa <89497197+cocoa-xu@users.noreply.github.com> Date: Thu, 4 Nov 2021 11:04:17 +0000 Subject: [PATCH 09/12] keep the test focused --- test/livebook/live_markdown/import_test.exs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/livebook/live_markdown/import_test.exs b/test/livebook/live_markdown/import_test.exs index 31c146d072a..252911a3031 100644 --- a/test/livebook/live_markdown/import_test.exs +++ b/test/livebook/live_markdown/import_test.exs @@ -660,7 +660,7 @@ defmodule Livebook.LiveMarkdown.ImportTest do ``` ```vega-lite - "$schema":"https://vega.github.io/schema/vega-lite/v5.json","data":{"values":[{"in":1,"out":1},{"in":2,"out":2},{"in":3,"out":3},{"in":4,"out":4},{"in":5,"out":5}]},"encoding":{"x":{"field":"in","type":"quantitative"},"y":{"field":"out","type":"quantitative"}},"height":200,"mark":"line","width":500} + not_a_json ``` """ From 8ff8d246992499af163be74ce3a47d3f633883d7 Mon Sep 17 00:00:00 2001 From: Cocoa <89497197+cocoa-xu@users.noreply.github.com> Date: Thu, 4 Nov 2021 11:10:03 +0000 Subject: [PATCH 10/12] improve tests for not including outputs --- test/livebook/live_markdown/export_test.exs | 44 ++++----------------- 1 file changed, 7 insertions(+), 37 deletions(-) diff --git a/test/livebook/live_markdown/export_test.exs b/test/livebook/live_markdown/export_test.exs index 735b635f61c..79aac52c2e1 100644 --- a/test/livebook/live_markdown/export_test.exs +++ b/test/livebook/live_markdown/export_test.exs @@ -530,7 +530,13 @@ defmodule Livebook.LiveMarkdown.ExportTest do | source: """ IO.puts("hey")\ """, - outputs: ["hey"] + outputs: [ + "hey", + {:vega_lite_static, + %{ + "$schema" => "https://vega.github.io/schema/vega-lite/v5.json" + }} + ] } ] } @@ -672,42 +678,6 @@ defmodule Livebook.LiveMarkdown.ExportTest do assert expected_document == document end - test "includes empty vega_lite_static output" do - notebook = %{ - Notebook.new() - | name: "My Notebook", - sections: [ - %{ - Notebook.Section.new() - | name: "Section 1", - cells: [ - %{ - Notebook.Cell.new(:elixir) - | source: """ - IO.puts("hey")\ - """, - outputs: [{:vega_lite_static, %{}}] - } - ] - } - ] - } - - expected_document = """ - # My Notebook - - ## Section 1 - - ```elixir - IO.puts("hey") - ``` - """ - - document = Export.notebook_to_markdown(notebook, include_outputs: true) - - assert expected_document == document - end - test "includes non-empty vega_lite_static output" do notebook = %{ Notebook.new() From bebdd2d71ec30dd338ec015470d7d7bf1d162156 Mon Sep 17 00:00:00 2001 From: Cocoa <89497197+cocoa-xu@users.noreply.github.com> Date: Thu, 4 Nov 2021 11:10:44 +0000 Subject: [PATCH 11/12] always dump vage_lite spec --- lib/livebook/live_markdown/export.ex | 3 --- 1 file changed, 3 deletions(-) diff --git a/lib/livebook/live_markdown/export.ex b/lib/livebook/live_markdown/export.ex index 4c1281aa256..660c9a0ff7d 100644 --- a/lib/livebook/live_markdown/export.ex +++ b/lib/livebook/live_markdown/export.ex @@ -153,9 +153,6 @@ defmodule Livebook.LiveMarkdown.Export do [delimiter, "output\n", text, "\n", delimiter] end - defp render_output({:vega_lite_static, spec}) when spec == %{}, - do: :ignored - defp render_output({:vega_lite_static, spec}) do ["```", "vega-lite\n", Jason.encode!(spec), "\n", "```"] end From e42c90c37d691205a9f431df1627a811a8f1be91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonatan=20K=C5=82osko?= Date: Thu, 4 Nov 2021 12:15:07 +0100 Subject: [PATCH 12/12] Apply suggestions from code review --- test/livebook/live_markdown/export_test.exs | 2 +- test/livebook/live_markdown/import_test.exs | 12 ++---------- 2 files changed, 3 insertions(+), 11 deletions(-) diff --git a/test/livebook/live_markdown/export_test.exs b/test/livebook/live_markdown/export_test.exs index 79aac52c2e1..7bf2b78ce90 100644 --- a/test/livebook/live_markdown/export_test.exs +++ b/test/livebook/live_markdown/export_test.exs @@ -678,7 +678,7 @@ defmodule Livebook.LiveMarkdown.ExportTest do assert expected_document == document end - test "includes non-empty vega_lite_static output" do + test "includes vega_lite_static output" do notebook = %{ Notebook.new() | name: "My Notebook", diff --git a/test/livebook/live_markdown/import_test.exs b/test/livebook/live_markdown/import_test.exs index 252911a3031..d1a75aea246 100644 --- a/test/livebook/live_markdown/import_test.exs +++ b/test/livebook/live_markdown/import_test.exs @@ -652,11 +652,7 @@ defmodule Livebook.LiveMarkdown.ImportTest do ## Section 1 ```elixir - Vl.new(width: 500, height: 200) - |> Vl.data_from_series(in: [1, 2, 3, 4, 5], out: [1, 2, 3, 4, 5]) - |> Vl.mark(:line) - |> Vl.encode_field(:x, "in", type: :quantitative) - |> Vl.encode_field(:y, "out", type: :quantitative) + :ok ``` ```vega-lite @@ -674,11 +670,7 @@ defmodule Livebook.LiveMarkdown.ImportTest do cells: [ %Cell.Elixir{ source: """ - Vl.new(width: 500, height: 200) - |> Vl.data_from_series(in: [1, 2, 3, 4, 5], out: [1, 2, 3, 4, 5]) - |> Vl.mark(:line) - |> Vl.encode_field(:x, \"in\", type: :quantitative) - |> Vl.encode_field(:y, \"out\", type: :quantitative)\ + :ok\ """, outputs: [] }