Using Jupyter Notebooks
DS 2023 | Communicating with Data

Overview¶
In this course we use Jupyter Notebooks.
Jupyter -- which is a portmantaeu of Julia, Python, and R -- refers to a platform that let’s you run Python code interactively and literately.
Interactively: Code is run in cells and the output of the code is printed below the cell. If the output is graphical, this is output too.
Literately: You can write either blocks of code or Markdown to alternate between code and text. This allows you to compose your script as a narrative, something that can be understood by others -- as well as your later self :-)
In this notebook, we highlight some feature of Jupyter Notebooks to keep in mind when using them.
Jupyter notebooks are JavaScript files¶
A Jupyter notebooks is actually a JavaScript file that is run in a browser. (More precisely, the notebooks are JSON documents that are rendered by the Jupyter front end, which is a JavaScript app.)
JavaScript is the native programming language of web browswers.
The browser executes the script, which produces an interactive application.
Jupyter notebook files end in .ipynb.
Jupyter notebook files are typically created within a programming editors, such as VS Code or Jupyter Lab
Understand what kernels are¶
Every notebook, in order to run the code they contain, is associated with a kernel.
A kernel is just an instance of an interpret, such as Python or R. Is it associated with an environment that contains various libraries and versions.
The way you select a kernel depends on the editor your are using.
Kernels can be changed.
Kernels often need to be restarted.
When shutting down a notebook, kernels need to be shut down, too.
Use the Markdown cells¶
The markdown cells in a notebook are as important as the code cells.
They provide context and explanation for the code.
Jupyter notebooks can in fact be used to write scientific (and other) articles.
Markdown is HTML¶
Markdown is an abbreviated syntax for generating HTML.
To use it effectively, you should understand how HTML structures documents.
These are the main elements of HTML that Markdown lets yu
Headers define sections and subsection of a document.
Paragraphs define chunks of text.
Lists define numbered or unnumbered lists of items.
Links define hypertext links to other documents.
Images define embedded images.
And so on.
Here is GitHub’s documentation on the flavor of Markdown they use.
Markdown is more than HTML¶
A very cool thing about markdown is that it allows you to embed other formatting languages. For example, you can write :
$x^2 + y_1 = z$produces
And
$$
I = log_2(\frac{1}{p})
$$produces
You can also create cool diagrams using the Mermaid language.
```mermaid
graph TB
A --> B
A --> C
B --> D
B --> C
```Be careful!¶
Since Jupyter notebooks let you run code in any order — i.e. out of order, from a programming perspective — you can get into trouble.
Re-running a cell may break if it changes the value of variable on which it depends.
The solution is to re-run your notebook often.
But be careful here, too: You may have deleted a cell that defines something that another cell depends on. When you restart the kernel an rerun the cells, the cell with the dependency will fail and throw an error.