Skip to content

Instantly share code, notes, and snippets.

@jtsagata
Last active September 23, 2017 11:23
Show Gist options
  • Select an option

  • Save jtsagata/419c58fbcfcdd22cf81af9474f639844 to your computer and use it in GitHub Desktop.

Select an option

Save jtsagata/419c58fbcfcdd22cf81af9474f639844 to your computer and use it in GitHub Desktop.
Launcher for jupyter notebook
#!/bin/bash
# Remember to first run
# aptapt install jq xdotool
jupyter=${HOME}/anaconda3/bin/jupyter
notebooks=${HOME}/MasterAdventures
terminal_name="Anaconda Kernel"
# Create firefox launcher with selected profile
if [[ ! -x /tmp/jupyter_firefox ]]; then
echo "Creating browser launcher"
cat << EOF > /tmp/jupyter_firefox
#!/bin/bash
/usr/bin/firefox -P Jupiter \$@
EOF
chmod +x /tmp/jupyter_firefox
fi
export BROWSER=/tmp/jupyter_firefox
function help() {
echo "$(basename $0)"
echo " Show jupiter notebook and start a kernel if none is running"
echo " --info Show configuration and runtime info"
echo " --help This message"
}
function start_notebook_and_minimize_term {
jupyter_run_cmd="${jupyter} notebook --notebook-dir=${notebooks}"
jupyter_paths_cmd="${jupyter} --paths"
echo "Launching : ${jupyter_paths_cmd} && ${jupyter_run_cmd}"
mate-terminal --window-with-profile=Jupiter --title="${terminal_name}" -x bash -c "${jupyter_paths_cmd} && ${jupyter_run_cmd}" &
# minimize_terminal
xdotool windowminimize $(xdotool search --sync --name "${terminal_name}" | head -1)
}
# display info about kernel
function display_info() {
${jupyter} --paths
json=$(jupyter notebook list --json)
echo "kernel:"
echo " pid:" $(echo $json | jq -r '.pid')
echo " dir:" $(echo $json | jq -r '.notebook_dir')
echo " url:" $(echo $json | jq -r '.url')
echo " port:" $(echo $json | jq -r '.port')
echo " tok:" $(echo $json | jq -r '.token')
echo ""
}
function start_or_show_notebook() {
jupyter_process=$(${jupyter} notebook list | awk '/^http/ {print $1}')
if [[ -n "${jupyter_process}" ]]; then
${BROWSER} ${jupyter_process} &
else
echo "No running kernel found. Starting a new kernel"
start_notebook_and_minimize_term
fi
}
if ! options=$(getopt -o hi -l help,info -- "$@")
then
echo "Try $(basename $0) --help"
exit 1
fi
set -- $options
while [ $# -gt 0 ]
do
case $1 in
-h|--help)
help; exit ;;
-i|--info)
display_info; exit;;
esac
shift
done
start_or_show_notebook
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment