Skip to content

Commit

Permalink
Allow disabling default livebook theme (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
pnezis authored Jun 27, 2024
1 parent dab662c commit 68f8c6d
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 5 deletions.
6 changes: 4 additions & 2 deletions assets/vega_lite/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,17 @@ export function init(ctx, data) {
"https://fonts.googleapis.com/css2?family=Inter:wght@400;500&display=swap"
);

const { spec, datasets } = data;
const { spec, datasets, config } = data;

if (!spec.data) {
spec.data = { values: [] };
}

let theme = config.theme === "livebook" ? livebookTheme : {};

const options = {
actions: { export: true, source: false, compiled: false, editor: false },
config: livebookTheme,
config: theme,
};

vegaEmbed(ctx.root, spec, options)
Expand Down
37 changes: 34 additions & 3 deletions lib/kino/vega_lite.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ defmodule Kino.VegaLite do
def static(vl) when is_struct(vl, VegaLite) do
data = %{
spec: VegaLite.to_spec(vl),
datasets: []
datasets: [],
config: config()
}

Kino.JS.new(__MODULE__, data,
Expand All @@ -51,6 +52,27 @@ defmodule Kino.VegaLite do
)
end

@doc """
Applies global configuration options for the VegaLite kinos.
## Options
* `:theme` - the theme to be applied on the rendered VegaLite
charts. Currently the only supported theme is `:livebook`. If
set to `nil`, no theme is applied. Defaults to `:livebook`.
"""
@spec configure(keyword()) :: :ok
def configure(opts) do
opts = Keyword.validate!(opts, theme: :livebook)

unless opts[:theme] in [nil, :livebook] do
raise ArgumentError,
"expected :theme to be either :livebook or nil, got: #{inspect(opts[:theme])}"
end

Application.put_all_env(kino_vega_lite: opts)
end

@doc """
Renders and returns a new kino with the given VegaLite definition.
Expand Down Expand Up @@ -164,7 +186,7 @@ defmodule Kino.VegaLite do

@impl true
def init(vl, ctx) do
{:ok, assign(ctx, vl: vl, datasets: %{})}
{:ok, assign(ctx, vl: vl, datasets: %{}, config: config())}
end

@compile {:no_warn_undefined, {VegaLite, :to_spec, 1}}
Expand All @@ -173,7 +195,8 @@ defmodule Kino.VegaLite do
def handle_connect(ctx) do
data = %{
spec: VegaLite.to_spec(ctx.assigns.vl),
datasets: for({dataset, data} <- ctx.assigns.datasets, do: [dataset, data])
datasets: for({dataset, data} <- ctx.assigns.datasets, do: [dataset, data]),
config: ctx.assigns.config
}

{:ok, data, ctx}
Expand Down Expand Up @@ -231,4 +254,12 @@ defmodule Kino.VegaLite do
:ok
end
end

defp config do
default_config = [theme: :livebook]

default_config
|> Keyword.merge(Application.get_all_env(:kino_vega_lite))
|> Map.new()
end
end
21 changes: 21 additions & 0 deletions test/kino/vega_lite_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,27 @@ defmodule Kino.VegaLiteTest do
assert_broadcast_event(kino, "push", %{data: [], dataset: nil, window: 0})
end

test "configure/2" do
# with invalid theme
assert_raise ArgumentError,
"expected :theme to be either :livebook or nil, got: :invalid",
fn -> Kino.VegaLite.configure(theme: :invalid) end

# with default theme
kino = start_kino()

data = connect(kino)
assert %{config: %{theme: :livebook}} = data

# with empty theme
Kino.VegaLite.configure(theme: nil)

kino = start_kino()

data = connect(kino)
assert %{config: %{theme: nil}} = data
end

defp start_kino() do
Vl.new()
|> Vl.mark(:point)
Expand Down

0 comments on commit 68f8c6d

Please sign in to comment.