Computational notebooks: connect code, explanations, and results
A computational notebook brings runnable code, explanations, and outputs into one document for exploration and sharing. A visible output does not prove that the current code produced it. Learn how cells and kernels interact, how hidden state arises, and how to check a clean rerun.
What is a computational notebook?
A computational notebook is a working document where you run pieces of code and read explanations and results alongside them. An analyst can divide a question into steps and place each step's inputs, code, table or chart, and interpretation close together. This makes notebooks useful for exploratory analysis, teaching, research records, and review with colleagues. Using a notebook by itself does not make an analysis correct or automatically reproducible.
The familiar .ipynb file used by Jupyter is a JSON document that stores cells, execution outputs, and metadata. It is Jupyter's file format, not a format that every computational notebook must use. Different notebook environments share the general idea but may differ in execution and storage details.
How do cells, outputs, and a kernel fit together?
A notebook is organized into cells. A Markdown cell holds formatted explanation; a code cell holds instructions for a kernel to execute. A run may attach text written to standard output, an error message, a number, a table, or a rich result such as an image to the code cell. The file can preserve earlier outputs along with the code. Therefore, seeing a result in a document does not prove that the current code just produced it. The Jupyter format stores cell source, output, and information such as execution count as distinct fields.
A kernel is the process that runs the code. It keeps variables in memory, so a later cell can use a value created earlier. This convenience also creates hidden state. For example, cell 5 may use a variable defined in cell 2. If someone ran cell 5 first and cell 2 later while exploring, a result may remain on screen. A first-time reader who runs cells from top to bottom may then get an error or a different result. Saved execution counts and outputs are clues, not a complete verification.
Example: analyze orders over five days
The numbers below are hypothetical and are not business data. Suppose the question is, “How many orders were placed per weekday on average?” A notebook might follow this sequence.
First, an explanation cell records the question, date range, and rule for counting an order. The next code cell contains illustrative daily order counts:
orders = [8, 11, 7, 14, 10]
daily_mean = sum(orders) / len(orders)
print(daily_mean)The output is 10.0. A following explanation can say, “In this example, the five-day average is 10 orders per day.” But the mean alone cannot show whether orders were stable, whether weekends behaved similarly, or whether a campaign caused a change. The date range and unit of aggregation can change the answer, so record them. The educational value of a notebook is that a reader can follow the connection between question, calculation, result, and interpretation rather than seeing only a line of code.
How can you check a result before trusting or sharing it?
Before citing or sharing an analysis, start with a fresh kernel and run all code cells from the beginning in document order. JupyterLab and similar products offer a “restart kernel and run all” command, but menu names vary by product and version. A general check is:
Record where the input data came from, the period, filters and time zone used, and whether required files are available.
Describe the execution environment and required package versions. Note conditions that can change results, such as randomness or external services.
Reset the kernel and run from the first cell through the last. Check for errors and confirm that outputs are new results.
Check key numbers against their stated denominator, period, and unit, using a small sample of source data or an independent calculation. Do not treat an old saved output as proof of success.
Save after execution and confirm that recipients have the data and environment notes they need. Remove secrets, personal information, and unnecessarily large outputs.
If a cell fails, do not keep running later cells in a way that hides the failure. First inspect input paths, column names, data types, and environment differences. After a fix, reset and rerun the whole sequence to make sure it no longer depends on old state. If data access requires permission, tell readers to run it in an authorized environment; never store credentials in the notebook.
What problems do notebooks not solve?
Code, explanations, and saved outputs are close together, which aids reading. Still, results may change or fail if a data file is missing, package versions differ, or remote data have changed. To support reruns, describe data source and version, execution environment, dependencies, and run instructions. A 2024 study of Jupyter notebooks linked to biomedical papers found that 1,203 of 10,388 notebooks that could be rerun after their dependencies were installed completed without errors; 324 of those produced results different from the saved originals. These figures describe that study's biomedical corpus and methods, not a universal rate. They illustrate why sharing one file alone cannot guarantee reproducibility.
For work that must run repeatedly on a schedule, or where a team needs to maintain and test shared functions, it may be better to move analysis logic into a script or package and use the notebook to explain results. A spreadsheet may fit better when people need to inspect formulas and inputs cell by cell. Choose a notebook based on whether readers need to follow and rerun the analysis, not because a particular tool is fashionable.
Sources
Project Jupyter, What is Jupyter? https://docs.jupyter.org/en/stable/what_is_jupyter.html
Jupyter nbformat, The Notebook file format. https://nbformat.readthedocs.io/en/latest/format_description.html
Rule et al., Ten simple rules for writing and sharing computational analyses in Jupyter Notebooks (2019). https://journals.plos.org/ploscompbiol/article?id=10.1371/journal.pcbi.1007007
Samuel and Mietchen, Computational reproducibility of Jupyter notebooks from biomedical publications (2024). https://doi.org/10.1093/gigascience/giad113
JupyterLab, Notebooks documentation. https://jupyterlab.readthedocs.io/en/stable/user/notebook.html