Reputation: 733
I like to work out of a Jupyter Notebook that is connected remotely to a PC from my laptop. I'd like to automate the process of connecting to the PC by ssh, initializing a Jupyter notebook in the PC, then opening a browser on my laptop to access that notebook. At present, these are the steps I take to do this:
jupyter notebook --no-browser --port=8889
ssh -N -L localhost:8887:localhost:8889 username@PC_IP_address
to bind the remote port 8889 to the local port 8887Both the PC and the laptop are on the same network. I automated 2. on my local machine by creating a profile in Iterm3 that executes the script. I'd like to automate the entire process so that with one alias, I can launch a remote notebook on my local machine. Many thanks.
Upvotes: 4
Views: 647
Reputation: 1
You can add a '&' after your first ssh so you don't have to control+c
Upvotes: 0
Reputation: 733
After much effort, here is a hacky solution that worked for me
jupykaggle(){
# arguments: projectfolder environmentname
competitionfolder=$1
envname=$2
ssh USER@IPADDRESS "cd Dropbox/data_science/kaggle/${competitionfolder};
source /home/USER/anaconda2/bin/activate ${envname};
/home/USER/anaconda2/bin/conda env list;
kill \`lsof -t -i:8889\`;
/home/USER/anaconda2/envs/${envname}/bin/jupyter notebook --no-browser --port=8889; exit"
# after notebook is initialized in remote host, Control+c to exit this ssh session
# and enter the second one to bind the remote port 8889 to local port 8887
echo "Bind remote port 8889 to local port 8887"
ssh -N -L localhost:8887:localhost:8889 USER@IPADDRESS
}
alias jupybrowser='open -a /Applications/Google\ Chrome.app http://localhost:8887'
I execute
jupykaggle projectfolder environmentname
to cd into the project directory, activate the virtual environment, kill the process running in the port I need to use, and initialize a jupyter notebook using that port (using the installation inside the virtual environment). I then exit this ssh session and enter another ssh session where I bind the remote port 8889 to the local port, 8887. The last alias just opens a Chrome browser in local host 8887 to open the notebook.
I still need to copy and paste the token from the remote notebook, but that's okay. This is still significantly fewer steps than my previous workflow.
Suggestions for improvements are welcome.
Upvotes: 1
Reputation: 4969
I'd use a function, if I were you. Put this in your .bashrc
jupy(){
ssh username@PC_IP_address jupyter notebook --no-browser --port=8889
ssh -N -L localhost:8887:localhost:8889 username@PC_IP_address
firefox https://localhost:8887
}
Upvotes: 3