View Source Kino.Render protocol (Kino v0.12.3)

Protocol defining term formatting in the context of Livebook.

Link to this section Summary

Types

t()

All the types that implement this protocol.

Functions

Transforms the given value into a Livebook-compatible output.

Link to this section Types

@type t() :: term()

All the types that implement this protocol.

Link to this section Functions

@spec to_livebook(t()) :: map()

Transforms the given value into a Livebook-compatible output.

For detailed description of the output format see t:Livebook.Runtime.output/0.

When implementing the protocol for custom struct, you generally do not need to worry about the output format. Instead, you can compose built-in kinos and call Kino.Render.to_livebook/1 to get the expected representation.

For example, if we wanted to render a custom struct as a mermaid graph, we could do this:

defimpl Kino.Render, for: Graph do
  def to_livebook(graph) do
    source = Graph.to_mermaid(graph)
    mermaid_kino = Kino.Mermaid.new(source)
    Kino.Render.to_livebook(mermaid_kino)
  end
end

In many cases it is useful to show the default inspect representation alongside our custom visual representation. For this, we can use tabs:

defimpl Kino.Render, for: Graph do
  def to_livebook(graph) do
    source = Graph.to_mermaid(graph)
    mermaid_kino = Kino.Mermaid.new(source)
    inspect_kino = Kino.Inspect.new(image)
    kino = Kino.Layout.tabs(Graph: mermaid_kino, Raw: inspect_kino)
    Kino.Render.to_livebook(kino)
  end
end