Skip to content

Commit

Permalink
Migrate start-notebook.sh to bash
Browse files Browse the repository at this point in the history
Based on

> Stop using bash, haha 👍

from jupyter#1532.

If there's more apetite for this, I'll try to migrate
`start.sh` and `start-singleuser.sh` as well - I think they should
all be merged together. We can remove the `.sh` suffixes for
accuracy, and keep symlinks in so old config still works. Since
the shebang is what is used to launch the correct interpreter,
the `.sh` doesn't matter.

Will help fix jupyter#1532,
as I believe all those things are going to be easier to do from
python than bash
  • Loading branch information
yuvipanda committed Oct 17, 2023
1 parent 4c0c0aa commit d1ff010
Showing 1 changed file with 35 additions and 15 deletions.
50 changes: 35 additions & 15 deletions images/base-notebook/start-notebook.sh
Original file line number Diff line number Diff line change
@@ -1,22 +1,42 @@
#!/bin/bash
#!/usr/bin/env python -u
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.
import os
import sys
import shlex

set -e

# The Jupyter command to launch
# JupyterLab by default
DOCKER_STACKS_JUPYTER_CMD="${DOCKER_STACKS_JUPYTER_CMD:=lab}"
# If we are in a JupyterHub, we pass on to `start-singleuser.sh` instead so it does the right thing
if "JUPYTERHUB_API_TOKEN" not in os.environ:
print(
"WARNING: using start-singleuser.sh instead of start-notebook.sh to start a server associated with JupyterHub.",
file=sys.stderr,
)
os.execvp("/usr/local/bin/start-singleuser.sh", sys.argv[1:])

if [[ -n "${JUPYTERHUB_API_TOKEN}" ]]; then
echo "WARNING: using start-singleuser.sh instead of start-notebook.sh to start a server associated with JupyterHub."
exec /usr/local/bin/start-singleuser.sh "$@"
fi

wrapper=""
if [[ "${RESTARTABLE}" == "yes" ]]; then
wrapper="run-one-constantly"
fi
# Wrap everything in start.sh, no matter what
command = ["/usr/local/bin/start.sh"]

# shellcheck disable=SC1091,SC2086
exec /usr/local/bin/start.sh ${wrapper} jupyter ${DOCKER_STACKS_JUPYTER_CMD} ${NOTEBOOK_ARGS} "$@"
# If we want to survive restarts, tell that to start.sh
if os.environ.get("RESTARTABLE") == "yes":
command.append("run-one-constantly")

# We always launch a jupyter subcommand from this script
command.append("jupyter")

# Launch the configured subcommand. Note that this should be a single string, so we don't split it
# We default to lab
jupyter_command = os.environ.get("DOCKER_STACKS_JUPYTER_CMD", "lab")
command.append(jupyter_command)

# Append any optional NOTEBOOK_ARGS we were passed in. This is supposed to be multiple args passed
# on to the notebook command, so we split it correctly with shlex
command += (
[]
if "NOTEBOOK_ARGS" not in os.environ
else shlex.split(os.environ["NOTEBOOK_ARGS"])
)

# Execute the command!
os.execvp(command[0], command)

0 comments on commit d1ff010

Please sign in to comment.