I was recently in a situation where I needed to remotely access a python notebook. Here’s how I went about doing it, and you can follow the steps it if you find yourself with a similar problem.
Instructions
The basic scenario is as follows:
- I want to host a Jupyter notebook on my laptop (Windows 10)
- I want to access it on a desktop (Windows 7)
There are a number of steps involved here, including setting up the python environment on the host, as well as enabling an SSH server.
Install Python & Notebooks
The first stage is on the Windows 10 Laptop, to prepare python:
- Install python: I use a clean install from python.org (you could use a pre-populated environment like anaconda, if you want less work) which includes pip. Just download and run the installer – I recommend the latest version of the python 3 branch.
- Press +X, and open a new “PowerShell” prompt. Then type
python
and if everything set up properly, then you should see a python interactive console- If you get a “command not found” error, then python didn’t get added to the PATH properly.
- Press +R, and type SystemPropertiesAdvanced, then click “Environment Variables”.
- Select the user (top half) variable for “Path”, Edit, and browse to add the folder that contains your newly installed python.exe
- (Optional) Set up a python virtual environment – I prefer these to system wide installs like anaconda uses, just because it makes it a bit less messy when installing packages.
- First you need to add the virtualenv package – from a PowerShell prompt, type
pip install virtualenv
- Important note: Don’t create a virtual environment that at any point includes spaces, hyphens or other possibly suspect characters. For some reason python / pip doesn’t like that.
- Create the virtual environment with the command
python -m virtualenv jupyterenv
- Start the virtual environment with the command:
.\jupyterenv\Scripts\activate.ps1
- First you need to add the virtualenv package – from a PowerShell prompt, type
- Install the jupyter package with the command:
pip install jupyter pandas numpy scipy
(You can add any additional packages you need to that last line) - Start the notebook to make sure everything worked:
jupyter notebook --nobrowser
- Make a note of the localhost URL that is generated, as stated on the command line
Enable SSH Server
The next step is to allow the use of an SSH server on the Windows 10 Laptop:
- Press +R and type ms-settings:optionalfeatures
- If you don’t already have it available, click “Add a feature” and install both of the OpenSSH components
- Press +X, and open a new “PowerShell (Admin)” prompt. Then run the command
Start-Service -Name "sshd"
- Use the command
ipconfig
and make a note of the IPv4 address for your internet connection (there may be more than one – make sure to select correctly)
Enable the SSH Client
On the Windows 7 Machine, start a connection over SSH. Unfortunately, Windows 7 doesn’t have native SSH software, so you’ll need to work around this limitation:
- For SSH on Windows 7, I use PuTTY, which can be installed from here
- Open up PuTTY, and prepare to add some new session info
- In ☰ Session, add the IPv4 address of the laptop to “Host Name”
- In ☰ Connection > SSH > Tunnels, Under “Source Port”, put the port that you want to use on the windows 7 machine. For simplicity I suggest using the same port as from the localhost URL. e.g. 8888.
- Under “Destination”, put the name and port part of the localhost URL from earlier, including the port. e.g. localhost:8888
- Click “Add”
- In ☰ Session, enter a name in the “Saved Sessions” box and click “Save”, so it is remembered for next time
Start working!
On the Windows 7 Machine, load the notebook:
- In PuTTY, select your saved session and then click “Load”
- Click “Open” to start the SSH session
- Enter the username and password for the laptop when prompted
- In Vivaldi (or your web browser of choice), enter the localhost URL for the notebook as stated on the command line of the laptop
- Load the webpage
- Either open up your python notebook, or create a new one
In my office, we run a (small) jupyter notebook on a server, where all the files are synced using dropbox. We should make the switch to JupyterLab. Being able to have your code, tests, comments, results and documentation all in one place is really a blessing!