From d0056d3035818de9e2f0fd83cb2137d0d8eb4237 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonatan=20K=C5=82osko?= Date: Mon, 24 Jul 2023 15:43:37 +0200 Subject: [PATCH] Automatically setup runtime when creating a new notebook (#2102) --- lib/livebook_web/live/home_live.ex | 2 +- lib/livebook_web/live/open_live.ex | 12 ++++-------- lib/livebook_web/live/session_helpers.ex | 11 +++++++++++ 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/lib/livebook_web/live/home_live.ex b/lib/livebook_web/live/home_live.ex index 20e34c5f35e..fe32d35241c 100644 --- a/lib/livebook_web/live/home_live.ex +++ b/lib/livebook_web/live/home_live.ex @@ -226,7 +226,7 @@ defmodule LivebookWeb.HomeLive do end def handle_params(%{}, _url, socket) when socket.assigns.live_action == :public_new_notebook do - {:noreply, create_session(socket)} + {:noreply, create_session(socket, queue_setup: true)} end def handle_params(_params, _url, socket), do: {:noreply, socket} diff --git a/lib/livebook_web/live/open_live.ex b/lib/livebook_web/live/open_live.ex index d2ccc639cee..f86408a9499 100644 --- a/lib/livebook_web/live/open_live.ex +++ b/lib/livebook_web/live/open_live.ex @@ -34,19 +34,19 @@ defmodule LivebookWeb.OpenLive do ~H""" <:topbar_action> - +
@@ -181,10 +181,6 @@ defmodule LivebookWeb.OpenLive do def handle_params(_params, _url, socket), do: {:noreply, socket} @impl true - def handle_event("new", %{}, socket) do - {:noreply, create_session(socket)} - end - def handle_event("hide_recent_notebook", %{"idx" => idx}, socket) do on_confirm = fn socket -> %{file: file} = Enum.fetch!(socket.assigns.recent_notebooks, idx) diff --git a/lib/livebook_web/live/session_helpers.ex b/lib/livebook_web/live/session_helpers.ex index 8e0f5838866..ebcf33fee1e 100644 --- a/lib/livebook_web/live/session_helpers.ex +++ b/lib/livebook_web/live/session_helpers.ex @@ -11,10 +11,17 @@ defmodule LivebookWeb.SessionHelpers do Creates a new session, redirects on success, puts an error flash message on failure. + ## Options + + * `:queue_setup` - whether to queue the setup cell right after + the session is started. Defaults to `false` + Accepts the same options as `Livebook.Sessions.create_session/1`. """ @spec create_session(Socket.t(), keyword()) :: Socket.t() def create_session(socket, opts \\ []) do + {queue_setup, opts} = Keyword.pop(opts, :queue_setup, false) + # Revert persistence options to default values if there is # no file attached to the new session opts = @@ -26,6 +33,10 @@ defmodule LivebookWeb.SessionHelpers do case Livebook.Sessions.create_session(opts) do {:ok, session} -> + if queue_setup do + Session.queue_cell_evaluation(session.pid, Livebook.Notebook.Cell.setup_cell_id()) + end + redirect_path = session_path(session.id, opts) push_navigate(socket, to: redirect_path)