Add bin/notebook script to start Phoenix + Livebook together

Starts Phoenix with a named node and Livebook attached to it on
port 8081. Single command, Ctrl-C stops both.
This commit is contained in:
Graham McIntire 2026-04-07 08:54:50 -05:00
parent 44f0ff26e5
commit 548e277bec
2 changed files with 44 additions and 16 deletions

40
bin/notebook Executable file
View file

@ -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

View file

@ -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