Getting Started with Jupyter
The user interface for Jupyter Notebook is quite intuitive. Exploring the user-interface and getting some hands-on experience with notebooks by running code is a great way to get a sense of all that Jupyter Notebooks has to offer. Let’s get started by taking a look at how to launch a notebook.
Using Jupyter Notebook
Launch a Notebook
To launch a Jupyter notebook, open your terminal and navigate to the directory where you would like to save your notebook. Then type the command
jupyter notebook and the program will instantiate a local server at http://localhost:8888 (or another specified port).
A browser window should immediately pop up with the Jupyter Notebook interface. Otherwise, you can use the address Jupyter Notebook directs you to in the terminal by copying and pasting it or typing it in your browser’s address bar. To stop the server and shutdown the kernel from the terminal, hit Ctrl-C twice.
Jupyter Notebook Interface
In the Jupyter Notebook interface, you can see all of the files in your current directory. All Jupyter Notebooks are identifiable by the notebook icon next to their name. If you already have a Jupyter Notebook in your current directory that you want to view, find it in your files list and click it to open.
To create a new notebook, go to New and select Notebook - Python 3. If you have other Jupyter Notebooks on your system that you want to use, you can click Upload and navigate to that particular file. Notebooks currently running will have a green icon, while non-running ones will be grey. To find all currently running notebooks, click on the Running tab to see a list.
Inside the Notebook
When you open a new Jupyter notebook, you’ll notice that it contains a cell.
Cells are how notebooks are structured and are the areas where you write your code. To run a piece of code, click on the cell to select it, then press Shift + Enter or press the Play button in the toolbar above. Additionally, the Cell dropdown menu has several options to run cells, including running one cell at a time or running all cells at once.
After you run a cell, the output of the cell’s code will appear in the space below. To stop running a piece of code, press the Stop button.
To create new cells, use the plus (+) button in the toolbar or hit Shift + Enter on the last cell in the Notebook. To cut, copy, delete or just generally edit cells, select the cell you want to modify and go to the Edit button in the navigation bar to see your options.
In addition to running lines of code, you can also include text-only cells that use Markdown to format and organize your notebooks.
When you create a new cell, it will default to being a Code cell. To create a cell that uses markdown, click on the Cell menu from the navigation bar, scroll down to Cell Type and choose Markdown.
Occasionally, you might need to restart the kernel. Head to the Kernel dropdown and hit Restart. To shut down a kernel, you can click Shutdown, which will have a dialogue process asking if that’s what you would like to do. To force an immediate shutdown, go to the File dropdown and click Close and Halt and the browser window will close itself. Restarting and shutting down kernels will affect your variables, so be careful.
The toolbar has several shortcut buttons for popular actions. From left to right:
- Add a New Cell
- Cut Selected Cells
- Copy Selected Cells
- Paste Cells Below
- Move Selected Cells Up
- Move Selected Cells Down
- Interrupt the Kernel
- Restart the Kernel
- Dropdown that allows you to change the cell type
- Shortcut to open the command palette.
Save & Export
Jupyter Notebook files are saved as you go. They will exist in your directory as a JSON file with the extension
.ipynb. You can also export Jupyter Notebooks in other formats, such as HTML. To do so, go to the File menu, scroll down to Download and select the type of file you’re looking for. A popup will appear asking where you would like this new file to download. Once you’ve navigated to the appropriate directory, click Save and Checkpoint.
Jupyter Notebook also has a modal user interface called the Modal Editor. This essentially means that your keys will complete different tasks depending on the mode your Notebook is in. There are two modes in Jupyter Notebook, Edit and Command.
In edit mode you will see that the cell border is green and the cursor is flashing in the cell. When the cell is in Edit Mode, you can add text just like a typical editor. To access Edit Mode, you can either hit Enter on a cell or click on it with your mouse.
In Command Mode, the cell boundary will be grey, and the left margin is colored blue. When you are in command mode, unlike Edit Mode you are accessing the notebook as a whole rather than an individual cell. When you are in command mode, you can access shortcuts (see Getting More out of Jupyter Notebook) that will make your work more efficient. To access command mode hit Esc, but remember not to start typing as if you’re in Edit Mode during command mode since it will trigger commands you may not be expecting!
As we’ve also seen, Jupyter Notebook is a very useful way to keep analysis. If you’re interested in learning more about Jupyter Notebooks, read their documentation. To try out a notebook in your browser, go to https://try.jupyter.org/.