Cells in Jupyter Notebooks: Types and Usages#

As outlined previously, Jupyter Book allows for the integration of .ipybn files (better known as Jupyter Notebooks), which are a flexible solution for writing both code (Julia, Python and R) or in Markdown (MyST) in one document.

Note

the name Jupyter references the three programming languages supported by Jupyter: Julia, Python and R

Jupyter Notebooks are organized in a sequence of “cells”, each cell can consist of multi-line content. Depending on the type of cell, the behavior changes if the cell type is changed. There are three types of cells: code cells, markdown cells, and raw cells (raw cells probably won’t be of high significance for building courses). Every cell starts off as a code cell, but can be changed depending on the program you are using:

.iypbn vs .md - When to use what?#

.ipynb

.md (Markdown)

.ipynb files are Jupyter Notebook files and are primarily used for interactive data science and scientific computing. They allow for the combination of code, text, and visualizations all in one place, making it easy to document and share code-driven projects. They are often used by data scientists and researchers to share their work with others, and can be easily converted to other formats like .html or .pdf for sharing.

.md files are Markdown files, which are primarily used for creating simple, easy-to-read documents. They use a simple syntax for formatting text and are often used for documentation, READMEs, and other types of text-based content. They are also commonly used in conjunction with version control systems like Git and can be easily rendered in various platforms.

.ipynb files are best for interactive, code-driven projects

.md files are best for documentation, READMEs, and other types of text-based content

Markdown Cells#

Text can be added to IPython Notebooks using Markdown cells. Markdown is a popular markup language that is a superset of HTML. Its specification can be found here:

http://daringfireball.net/projects/markdown/

You can view the source of a cell by double clicking on it, or while the cell is selected in command mode, press Enter to edit it. Once a cell has been edited, use Shift-Enter to re-render it.

Local files#

If you have local files in your Notebook directory, you can refer to these files in Markdown cells directly:

[subdirectory/]<filename>

For example, in the static folder, we have the logo:

<img src="static/logo.png" />

These do not embed the data into the notebook file, and require that the files exist when you are viewing the notebook.

Security of local files#

Note that this means that the IPython notebook server also acts as a generic file server for files inside the same tree as your notebooks. Access is not granted outside the notebook folder so you have strict control over what files are visible, but for this reason it is highly recommended that you do not run the notebook server with a notebook directory at a high level in your filesystem (e.g. your home directory).

When you run the notebook in a password-protected manner, local file access is restricted to authenticated users unless read-only views are active.

Code cells#

When executing code in IPython, all valid Python syntax works as-is, but IPython provides a number of features designed to make the interactive experience more fluid and efficient. First, we need to explain how to run cells. Try to run the cell below!

import pandas as pd

print("Hi! This is a cell. Click on it and press the ▶ button above to run it")
Hi! This is a cell. Click on it and press the ▶ button above to run it

You can also run a cell with Ctrl+Enter or Shift+Enter. Experiment a bit with that.