What is a Jupyter Notebook?
For this lesson we will be creating and using Jupyter Notebooks. The Jupyter Notebook is an open source web application that you can use to create and share documents that contain live code, equations, visualizations, and text. Jupyter Notebook is maintained by the people at Project Jupyter. Jupyter supports over 40 programming languages, including Python, R, Julia, and Scala. Notebooks can be shared with others using email, Dropbox, GitHub and the Jupyter Notebook Viewer.
Python vs. Jupyter
You may be wondering why you type jupyter notebook
to run the Python interpreter rather than just python
. This does also work but this is a much more basic interpreter than Jupyter Notebook that doesn’t have tab completion, syntax highlighting, etc. If you ever need an interactive Python prompt, Jupyter notebook is the best option!
Start a Jupyter Notebook:
For this lesson, Jupyter Notebook is installed in a specific Conda environment. In our class computer that environment is called python-intro
. After activating the conda environment using Conda activate python intro
, you can start a Jupyter Notebook server with the following command:
This command may run up a series of processes that will end up in two ways:
- The terminal may end up providing you a link to click on so you can access the Jupyter Notebook.
OR
- A Chrome, Firefox or Edge window will pop-up with a Jupyter Notebook tab open.
Creating a Notebook
After starting a Notebook server the next thing to do is create or open an actual Notebook document!
To create the Notebook click the New button (upper right) which opens a drop down with a list of choices. You will select the option for Python 3.
The webpage should now look like this:
Naming your notebook
You will notice that at the top of the page is the word Untitled. This is the title for the page and the name of your Notebook. Go ahead and change it to something more meaningful. Just move your mouse over the word Untitled
and click on the text. You should now see an in-browser dialog titled Rename Notebook.
Running Cells
A Notebook’s cell defaults to using code whenever you first create one, and that cell uses the kernel that you chose when you started your Notebook.
In this case, you started yours with Python 3 as your kernel, so that means you can write Python code in your code cells. Since your initial Notebook has only one empty cell in it, the Notebook can’t really do anything.
Thus, to verify that everything is working as it should, you can add some Python code to the cell and try running its contents.
Getting Python to do something
Now we can start and (possibly more importantly!) exit Python, we can try to get it do something by giving it a command. The Jupyter Notebook intepreter (i.e. the In [x]:
prompt) works in a very similar way to the shell except that here you will be typing python code directly instead of running programs. We shall start by getting Python to print something. This is very basic but will be invaluable going forward:
Running a cell means that you will execute the cell’s contents. To execute a cell, you can just select the cell and click the Run button that is in the row of buttons along the top. It’s towards the middle. If you prefer using your keyboard, you can just press Shift+Enter.
So what did we just do? We typed in a python statement that was interpreted by Python and acted on when we pressed Shift+Enter. It interpreted this as ’call the function print
with the argument "Hello World"
. It went away, ran the appropriate code and returned.
But what does the print
function do? In this case, it’s fairly self-expanatory but if you wanted to know more you can use the help
function:
Help on built-in function print in module builtins:
print(*args, sep=' ', end='\n', file=None, flush=False)
Prints the values to a stream, or to sys.stdout by default.
sep
string inserted between values, default a space.
end
string appended after the last value, default a newline.
file
a file-like object (stream); defaults to the current sys.stdout.
flush
whether to forcibly flush the stream.
Jupyter Notebooks also have shell-like behavior in that you can use Tab
to auto-complete a function or variable name:
Adding Rich Content to your Notebook
Jupyter Notebook supports adding rich content to its cells. Lets take a look some of the things you can do with your cells using Markup and Code.
Cell Types
There are 3 cell types you can create in a Notebook: Code, Markdown, Raw NBConvert. The Cell menu Cell Type selection shows you them with their corresponding keyboard short-cut:
The primary cell types that you will use are the Code and Markdown cell types. You have already learned how code cells work, so let’s learn how to style your text with Markdown.
Styling text with Markdown
Jupyter Notebook supports Markdown, which is a markup language that is a superset of HTML. We will look at some of the basic styling possible using Markdown in the following examples, for a more extensive guide visit: Ultimate Markdown Guide
To write Markdown in the Notebook, lets first set a new cell to Markdown or use keyboard short-cut M and add some text to the cell:
Run the cell using your favorite method and it should look like this:
To make your text BOLD, use a double underscore or a double asterisk.
Creating headers in Markdown involves placing a pound sign at the beginning of a line and making a space after! The more pound signs you use, the smaller the header. Jupyter Notebook even kind of previews it for you:
Running the cell you are left with a beautiful header for your notebook.
In case you want to insert a code example that you don’t expect to be run by the end user, there’s a Markdown for that too! For inline code highlighting, just surround the code with backticks. If you want to insert a block of code, you can use triple backticks and also specify the programming language:
Jupyter notebooks provide a wide set of features to explore and use. You will want to investigate more on the topic. Some important topics not covered are Exporting Notebooks, Installing Kernels, Notebook extensions, hosting your Jupyter Notebook on Jupyter Hub and more!
Quit Jupyter Notebooks
To quit out, you can do the following:
- Hit the Quit button then close the browser tab
- In the shell press Cntrl C twice anmd then close the browser tab containing the running Jupyter Notebook.
This will then drop you back to the shell prompt you were at before.
All text and materials in these workshops comes from the Data Carpentries. More especifically this lesson follows the Data Carpentries lesson “Programming with Python”.
CDABS has modified this lesson to better fit our techonogical capabilities.