diff --git a/bin/notebook b/bin/notebook new file mode 100755 index 00000000..8dc37320 --- /dev/null +++ b/bin/notebook @@ -0,0 +1,40 @@ +#!/bin/bash +# Start Phoenix + Livebook together. Livebook attaches to the Phoenix node +# and gets full access to Repo, Scorer, Weather, etc. +# +# Usage: bin/notebook +# Then open http://localhost:8081 + +set -e + +COOKIE="microwaveprop_nb" +NODE="prop@$(hostname -s)" +PORT="${LIVEBOOK_PORT:-8081}" +NOTEBOOK="notebooks/algorithm_analysis.livemd" + +echo "Starting Phoenix on :4000 and Livebook on :${PORT}..." +echo "Livebook will be at http://localhost:${PORT}" +echo "" + +# Start Phoenix in background with a node name +elixir --sname prop --cookie "$COOKIE" -S mix phx.server & +PHOENIX_PID=$! + +# Wait for the node to come up +sleep 3 + +# Start Livebook attached to the Phoenix node +LIVEBOOK_HOME="$(pwd)/notebooks" \ + livebook server \ + --port "$PORT" \ + --default-runtime "attached:${NODE}:${COOKIE}" \ + --open "$NOTEBOOK" & +LIVEBOOK_PID=$! + +# Trap ctrl-c to kill both +trap "kill $PHOENIX_PID $LIVEBOOK_PID 2>/dev/null; exit" INT TERM + +echo "Phoenix PID: $PHOENIX_PID, Livebook PID: $LIVEBOOK_PID" +echo "Press Ctrl-C to stop both." + +wait diff --git a/lib/mix/tasks/notebook.ex b/lib/mix/tasks/notebook.ex index 1770b976..1f90eb91 100644 --- a/lib/mix/tasks/notebook.ex +++ b/lib/mix/tasks/notebook.ex @@ -1,27 +1,15 @@ defmodule Mix.Tasks.Notebook do - @shortdoc "Start Phoenix + Livebook at localhost:8081/notebooks/algorithm_analysis.livemd" + @shortdoc "Print instructions for starting the analysis notebook" @moduledoc false use Mix.Task @impl Mix.Task def run(_args) do - hostname = :inet.gethostname() |> elem(1) |> List.to_string() - cookie = "microwaveprop_notebook" - node = "prop@#{hostname}" - IO.puts(""" - Start Phoenix with a node name first: + Run bin/notebook to start Phoenix + Livebook together. - iex --sname prop --cookie #{cookie} -S mix phx.server - - Then in another terminal: - - LIVEBOOK_NODE=#{node} LIVEBOOK_COOKIE=#{cookie} livebook server \\ - --port 8081 \\ - --default-runtime attached:#{node}:#{cookie} \\ - --open notebooks/algorithm_analysis.livemd - - Livebook will be at http://localhost:8081 + Livebook will be at http://localhost:8081 with full access to + Repo, Scorer, BandConfig, Weather, and all app modules. """) end end