Accessing the TM351 VM From Visual Studio Code Editor

The Python extension for the cross-platform Microsoft Visual Code editor offers increasing levels of support for Jupyter notebooks with every release.

For example, the October 2019 release included support for a live notebook preview within VS Code.

If you run VS Code on your host computer, you can edit shared notebooks

(On my computer, I have set files with an .ipynb suffix to open, by default, in VS Code.)

To connect to the Jupyter environment running inside the TM351VM, we need to make a couple of tweaks to the Jupyter service definition file so that it can accept connections from VS Code. Note that the settings below are far from secure, so do not use these settings on a public server!

The following cell will create a backup of the original service definition file, and then update the original so that it can accept external connections. As a mild level of security, we also define a token that needs to be used as part of the connection string.

By default, we set this as letmein but you should change it to something more secure.

Running the following code cell will update the service definition file and then restart the Jupyter server. You will need to reload this notebook once the server has restarted.

NOTE THAT THE FIRST TIME YOU TRY TO ACCESS A NOTEBOOK OR THE NOTEBOOK SERVER HOMEPAGE AFTER RESTARTING THE SERVER, YOU WILL BE PROMPTED TO ENTER A TOKEN.

THIS IS THE TOKEN VALUE YOU SET IN THE SCRIPT BELOW.

In [ ]:
%%bash
sudo cp /lib/systemd/system/jupyter.service /lib/systemd/system/jupyter.service.backup
sudo sed -i "/ExecStart=/c\ExecStart=/usr/local/bin/jupyter notebook --NotebookApp.token=letmein --port=8888 --ip=0.0.0.0 --y --log-level=WARN --no-browser --notebook-dir=/vagrant/notebooks --allow-root --NotebookApp.allow_origin='*' --NotebookApp.allow_remote_access=True" /lib/systemd/system/jupyter.service

sudo systemctl daemon-reload
sudo systemctl restart jupyter.service

Accessing the VM From VS Code

Open a notebook (.ipynb file) in VS Code.

From the Command Palette (⇧⌘P), select the Python: Specify Jupyter server URI command and then the Type in the URI to connect to a running Jupyter server option.

Enter the URL:

http://localhost:35180?token=letmein

(or whatever token you used).

You should now be able to execute the code cells in the notebook openeded inside the VS Code editor against a TM351 VM notebook kernel.

Because you are connected to a Python kernel running inside the VM, you should be able to execute code that depends on Python packages installed within the VM, as well as connecting to and running queries against the databases inside the VM.