Although a Jupyter notebooks opens in your web browser, the software must be installed on your computer. Jupyter Notebooks is included with the Anaconda distribution. It can also be separately installed with thecondapackage manager. (See Appendix A.)
B.1.1 Launch Jupyter Notebooks
Once you have installed the software, you can launch a Jupyter Notebooks session from your operating system’s command line with the command
$ jupyter notebook
You can also use the Anaconda Navigator application to launch the Jupyter Notebooks application.
(You may get a message like
Copy/paste this URL into your browser when you connect for the first time, to login with a token: http://localhost:8888/?token=XXXX
Do as you are instructed to get started.)
A window will open in your default web browser with “Jupyter” at the top left corner (Figure B.1).2 The first of three panes is selected by default. This is a file browser that you can use to find and
1“Jupyter Notebooks” is an extension of “IPython Notebooks” that will eventually be subsumed into “JupyterLab.”
The interface is similar to the notebook environment ofMathematica, Maple, and Sage.
2 T2Starting Jupyter launches several “background processes” that you do not see. These processes communicate with your web browser, which communicates with you. Both Jupyter and your notebooks are “local” since they are located on your computer. The web browser simply provides a convenient and familiar interface for human users.
120
B.1 Getting Started 121
Figure B.1: The Jupyter Notebook file browser.
open an existing notebook or create new notebooks. If you open a Jupyter notebook from your operating system’s command line, the file browser will start in the directory from which you issued the command. If you launch from Anaconda Navigator, the file browser will start in your home directory.
B.1.2 Open a notebook
If you don’t have any notebooks yet, use the file browser and navigate to a folder where you’d like to create one. (You can create a new folder with the
Newbutton at the top right of the file browser.
Check the box next to the new folder and then click
Renameto rename it.) Next, click on the Newbutton and select “Python 3” from the dropdown menu. A new tab will open with a Jupyter notebook that contains a single empty code cell calledIn [ ](Figure B.2). Enter 1+1and then click the
button or hit<Shift-Enter>on your keyboard. You have just run your first code in a Jupyter notebook!
You can save your notebook with the
button, the menu item File>Save, or the shortcut
<Cmd-S>. (Use the menu inside the Jupyter window — not your browser’sFile>Savemenu item.) You can now find it in your computer’s file system (probably namedUntitled.ipynb). If you would like to rename a file, you can simply click on the name of the notebook at the top of the page, or you can use the menu optionFile>Rename....
You can also open existing notebooks saved on your computer. For example, you can view a sample notebook at the blog that accompanies this book.3 It illustrates many features of Jupyter notebooks. You can download the notebook, open it, modify it, and play with your own copy. Many other notebooks are also available for download from the web.
B.1.3 Multiple notebooks
You can run multiple notebooks at the same time. Return to the file browser tab, create a second new notebook, and perform some other calculations. You now have two independent notebook sessions.
Each one has its own state, including the values of all variables. No information is shared between notebooks. You can keep track of all open notebooks in the file browser tab: just select the
Running pane. From this pane, you can selectively terminate any of your running notebooks.
3physicalmodelingwithpython.blogspot.com/2016/01/jupyter-notebooks.html.
Jump to Contents Jump to Index
122 Appendix B Jupyter Notebooks
Figure B.2: A new Jupyter notebook. The green border on the selected cell indicates that Jupyter is in input mode.
B.1.4 Quitting Jupyter
When you are finished computing and editing, you will need to be sure you terminate all of the processes Jupyter is running on your computer. Simply closing the tabs in your web browser will not do the trick.
First, shut down each notebook. You can do this from within a notebook with the menu option File>Close and Halt. This will close the notebook and shut down the Python process that was running in the background. Alternatively, you can use the file browser. Select the “Running” pane to see a list of all active notebooks. There will be a
Shutdownbutton next to each. Use it to shut down all active notebooks.
Next, you need to terminate the Jupyter Notebooks application itself. The simplest way is to use the
Quitbutton (Figure B.1). Alternatively, locate the “terminal window” associated with the application. If you launched the Jupyter Notebooks application from the Anaconda Navigator, you will need to find an application window that displays a series of messages fromNotebookApp: [I 03:14:04.679 NotebookApp] ...
[W 03:17:26.088 NotebookApp] ...
...
If you started Jupyter Notebooks from your operating system’s command line, this will be the terminal window where you entered the startup command. Click on this application. Close the Jupyter Notebooks application once and for all by typing <Ctrl-C>. The program will ask for confirmation. Press<y>then<Enter> to confirm. The program is now closed.
The remainder of this appendix provides a brief overview of some useful features of Jupyter notebooks. However, the best way to learn how to use Jupyter notebooks is to create your own and explore those created by others.
B.1.5 T2Setting the default directory
It is good practice to keep all of your work in one location. You can set the default starting folder for Jupyter notebook sessions. (This is analogous to choosing the “current working directory” in Spyder.)