Skip to content

Commit

Permalink
Update outputs format
Browse files Browse the repository at this point in the history
  • Loading branch information
jonatanklosko committed Aug 23, 2023
1 parent 703889a commit 430b8bf
Show file tree
Hide file tree
Showing 18 changed files with 234 additions and 629 deletions.
4 changes: 2 additions & 2 deletions lib/kino.ex
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ defmodule Kino do
def inspect(term, opts \\ []) do
label = if label = opts[:label], do: "#{label}: ", else: ""

{:terminal_text, text, info} = Kino.Output.inspect(term, opts)
output = {:terminal_text, label <> text, info}
output = Kino.Output.inspect(term, opts)
output = update_in(output.text, &(label <> &1))
Kino.Bridge.put_output(output)

term
Expand Down
22 changes: 12 additions & 10 deletions lib/kino/control.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,13 @@ defmodule Kino.Control do
#=> {:hello, %{origin: "client1"}}
"""

defstruct [:attrs]
defstruct [:ref, :destination, :attrs]

@opaque t :: %__MODULE__{attrs: Kino.Output.control_attrs()}
@opaque t :: %__MODULE__{
ref: Kino.Output.ref(),
destination: Process.dest(),
attrs: map()
}

@opaque interval :: {:interval, milliseconds :: non_neg_integer()}

Expand All @@ -46,12 +50,10 @@ defmodule Kino.Control do
ref = Kino.Output.random_ref()
subscription_manager = Kino.SubscriptionManager.cross_node_name()

attrs = Map.merge(attrs, %{ref: ref, destination: subscription_manager})

Kino.Bridge.reference_object(ref, self())
Kino.Bridge.monitor_object(ref, subscription_manager, {:clear_topic, ref})

%__MODULE__{attrs: attrs}
%__MODULE__{ref: ref, destination: subscription_manager, attrs: attrs}
end

@doc """
Expand Down Expand Up @@ -268,7 +270,7 @@ defmodule Kino.Control do
Enum.map(fields, fn {field, input} ->
# Make sure we use this input only in the form and nowhere else
input = Kino.Input.duplicate(input)
{field, input.attrs}
{field, Kino.Render.to_livebook(input)}
end)

submit = Keyword.get(opts, :submit, nil)
Expand Down Expand Up @@ -309,7 +311,7 @@ defmodule Kino.Control do
@spec subscribe(t() | Kino.Input.t(), term()) :: :ok
def subscribe(source, tag)
when is_struct(source, Kino.Control) or is_struct(source, Kino.Input) do
Kino.SubscriptionManager.subscribe(source.attrs.ref, self(), tag)
Kino.SubscriptionManager.subscribe(source.ref, self(), tag)
end

@doc """
Expand All @@ -318,7 +320,7 @@ defmodule Kino.Control do
@spec unsubscribe(t() | Kino.Input.t()) :: :ok
def unsubscribe(source)
when is_struct(source, Kino.Control) or is_struct(source, Kino.Input) do
Kino.SubscriptionManager.unsubscribe(source.attrs.ref, self())
Kino.SubscriptionManager.unsubscribe(source.ref, self())
end

@doc """
Expand Down Expand Up @@ -377,7 +379,7 @@ defmodule Kino.Control do
assert_stream_source!(source)

case source do
%struct{attrs: %{ref: ref}} when struct in [Kino.Control, Kino.Input] ->
%struct{ref: ref} when struct in [Kino.Control, Kino.Input] ->
{[{nil, ref} | tagged_topics], tagged_intervals}

%Kino.JS.Live{ref: ref} ->
Expand Down Expand Up @@ -428,7 +430,7 @@ defmodule Kino.Control do
{tag, source} = entry

case source do
%struct{attrs: %{ref: ref}} when struct in [Kino.Control, Kino.Input] ->
%struct{ref: ref} when struct in [Kino.Control, Kino.Input] ->
{[{tag, ref} | tagged_topics], tagged_intervals}

%Kino.JS.Live{ref: ref} ->
Expand Down
2 changes: 1 addition & 1 deletion lib/kino/frame.ex
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ defmodule Kino.Frame do
defp update_outputs(state, _destination, _update_fun), do: state

