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

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
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
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 record...
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
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
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
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
Codball
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
New
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
New

Other popular topics Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement