Skip to content

jupyter

Installing and using jupyter locally

At this point you should already be familiar with jupyter notebooks. We have previously connected to and executed code in jupyter using the cloud-based service Binder. Binder involves a jupyter server running remotely (somewhere in the world) that you connect to and interact with through your browser. In contrast to this remote style of using notebooks, here we will learn to install and use jupyter locally on your own machine. In addition, we will dive a bit deeper into the format of jupyter notebooks, and best practices for sharing them using GitHub.

Installing Jupyter notebook

Install jupyter from conda-forge using the following command.

conda install notebook -c conda-forge

Configuring your local notebook

Although it is not required, we can run a one-time configuration of jupyter before starting it to set some preferences that will make it easier to use. Call the command below to create a jupyter configuration file which will be stored in the .jupyter/ dir in your home directory.

jupyter-notebook --generate-config
Then store a password by running the command below. This is just a measure that adds additional security.

jupyter-notebook password

Now we can start a jupyter notebook server with the following command. The option --no-browser is not necessary for everyone. For WSL2 users it will hide a warning message that would otherwise pop up telling you that it cannot open a browser automatically from your WLS2 system. Either way, once the server is runnign we can open a browser on our own and connect to it by entering the URL addresss.

# move to your home directory, and start a notebook server from here.
cd ~
jupyter lab --no-browser

If you entered the option above then it will not have automatically opened in your default browser. You will see that some text is being printed in the terminal. We'll describe this in the next section. For now, open a new tab in the browser of your choice and in the address bar enter localhost:8888. You are telling it that there is a local server running on your computer, and that it is sending information to port number 8888. This is the default port number used by jupyter. If you have other running notebooks and the 8888 port is not available it will choose the next available port, and it will tell you this in the status message, e.g.:

[I 2025-01-29 20:46:49.530 ServerApp] The port 8888 is already in use, trying another port.
[I 2025-01-29 20:46:49.531 ServerApp] Serving notebooks from local directory: /Users/isaac/proj/hack-the-planet/old-docs/lectures/5.0
[I 2025-01-29 20:46:49.531 ServerApp] Jupyter Server 2.15.0 is running at:
[I 2025-01-29 20:46:49.531 ServerApp] http://localhost:8889/lab

Creating a new jupyter notebook

Navigate to your hacks/hack-2-shell directory (which is a github repository) using the filebrowser on the left. Create a new Notebook using the Python 3 button in the Launcher. In a new cell type print("Hello world") and run this cell. Give the notebook a name by either clicking File-Save Notebook As... or by right-clicking on Untitled.ipynb in the leftnav and choosing Rename. Call the notebook 'MyFirstPython.ipynb'.

What is a server?

A server is a computer program that sends and receives information over a network. Jupyter works as a server that runs a Python kernel (an open session of Python) in the background, which processes information that it receives, and sends outputs back. A web browser is the best way to interface with a server, since it can use all of the power of web design to make a nice interface for displaying outputs. This is the idea behind jupyter notebooks: A python backend that uses and web-interface frontend for users to interact with.

Jupyter filesystem interface

As you have seen before, the jupyter interface will show you a view of your filesystem, including folder and files, from which you can either select an existing file to open, or create a new one. Jupyter notebooks are saved in a file format with the suffix .ipynb, and these will appear with little notebook icons next to them.

Stopping the server

When you start a jupyter server in a terminal, you can just minimize that terminal or stick it in a corner. The server will continue to function for as long as you have this server running. When you are done using, and have saved your notebooks, you can stop the server by closing it (or interrupt it by pressing ctrl-c). Try this now by clicking on the terminal running the server and interrupting it. You can also close the browser tabs that were open. You can start the notebook server again at any time following the same commands be used above.

Assessment

To complete this assignment, open a 'Terminal' using the launcher and add/commit/push your new notebook back up to your github repository.