Starting and Using a Jupyter Notebook
This page provides operating system-specific advice for starting a Jupyter notebook, as well as the basics of using a Jupyter notebook, as mentioned in Section 2.2.3 of the textbook. Additional information on using a Jupyter notebook is given in Section 3.2.4 in the textbook.
Starting Jupyter Using Anaconda Navigator
The easiest way to start Jupyter is from Anaconda Navigator. The documentation for Navigator gives details (including how to start the Navigator app on different operating systems), but the main steps are:
Once Jupyter is started, we can create a new notebook or open existing notebooks.
Creating a Jupyter Notebook from a Terminal Window or Desktop App
Here are various ways of doing so, depending on our operating system:
- Linux: Type
jupyter notebook
in a terminal window. A window will be opened in a web browser window that lists the files in the current working directory. (See Section 10.2.1 in the textbook for a description of a working directory.) To create a new notebook, select “New” → “Python 3” (The “Python 3” selection is listed under the “Notebooks” heading in the “New” menu button.) - Mac OSX: Type
jupyter notebook
in a terminal window. The notebook will be opened in a web browser window. - Windows: Search for the “Jupyter Notebook” desktop app and run it. Windows may ask for which browser to use to open the notebook in. Select which browser and the Jupyter notebook will come up.
The location of the jupyter
application is the same place as the python
application. See the page on starting and using the Python interpreter in a terminal window for the discussion of how to find the full path to those applications for various operating systems.
Opening an Existing Jupyter Notebook from a Terminal Window
To open an existing Jupyter notebook: Everything is the same as creating a new notebook above, except, to use a terminal window to access the notebook, type:
jupyter notebook file.ipynband the file.ipynb notebook will be opened in a web browser window. If a Jupyter notebook session is already running in a browser, additional notebooks can be opened via the “File” → “Open…” menu command.
Downloading and Running Jupyter Notebooks from the Internet
Many Jupyter notebooks are also available online (one such repository is nbviewer.jupyter.org). To run them on a local computer, visit the notebook, click on the Download icon as shown below (circled in red):
save the web page to the computer as a file, and open the file using Jupyter. We can use this method to access and run the Jupyter notebooks that are part of this book.
Using a Jupyter Notebook
The Jupyter notebook allows us to process blocks of code at a time, arbitrarily edit previously executed code and re-run it, and include nicely formatted blocks of descriptive text. Below shows what an empty Jupyter notebook looks like when opened:
The cell next to the In [ ]
prompt is where we type in our Python code. Below shows a Jupyter notebook with the commands of Figure 2.1 in the textbook typed in one cell labeled with In [1]
:
When that cell is executed, the output from the last line of code is output in the Out[1]
cell. In multi-cell Jupyter notebooks, the numbering increases for each new In
/Out
cell. Here are some options for executing a cell(s):
- Type “Ctrl+Enter”: Execute the contents of the current cell,
- Type “Shift and Enter”: Execute the contents of the current cell and open an empty cell below the current cell for additional input,
- Select the menu command “Cell → Run All”: Run all cells in the notebook.
While basic calculators usually can only save a single value in memory, Jupyter automatically saves an open notebook. We can also manually save the current notebook via the menu command “File → Save and Checkpoint”.
Renaming a Jupyter Notebook
We can change a Jupyter notebook filename using the typical operating system commands to manipulate filenames. We can also change the name of the notebook file from inside Jupyter. Click the title field to the right of the Jupyter logo:
which will bring up a pop-up window in which we can edit the name of the file:
Making the Jupyter Notebook Stop
The “Kernel” menu contains commands to stop execution of the notebook (“Kernel → Interrupt”) and restart and rerun the notebook (“Kernel → Restart & Run All”). To close the window and stop the kernel select the “File → Close and Halt” menu option. Close and halt to close the window and stop the kernel. If the notebook was started on Windows using the Jupyter Notebook installed with the Anaconda distribution, clicking the “Logout” and/or “Quit” buttons may close the notebook. If the notebook was started from a terminal window, type “Ctrl-c” then y
to shutdown all the notebooks running.