{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Accessing the TM351 VM From Visual Studio Code Editor\n", "\n", "The Python extension for the cross-platform Microsoft Visual Code editor offers increasing levels of support for Jupyter notebooks with every release.\n", "\n", "For example, the [October 2019 release](https://code.visualstudio.com/docs/python/jupyter-support) included support for a live notebook preview within VS Code.\n", "\n", "If you run VS Code on your host computer, you can edit shared notebooks \n", "\n", "(On my computer, I have set files with an `.ipynb` suffix to open, by default, in VS Code.)\n", "\n", "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!\n", "\n", "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.\n", "\n", "By default, we set this as `letmein` but you should change it to something more secure.\n", "\n", "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.\n", "\n", "__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.__\n", "\n", "__THIS IS THE TOKEN VALUE YOU SET IN THE SCRIPT BELOW.__" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%bash\n", "sudo cp /lib/systemd/system/jupyter.service /lib/systemd/system/jupyter.service.backup\n", "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\n", "\n", "sudo systemctl daemon-reload\n", "sudo systemctl restart jupyter.service" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Accessing the VM From VS Code\n", "\n", "Open a notebook (`.ipynb` file) in VS Code.\n", "\n", "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.\n", "\n", "Enter the URL:\n", "\n", "`http://localhost:35180?token=letmein`\n", "\n", "(or whatever token you used).\n", "\n", "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.\n", "\n", "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." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.5.2" } }, "nbformat": 4, "nbformat_minor": 2 }