This tutorial walks you through the
Jupyter Notebook interface.
At the end of this activity, you will be able to:
Jupyter Notebookfrom the terminal.
- Write and run
- Be able to add and run
Markdowntext in a
What You Need
- You will need to have Git, Bash, and Anaconda setup on your computer to complete this lesson. Instructions for setting up these tools are here: Setup Git/Bash and Anaconda
- You will also need to have the
earth-analytics-pythonenvironment set up. Instructions for setting up this environment are here: Set up earth analytics python environment
- You will need to have completed the Introduction to Bash Shell lesson.
About Jupyter Notebook
Jupyter Notebook supports a reproducible workflow. It allows you to:
- Write and run inline code interactively.
- Share your work with colleagues, so they can see both your code and the code outputs.
- Document all aspects of your workflow using a combination of well-written code and
Advantages of Jupyter Notebook
Jupyter Notebook also makes coding more efficient.
Jupyter Notebookhas different shortcuts for running cells, adding new cells and formatting the notebook to help save you time
- Autocomplete : Autocomplete is available to quickly find function names, arguments for those functions, and variables that are stored in your environment. This makes typing your code easier and less error-prone. Hitting tab while typing will prompt
Jupyter Notebookto help you complete the name of a function or variable that you want to call.
- Markdown Integration: You can combine text and code in the same document. This allows you to document and explain the code you have written and make your workflow more reproducible.
Get to Know Jupyter Notebook
In this lesson, you will explore Jupyter Notebook, the Integrated Development Environment (IDE) that you will use to write code, navigate files on our computer, inspect variables and more.
An IDE is different from a text editor. IDEs allow you to write, test, and debug code.
Other commonly used
Python IDEs are Spyder (which comes with the
Python Anaconda distribution) and PyCharm. There are many tools to help you code, but in this class, you will use
Jupyter Notebook is an open source product and free under the BSD 3-clause “New” or “Revised” License.
How to Start Jupyter Notebook
- To launch
Jupyter Notebookvia the terminal, first make sure you are in the
earth-analyticsdirectory and that the
earth-analytics-pythonenvironment has been activated. Then, you can launch
Jupyter Notebookby typing
$ source activate earth-analytics-python $ cd earth-analytics $ jupyter notebook
If you are on Windows, use
activate earth-analytics-python to active the conda environment:
$ activate earth-analytics-python $ cd earth-analytics $ jupyter notebook
Jupyter Notebooks combine three components:
The Jupyter Notebook:
Jupyter Notebookis a web application that launches in a web browser like Firefox or Safari. The notebook is where you write and run your code.
Kernels: A kernel is the program that runs your code. For instance, if you want to run
Pythoncode, then you will run a
Jupyter Notebook. The kernel runs your code in the given language and returns output back to the
Jupyter Notebookweb application.
Jupyter Notebooksupports over 40 different kernel languages. In this class, you will use
Python. Specifically, you will use the
earth-analytics-pythonkernel which contains all of the libraries that you need for this course.
Notebook documents The notebook document is the file that you use to store your code and
Jupyter Notebookfile has an
When you first start the Jupyter Notebook server, your browser will open to the notebook dashboard.
The dashboard serves as a homepage for
Jupyter Notebook. Its main purpose is to display the notebooks and files in the current directory. For this class, you will always work in the
Explore the Jupyter Notebook Interface
When you open or create a new
Jupyter Notebook file, you will see three main parts:
- Notebook Area and Cells
Work With Markdown and Code Cells in Jupyter Notebooks
Jupyter Notebook file consists of a set of cells that can store text or code. You will focus on code and
Markdown cells for this lesson.
- Text Cells: Text cells allow you to write
Markdown. This is where you will describe your workflow.
- Code Cells: Allow you to write code (e.g. using
For a full list of keyboard shortcuts, click the help button, then the keyboard shortcuts button.
Markdown Cells in Jupyter
Jupyter Notebook file can contain text written using the
Markdown syntax, in a cell that is specified for markdown.
Code Cells in Jupyter Notebooks
You can add code to the code cells in
Jupyter Notebook. Code is written in code chunks. When you run the code in a cell, the code output displayed below. A cell can be run by hitting the run button or using a keyboard shortcut, as discussed in the next section.
For more information on code in
Jupyter Notebook, see the next lesson on Install and Import Python Packages.
Useful Jupyter Shortcuts For Running Code and Adding Cells
Menu Tools vs. Keyboard Shortcuts
- You can manipulate your
Jupyter Notebookusing the drop down tools from the menu, with keyboard shortcuts, or using both.
- Keyboard shortcuts allow your workflow to be faster and more efficient.
- The most important keyboard shortcuts are Enter, which enters edit mode, and Esc, which enters command mode.
- Enter Mode is indicated by green around cells and is for editing the contents of a cell
- Command Mode is indicated by a grey cell border with blue on the left edge. This mode allows you to edit the notebook as a whole.
The table below shows common tasks in
Jupyter Notebook and how to do them using keyboard shortcuts or the menu tool.
|Function||Keyboard Shortcut||Menu Tools|
|Save notebook||Esc + s||File → Save and Checkpoint|
|Create new cell||Esc + a (above), Esc + b (below)||Insert→ cell above Insert → cell below|
|Run Cell||Ctrl + enter||Cell → Run Cell|
|Copy Cell||c||Copy Key|
|Paste Cell||v||Paste Key|
|Interrupt Kernel||Esc + i i||Kernel → Interrupt|
|Restart Kernel||Esc + 0 0||Kernel → Restart|
|Find and replace on your code but not the outputs||Esc + f||N/A|
|merge multiple cells||Shift + M||N/A|
|When placed before a function Information about a function from its documentation||?||N/A|
Data Tip: Inline magic are commands built into the Python kernel and always start with a %. Magic commands are useful shortcuts. Magic commands always start with a % because this symbol isn’t valid in Python. Magic commands also only work on the ipython kernel. Magic commands provide shortcuts to common tasks that would normally take several lines of code to accomplish in Python.
|Magic||What it does|
|Saves the contents of a cell to an external file|
|Shows how long it takes a cell of code to run|
|List all variables of a global scope|
|Pass variables between notebooks|
|Insert code from an external script|