PollPenn
PollPenn

Reputation: 733

How to automate initialization of a remote Jupyter notebook on local machine?

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:

  1. SSH into the remote PC and initialize a Jupyter notebook by the executing jupyter notebook --no-browser --port=8889
  2. Back on my local machine, I run ssh -N -L localhost:8887:localhost:8889 username@PC_IP_address to bind the remote port 8889 to the local port 8887
  3. I then open a browser on my laptop to access the remote notebook at the url https://localhost:8887

Both 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

Answers (3)

You can add a '&' after your first ssh so you don't have to control+c

Upvotes: 0

PollPenn
PollPenn

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

Ljm Dullaart
Ljm Dullaart

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

Related Questions