Created
January 14, 2019 17:14
-
-
Save piwwww/28abb5a016a1a78b5b7f9bcbbbbb0a10 to your computer and use it in GitHub Desktop.
Running Jupyter Lab Remotely (Local Part)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# tips from Ben Lindsay: http://benjlindsay.com/blog/running-jupyter-lab-remotely/ | |
# This function does a few things when you type jllocal: | |
# Runs ssh tunneling command if it's not already running | |
# Grabs the Jupyter token from the remote machine | |
# Opens a tab in your browser with the right url and token for you | |
# When you're done with Jupyter Lab, you just type jllocal kill and it will shut down the ssh connection. | |
function jllocal { | |
cmd="ssh -Y -fN -L localhost:8888:localhost:8888 USERNAME@HOSTNAME" | |
running_cmds=$(ps aux | grep -v grep | grep "$cmd") | |
if [[ "$1" == 'kill' ]]; then | |
if [ ! -z $running_cmds ]; then | |
for pid in $(echo $running_cmds | awk '{print $2}'); do | |
echo "killing pid $pid" | |
kill -9 $pid | |
done | |
else | |
echo "No jllocal commands to kill." | |
fi | |
else | |
if [ ! -z $n_running_cmds ]; then | |
echo "jllocal command is still running. Kill with 'jllocal kill' next time." | |
else | |
echo "Running command '$cmd'" | |
eval "$cmd" | |
fi | |
url=$(ssh USERNAME@HOSTNAME \ | |
'/REMOTE/PATH/TO/jupyter notebook list' \ | |
| grep http | awk '{print $1}') | |
echo "URL that will open in your browser:" | |
echo "$url" | |
open "$url" | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment