-
Notifications
You must be signed in to change notification settings - Fork 65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow Kino.JS.Live outputs to be exported #321
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -202,7 +202,7 @@ defmodule Kino.JS do | |
|
||
defstruct [:module, :ref, :export] | ||
|
||
@opaque t :: %__MODULE__{module: module(), ref: Kino.Output.ref(), export: map()} | ||
@opaque t :: %__MODULE__{module: module(), ref: Kino.Output.ref(), export: boolean()} | ||
|
||
defmacro __using__(opts) do | ||
quote location: :keep, bind_quoted: [opts: opts] do | ||
|
@@ -435,19 +435,23 @@ defmodule Kino.JS do | |
|
||
## Options | ||
|
||
* `:export_info_string` - used as the info string for the Markdown | ||
code block where output data is persisted | ||
|
||
* `:export_key` - in case the data is a map and only a specific part | ||
should be exported | ||
* `:export` - a function called to export the given kino to Markdown. | ||
See the "Export" section below | ||
|
||
## Export | ||
|
||
The output can optionally be exported in notebook source by specifying | ||
`:export_info_string`. For example: | ||
an `:export` function. The function receives `data` as an argument | ||
and should return a tuple `{info_string, payload}`. `info_string` | ||
is used to annotate the Markdown code block where the output is | ||
persisted. `payload` is the value persisted in the code block. The | ||
value is automatically serialized to JSON, unless it is already a | ||
string. | ||
|
||
For example: | ||
|
||
data = "graph TD;A-->B;" | ||
Kino.JS.new(__MODULE__, data, export_info_string: "mermaid") | ||
Kino.JS.new(__MODULE__, data, export: fn data -> {"mermaid", data} end) | ||
|
||
Would be rendered as the following Live Markdown: | ||
|
||
|
@@ -457,12 +461,22 @@ defmodule Kino.JS do | |
``` | ||
```` | ||
|
||
Non-binary data is automatically serialized to JSON. | ||
> #### Export function {: .info} | ||
> | ||
> You should prefer to use the `data` argument for computing the | ||
> export payload. However, if it cannot be inferred from `data`, | ||
> you should just reference the original value. Do not put additional | ||
> fields in `data`, just to use it for export. | ||
""" | ||
@spec new(module(), term(), keyword()) :: t() | ||
def new(module, data, opts \\ []) do | ||
# TODO: remove the old export options in Kino v0.14.0 | ||
export = | ||
if info_string = opts[:export_info_string] do | ||
IO.warn( | ||
"passing :export_info_string to Kino.JS.new/3 is deprecated. Specify an :export function instead" | ||
) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will we have to update many kinos or only Kino.VegaLite? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The only other one I found is kino_wardely. |
||
export_key = opts[:export_key] | ||
|
||
if export_key do | ||
|
@@ -477,17 +491,19 @@ defmodule Kino.JS do | |
end | ||
end | ||
|
||
%{info_string: info_string, key: export_key} | ||
fn data -> {info_string, data[export_key]} end | ||
end | ||
|
||
export = export || opts[:export] | ||
|
||
ref = Kino.Output.random_ref() | ||
|
||
Kino.JS.DataStore.store(ref, data) | ||
Kino.JS.DataStore.store(ref, data, export) | ||
|
||
Kino.Bridge.reference_object(ref, self()) | ||
Kino.Bridge.monitor_object(ref, Kino.JS.DataStore, {:remove, ref}) | ||
|
||
%__MODULE__{module: module, ref: ref, export: export} | ||
%__MODULE__{module: module, ref: ref, export: export != nil} | ||
end | ||
|
||
@doc false | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -182,11 +182,16 @@ defmodule Kino.JS.Live do | |
end | ||
''' | ||
|
||
defstruct [:module, :pid, :ref] | ||
defstruct [:module, :pid, :ref, :export] | ||
|
||
alias Kino.JS.Live.Context | ||
|
||
@opaque t :: %__MODULE__{module: module(), pid: pid(), ref: Kino.Output.ref()} | ||
@opaque t :: %__MODULE__{ | ||
module: module(), | ||
pid: pid(), | ||
ref: Kino.Output.ref(), | ||
export: boolean() | ||
} | ||
|
||
@type payload :: term() | {:binary, info :: term(), binary()} | ||
|
||
|
@@ -322,16 +327,25 @@ defmodule Kino.JS.Live do | |
|
||
The given `init_arg` is passed to the `init/2` callback when | ||
the underlying kino process is started. | ||
|
||
## Options | ||
|
||
* `:export` - a function called to export the given kino to Markdown. | ||
This works the same as `Kino.JS.new/3`, except the function | ||
receives `t:Kino.JS.Live.Context.t/0` as an argument | ||
Comment on lines
+331
to
+335
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @josevalim I made this an option instead of a callback, for two reasons:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, I'm not sure if should actually be passing the state to that function. If we do that, then people may make the export reflect the state (e.g. page when paginating the table). It is undesired, because (a) we wouldn't know that we should re-save when the page changes, so it would end up pretty arbitrary (b) it wouldn't reflect the actual value, that is, if the cell returns |
||
|
||
""" | ||
@spec new(module(), term()) :: t() | ||
def new(module, init_arg) do | ||
@spec new(module(), term(), keyword()) :: t() | ||
def new(module, init_arg, opts \\ []) do | ||
export = opts[:export] | ||
|
||
ref = Kino.Output.random_ref() | ||
|
||
case Kino.start_child({Kino.JS.Live.Server, {module, init_arg, ref}}) do | ||
case Kino.start_child({Kino.JS.Live.Server, {module, init_arg, ref, export}}) do | ||
{:ok, pid} -> | ||
subscription_manager = Kino.SubscriptionManager.cross_node_name() | ||
Kino.Bridge.monitor_object(pid, subscription_manager, {:clear_topic, ref}) | ||
%__MODULE__{module: module, pid: pid, ref: ref} | ||
%__MODULE__{module: module, pid: pid, ref: ref, export: export != nil} | ||
|
||
{:error, reason} -> | ||
raise ArgumentError, | ||
|
@@ -348,7 +362,7 @@ defmodule Kino.JS.Live do | |
pid: kino.pid, | ||
assets: kino.module.__assets_info__() | ||
}, | ||
export: nil | ||
export: kino.export | ||
} | ||
end | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.