mbklein

mbklein

Using Smart Cells in an attached runtime

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?

Marked As Solved

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)

Also Liked

mbklein

mbklein

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).

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 : )

Where Next?

Popular in Questions Top

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
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
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

Other popular topics Top

Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52341 488
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 126479 1222
New

We're in Beta

About us Mission Statement