livebook/lib/livebook_web/live/kino/vega_lite_live.ex
Jonatan Kłosko ce7adef7e4
Add VegaLite widget integration (#306)
* Add support for LiveWidget.VegaLite

* LiveWidget -> Kino

* Show an error when rendering unsupported Kino widget

* Match on Kino.Widget

* Add catch-all for unknown outputs
2021-05-31 22:48:05 +02:00

32 lines
835 B
Elixir

defmodule LivebookWeb.Kino.VegaLiteLive do
use LivebookWeb, :live_view
@impl true
def mount(_params, %{"pid" => pid, "id" => id}, socket) do
send(pid, {:connect, self()})
{:ok, assign(socket, id: id)}
end
@impl true
def render(assigns) do
~L"""
<div id="vega-lite-<%= @id %>" phx-hook="VegaLite" phx-update="ignore" data-id="<%= @id %>">
</div>
"""
end
@impl true
def handle_info({:connect_reply, %{spec: spec}}, socket) do
{:noreply, push_event(socket, "vega_lite:#{socket.assigns.id}:init", %{"spec" => spec})}
end
def handle_info({:push, %{data: data, dataset: dataset, window: window}}, socket) do
{:noreply,
push_event(socket, "vega_lite:#{socket.assigns.id}:push", %{
"data" => data,
"dataset" => dataset,
"window" => window
})}
end
end