mbklein

mbklein

As I’ve mentioned before, I’m in the middle of setting up a Livebook instance connected to our main application runtime to allow scripting/reuse of administrative tasks we currently do via iex -remsh.

The first step has been a success: I’ve successfully connected a Livebook instance to my running application, and I’m able to do a whole lot of what I set out to do by loading and running livemd files.

Taking things a bit further: By adding kino as a dependency to my application, I am able to render Smart Cells in the Livebook. What I haven’t figured out yet is if there’s a way to configure my application so users can add Smart Cells to a notebook via Livebook’s interactive editor.

For example:

What kind of introspection/query is Livebook using to determine what Smart Cells it can insert, and is there a way I can get my application to provide that info the same way the empty/default Elixir Livebook runtime does?

Showing Posts 1 to 6

jonatanklosko

jonatanklosko

Creator of Livebook

Smart cells usually come from packages and they are registered, here is an example from :kino_vega_lite. So to make the smart cells available in attached runtime you would need to have the corresponding packages as dependencies in your application. That’s why it’s oftentimes a better idea to use the default standalone runtime, connect to the app node and run things on that node using :erpc, while having the ability to install any visualisation packages or smart cells in the notebook, without clobbering app dependencies : )

mbklein

mbklein OP

I’d love to go the RPC route, but I’m trying to figure out how to do that and still have my livebook notebooks written using what looks like vernacular Elixir. Using a simple Ecto example, I want to be able to write

alias MyApp.Data.Schemas.Foo
alias MyApp.Repo

Foo |> Repo.aggregate(:count)

but with :gen_rpc, I’ve only been able to call it as:

alias MyApp.Data.Schemas.Foo
alias MyApp.Repo

:gen_rpc.call(:"myapp@127.0.0.1", Repo, :aggregate, [Foo, :count])

I can’t figure out at all how I would write anything that uses macros.

I really want all the richness and syntax of Elixir with the flexibility of RPC. Do you know if that’s possible?

jonatanklosko

jonatanklosko

Creator of Livebook

Yeah that’s the tradeoff of using RPC calls, if you need to use the remote modules heavily it’s a bit awkward.

I can imagine some helper would make it feel more readable, here’s a quick example:

defmodule DistUtils do
  def connect_remote(node, cookie) do
    Node.set_cookie(node, cookie)
    Node.connect(node)
    :persistent_term.put(:remote_node, node)
  end

  defmacro remotely!(fun) do
    quote bind_quoted: [fun: fun] do
      node = :persistent_term.get(:remote_node)

      myself = self()
      ref = make_ref()

      {pid, monitor_ref} =
        Node.spawn_monitor(node, fn ->
          result = fun.()
          send(myself, {:result, ref, result})
        end)

      receive do
        {:result, ^ref, result} ->
          result

        {:DOWN, ^monitor_ref, :process, ^pid, reason} ->
          raise "failed with reason: #{inspect(reason)}"
      end
    end
  end
end

Then connect with

require DistUtils

DistUtils.connect_remote(node, cookie)

and run code on the remote node:

DistUtils.remotely!(fn ->
  Foo |> Repo.aggregate(:count)
end)

(@josevalim if that solution is not sane let me know, or if you have any other ideas : D)

mbklein

mbklein OP

That’s actually kind of the solution I was thinking of. I’m going to experiment with it a bit unless/until Jose pops in and says it’s a terrible idea. :smile:

mbklein

mbklein OP

This is actually working great. Thanks so much for the example!

Maybe it’s time for me to play with creating an RPC Smart Cell…

jonatanklosko

jonatanklosko

Creator of Livebook

@mbklein there is actually :erpc.call, so there’s no much need for the macro:

:erpc.call(node, fn ->
  Foo.foo()
end)

A smart cell could make it more approachable! The only factor is that the smart cell editor won’t have the regular intellisense (not much issue for modules, since they are likely only available on the remote node anyway, but relevant when using built-in functions).

— All posts loaded —

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
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
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews