Skip to content

Instantly share code, notes, and snippets.

@hugobarauna
Created June 13, 2024 18:03
Show Gist options
  • Save hugobarauna/018e1e487499249090bb30ed985eb764 to your computer and use it in GitHub Desktop.
Save hugobarauna/018e1e487499249090bb30ed985eb764 to your computer and use it in GitHub Desktop.
Livebook app with Kino widgets demo

Kino UI widgets

Mix.install([
  {:kino, "~> 0.12.3"}
])

Kino.Input

defmodule Helpers do
  def display_widget_demo("select") do
    code =
      quote do
        Kino.Input.select("select", en: "English", fr: "Français")
      end

    markdown = build_code_sample("select", code)

    render_input_demo(markdown, code)
  end

  def display_widget_demo(function_name) do
    code =
      quote do
        Kino.Input.unquote(String.to_atom(function_name))(unquote(function_name))
      end

    markdown = build_code_sample(function_name, code)

    render_input_demo(markdown, code)
  end

  defp build_code_sample(title, code) do
    Kino.Markdown.new("""
    ### #{title}

    ```elixir
    #{Macro.to_string(code)}
    ```
    """)
  end

  defp render_input_demo(code_sample_markdown, code) do
    {result, _bindings} = Code.eval_quoted(code)

    Kino.render(
      Kino.Layout.grid([code_sample_markdown, result, Kino.HTML.new("<br/>")], boxed: true)
    )

    Kino.render(Kino.HTML.new("<br/>"))
  end
end
import Helpers
Kino.Markdown.new("## Kino.input")
kino_input_functions = ~w(
audio
checkbox
color
file
image
number
password
range
select
text
textarea
url
utc_datetime
utc_time
)

Enum.each(kino_input_functions, fn function_name ->
  display_widget_demo(function_name)
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment