3.2 Using Jupyter notebooks
Now you have set up Jupyter to run with the programming language of your choice, we should start using it. How you do that is detailed in this section.
3.2.1 Creating a notebook
You can either open up the Anaconda navigator and then Jupyter notebooks, or open Jupyter notebooks directly. Once open, navigate to the directory you would like to create the notebook in (If you are using a version control system like Git, then you should be within the project’s repository)
Select the New button in the top right corner, and then select the language you would like to program in (this assumes that you have downloaded an appropriate kernel if you would like to use a language other than Python)
3.2.2 Running a Jupyter notebook
Now you have the notebook open in your chosen language, it’s time to start doing some data exploration and analysis. Here, we’ll cover some basic commands that will get you started, but to fully leverage the power of the notebook, you should read the Jupyter documentation, along with the documentation of your preferred kernel, particularly sections relating to magic
commands (which are language-specific).
When you are writing in a cell (‘Edit’ mode), you can use these commands:
Keyboard shortcut | |
---|---|
Shift + Enter |
Executes the current cell and enters you into the next one |
Ctrl/Cmd + Enter |
Executes the current cell, but does not enter you into the next one |
Esc |
This exits ‘Edit’ mode without executing the cells |
Tab |
Code completion or indent |
If you are not in ‘Edit’ mode (‘Command’ mode), and therefore at least one cell is selected, you can use these commands:
Keyboard shortcut | |
---|---|
Ctrl/Cmd + a |
Add an empty cell above your current cell |
Ctrl/Cmd + b |
Add an empty cell below your current cell |
dd |
Delete the selected cell |
Ctrl/Cmd + m |
Change the cell type to ‘Markdown’ so you can add text |
Ctrl/Cmd + y |
Change the cell type to ‘Code’ so you can add code |
Enter |
Enter ‘Edit’ mode |
3.2.3 Customizing Jupyter notebook’s UI
The following section is not essential and can be ignored if you want to keep things as simple as possible.
Because I do not like the In[]
Out[]
text showing in documents, along with centering plots/figures, I have customized the Jupyter notebook settings. If you would like to do the same, this section should help you. It is not necessary, but I feel that it gives cleaner documents (including pdf documents via LaTeX). If you do this, it is essential that you routinely restart the kernel to run everything again in a fresh environment as it is incredibly easy to run code blocks out of order and forget how this changes the output and introduces hidden packages.
If you would like to customize the look of the notebook, jupyterthemes is a great package that can be installed. I have also edited the custom.css
file (C:\Users\owner\.jupyter\custom\
), adding display: None;
under the section
div.prompt,
.prompt {
so that it now reads
div.prompt,
.prompt {
font-family: monospace, monospace;
font-size: 9pt !important;
font-weight: normal;
display: None;
.
.
.
}
This removes the In[]
Out[]
text. To centre the output of tables/figures, add
.output_png {
display: table-cell;
text-align: center;
vertical-align: middle;
}
to the custom.css
file, right after the .prompt {..}
section.
To enable soft wrapping in the notebook, you need to edit the notebook.json
file (C:\Users\owner\.jupyter\nbconfig\
). If it does not exist, you need to create it. Once open, add
{
"MarkdownCell": {
"cm_config": {
"lineWrapping": true
}
},
"CodeCell": {
"cm_config": {
"lineWrapping": true
}
}
}
before restarting Jupyter.