wwaldner

wwaldner

Edit JSON in LiveBook

I am trying to figure out a way to load a file in Livebook and then allow editing parts of the data in editor. I want the Livebook to be able to perform subsequent actions on the data. This seems such a simple use case that I can’t believe I am having such a hard time figuring this out.

The basic issue I have is getting data a file/variable into a Kino. The only way I see doing this would be to create a smartcell that would introspect the variables in the livebook, and provide a custom JS editor that would allow editing the data. This is way too much work. I would be happy to just show the data in a JSON format so it can be edited and the allow other subsequent actions on the data.

The sample Livebook tutorials has a MD that gets me so close, but falls short of what I need. The sample allows showing an editor and assigning JSON to a variable in the Livebook. This is exactly what I need to do, however, I would like a fixed variable name and populate the editor with data read from a file. I don’t see a way to get data into the JSON editor from another variable or a file.

This can be 4 different sections/frames of the Livebook

  1. Load CSV file into variable :white_check_mark:
  2. Parse CSV and prepare a couple chunks of JSON :white_check_mark:
  3. Allow editing the JSON(perfer to use code editor, but could build Kino form) :sos:
  4. Perform actions on the JSON :white_check_mark:
# Untitled notebook

```elixir
Mix.install([
  {:jason, "~> 1.4"},
  {:kino, "~> 0.13.1"}
])

Load

var1 = %{name: "fred"} |> Jason.encode!()

Editing

# I want to be able to edit the JSON in code editor
# show me the editor with 
Kino.Text.new(var1)

Actions

# OK, do some actions on var1

Most Liked

acalejos

acalejos

I did something similar in Merquery.

It’s a SmartCell using a JS editor just like you said. It’s using CodeMirror, and even
has prettification for JSON using Prettier.

Check it out and maybe it’ll point you in the right direction.

It will also parse the JSON on the server side, and if it’s valid, decode it into a JSON map.

https://github.com/acalejos/merquery/blob/main/assets/src/components/Editor.vue

Last Post!

jonatanklosko

jonatanklosko

Creator of Livebook

Ah yeah, it is doable by implementing scan_binding for the SmartCell, but for this case it’s not ideal because we would effectively need to send the whole binding across processes, unless we assume the input variable has a fixed name. An input (the textarea) is semantically closer for the use case.

Where Next?

Popular in Questions Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

Other popular topics Top

Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement