-
Notifications
You must be signed in to change notification settings - Fork 22
/
viewer.ex
38 lines (32 loc) · 893 Bytes
/
viewer.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
defmodule VegaLite.Viewer do
@moduledoc """
Graphics rendering using Erlang's `:wx` bindings.
This method if useful for viewing graphics in scripts
and iex sessions.
"""
@doc """
Renders a `VegaLite` specification in GUI window widget.
Requires Erlang compilation to include the `:wx` module.
"""
@spec show(VegaLite.t()) :: :ok | :error
def show(vl) do
with {:ok, _pid} <- start_wx_viewer(vl), do: :ok
end
@doc """
Same as `show/1`, but blocks until the window widget is closed.
"""
@spec show_and_wait(VegaLite.t()) :: :ok | :error
def show_and_wait(vl) do
with {:ok, pid} <- start_wx_viewer(vl) do
ref = Process.monitor(pid)
receive do
{:DOWN, ^ref, :process, _object, _reason} -> :ok
end
end
end
defp start_wx_viewer(vl) do
vl
|> VegaLite.Export.to_html()
|> VegaLite.WxViewer.start()
end
end