cd-slash

cd-slash

I have a livebook cell that starts with a single input, then appends additional select inputs recursively based on the input of the previous select box. It’s a self-referencing table, so it’s using parent / child relations to populate the next box. I’m pretty surprised that this works as I thought the recursive Kino.listen/2 calls would break something, but pleasingly it works pretty well.

The obvious problem is that inputs are only appended, not removed. If an earlier selection in the list is changed, another box is simply appended on the end. I can’t find any obvious way to track inputs appended to a frame so I can remove them without clearing the whole form - is there any way to track these? I have also tried using a Kino form or just adding raw inputs but neither seems to allow an append like the frame does.

defmodule AreaInputs do
  import Ecto.Query

  defp parent_id_filter(queryable, parent_id) do
    case parent_id do
      nil -> queryable |> where([a], is_nil(a.parent_id))
      _ -> queryable |> where([a], a.parent_id == ^parent_id)
    end
  end

  def get_areas(parent_id) do
    Area
    |> parent_id_filter(parent_id)
    |> order_by([a], a.name)
    |> AreaRepo.all()
    |> Enum.map(fn area -> {area.id, area.name} end)
  end

  def append_select_and_listen(frame, event) do
    new_input = Kino.Input.select("", AreaInputs.get_areas(event.value))
    Kino.Frame.append(frame, new_input)

    Kino.listen(new_input, fn event_2 ->
      append_select_and_listen(frame, event_2)
    end)
  end
end

frame = Kino.Frame.new() |> Kino.render()
initial_input = Kino.Input.select("", AreaInputs.get_areas(nil))
Kino.Frame.render(frame, initial_input)

Kino.listen(initial_input, fn event ->
  AreaInputs.append_select_and_listen(frame, event)
end)

Showing Posts 1 to 2

jonatanklosko

jonatanklosko

Creator of Livebook

Hey @cd-slash : ) For this case I think you need a process to manage the frame and inputs, so you can re-render all the content. So more specifically, you could have a GenServer that has the frame and you would subscribe it to input events. Then, when an even is received, you re-render into the frame new set of inputs accordingly.

In the future we may have an abstraction to streamline this. Currently there is a proof of concept of a wizard in Kino Add Kino.Wizard POC by josevalim · Pull Request #426 · livebook-dev/kino · GitHub.

hugobarauna

hugobarauna

Livebook Core Team
— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

jeffyzooom
Hi all, I’ve been working on RustyCSV, a CSV parser and encoder for Elixir backed by Rust NIFs. Version 0.4.0 is now available. The goa...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews