Skip to content

Commit

Permalink
Allow configuring vega embed actions
Browse files Browse the repository at this point in the history
  • Loading branch information
pnezis committed Jun 27, 2024
1 parent 874a5d1 commit aa43048
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 7 deletions.
7 changes: 6 additions & 1 deletion assets/vega_lite/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,12 @@ export function init(ctx, data) {
let theme = config.theme === "livebook" ? livebookTheme : {};

const options = {
actions: { export: true, source: false, compiled: false, editor: false },
actions: {
export: config.export,
source: config.view_source,
compiled: config.view_compiled_vega,
editor: config.open_vega_editor
},
config: theme,
};

Expand Down
40 changes: 38 additions & 2 deletions lib/kino/vega_lite.ex
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,49 @@ defmodule Kino.VegaLite do
* `: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`.
* `:export` - whether the **Export as SVG/PNG** actions will be
enabled on the chart widget. Defaults to `true`.
* `:view_source` - whether the **View Source** action will be enabled
on the chart widget. Defaults to `false`.
* `:view_compiled_vega` - whether the **View Compiled Vega** action
will be enabled on the chart widget. Defaults to `false`.
* `:open_vega_editor` - whether the **Open in Vega Editor** action
will be enabled on the chart widget. Defaults to `false`.
"""
@spec configure(keyword()) :: :ok
def configure(opts) do
opts = Keyword.validate!(opts, theme: :livebook)
opts =
Keyword.validate!(opts,
theme: :livebook,
export: true,
view_source: false,
view_compiled_vega: false,
open_vega_editor: false
)

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

for field <- [:export, :view_source, :view_compiled_vega, :open_vega_editor] do
validate_boolean!(opts, field)
end

Application.put_all_env(kino_vega_lite: opts)
end

defp validate_boolean!(opts, field) do
unless is_boolean(opts[field]) do
raise ArgumentError,
"expected #{inspect(field)} to be a boolean, got: #{inspect(opts[field])}"
end
end

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

defp config do
default_config = [theme: :livebook]
default_config = [
theme: :livebook,
export: true,
view_source: false,
view_compiled_vega: false,
open_vega_editor: false
]

default_config
|> Keyword.merge(Application.get_all_env(:kino_vega_lite))
Expand Down
38 changes: 34 additions & 4 deletions test/kino/vega_lite_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -99,19 +99,49 @@ defmodule Kino.VegaLiteTest do
"expected :theme to be either :livebook or nil, got: :invalid",
fn -> Kino.VegaLite.configure(theme: :invalid) end

# with default theme
# with invalid value for boolean options
for field <- [:export, :view_source, :view_compiled_vega, :open_vega_editor] do
message = "expected #{inspect(field)} to be a boolean, got: nil"
assert_raise ArgumentError, message, fn -> Kino.VegaLite.configure([{field, nil}]) end
end

# with default values
kino = start_kino()

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

assert %{
config: %{
theme: :livebook,
export: true,
view_source: false,
view_compiled_vega: false,
open_vega_editor: false
}
} = data

# with empty theme
Kino.VegaLite.configure(theme: nil)
Kino.VegaLite.configure(
theme: nil,
export: false,
view_source: true,
view_compiled_vega: true,
open_vega_editor: true
)

kino = start_kino()

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

assert %{
config: %{
theme: nil,
export: false,
view_source: true,
view_compiled_vega: true,
open_vega_editor: true
}
} = data
end

defp start_kino() do
Expand Down

0 comments on commit aa43048

Please sign in to comment.