defp put_update(destination, ref, outputs, type) do
output = Kino.Output.frame(outputs, %{ref: ref, type: type})
output = %{type: :frame_update, ref: ref, update: {type, outputs}}

case destination do
:default -> Kino.Bridge.put_output(output)
Expand Down
15 changes: 13 additions & 2 deletions lib/kino/image.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,19 @@ defmodule Kino.Image do
The given type be either `:jpeg`/`:jpg`, `:png`, `:gif`, `:svg`, `:pixel`
or a string with image MIME type.
Note that a special `:pixel` format is supported, see `t:Kino.Output.image/0`
for the specification.
## Pixel data
Note that a special `image/x-pixel` MIME type is supported. The
binary consists of the following consecutive parts:
* height - 32 bits (unsigned big-endian integer)
* width - 32 bits (unsigned big-endian integer)
* channels - 8 bits (unsigned integer)
* data - pixel data in HWC order
Pixel data consists of 8-bit unsigned integers. The number of channels
can be either: 1 (grayscale), 2 (grayscale + alpha), 3 (RGB), or 4
(RGB + alpha).
"""
@spec new(binary(), common_image_type() | mime_type()) :: t()
def new(content, type) do
Expand Down
20 changes: 9 additions & 11 deletions lib/kino/input.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,14 @@ defmodule Kino.Input do
more details.
"""

defstruct [:attrs]
defstruct [:ref, :id, :destination, :attrs]

@opaque t :: %__MODULE__{attrs: Kino.Output.input_attrs()}
@opaque t :: %__MODULE__{
ref: Kino.Output.ref(),
id: String.t(),
destination: Process.dest(),
attrs: map()
}

defp new(attrs) do
token = Kino.Bridge.generate_token()
Expand All @@ -37,17 +42,10 @@ defmodule Kino.Input do
ref = Kino.Output.random_ref()
subscription_manager = Kino.SubscriptionManager.cross_node_name()

attrs =
Map.merge(attrs, %{
ref: ref,
id: persistent_id,
destination: subscription_manager
})

Kino.Bridge.reference_object(ref, self())
Kino.Bridge.monitor_object(ref, subscription_manager, {:clear_topic, ref})

%__MODULE__{attrs: attrs}
%__MODULE__{ref: ref, id: persistent_id, destination: subscription_manager, attrs: attrs}
end

@doc false
Expand Down Expand Up @@ -662,7 +660,7 @@ defmodule Kino.Input do
"""
@spec read(t()) :: term()
def read(%Kino.Input{} = input) do
case Kino.Bridge.get_input_value(input.attrs.id) do
case Kino.Bridge.get_input_value(input.id) do
{:ok, value} ->
value

Expand Down
4 changes: 2 additions & 2 deletions lib/kino/js.ex
Original file line number Diff line number Diff line change
Expand Up @@ -491,8 +491,8 @@ defmodule Kino.JS do
end

@doc false
@spec js_info(t()) :: Kino.Output.js_info()
def js_info(%__MODULE__{} = kino) do
@spec output_attrs(t()) :: map()
def output_attrs(%__MODULE__{} = kino) do
%{
js_view: %{
ref: kino.ref,
Expand Down
2 changes: 1 addition & 1 deletion lib/kino/js/data_store.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ defmodule Kino.JS.DataStore do
@doc """
Stores output data under the given ref.
"""
@spec store(Kino.Output.js_output_ref(), term()) :: :ok
@spec store(Kino.Output.ref(), term()) :: :ok
def store(ref, data) do
GenServer.cast(@name, {:store, ref, data})
end
Expand Down
4 changes: 2 additions & 2 deletions lib/kino/js/live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -340,8 +340,8 @@ defmodule Kino.JS.Live do
end

@doc false
@spec js_info(t()) :: Kino.Output.js_info()
def js_info(%__MODULE__{} = kino) do
@spec output_info(t()) :: map()
def output_info(%__MODULE__{} = kino) do
%{
js_view: %{
ref: kino.ref,
Expand Down
Loading

0 comments on commit 430b8bf

Please sign in to comment.