What is the usage of tag over the cell in jupyter?

Tagging is a fairly recent and perhaps not quite finished feature of jupyter-notebooks, added with version 5.0. From what I understand they are mostly meant for tools such as nbconvert (converts notebooks to other formats such as pdf) and nbval (validates notebooks) and other more or less integrated tools working with jupyter notebooks. Being able to add tags to a cell would enable different behaviours for such tools depending on a cells tag. Some example that could be accomplished with the ability to add tags would be:

  • nbconvert - hide a cell, hide the input leaving the output visible, collapse a cell leaving a way to reveal it
  • nbconvert to latex - markdown cell contains title (or subtitle, abstract...)
  • nbval - check/ignore output from a cell, skip executing a cell, expect a cell to raise an error
  • nbgrader - solution cell, tests cell
  • nbparameterise - cell contains input parameters.

as envisaged by takluyver over at jupyter's github. If you want more information on implementation and the discussion surrounding it you can read more here.


Adding to Christian's answer, there is an important utility you can get from using tags. You can run all cells and keep running even when encountering runtime errors. You flag a cell with raises-exception tag. Very useful for educational purposes. Source.


As pointed out by Christian above, one great use is to provide different input parameters value to this notebook program, nbparameterise is one example. See here. Papermill is another one: see here

From a user perspective, one can probably achieve the same thing by using env variable os.getenv(), or getting from command line argument sys.getargv(), but adding tag to a cell seems to be the easiest.

Under the hood, the jupyter notebook is saved as a json file. Let says you tag the first cell as Parameters, and declare variables var1=10 and var2 ='adam'. The json file would look something like below, and the tags is in the metadata section. So it is simple for a tool to parse this json and get to the tags section, and say replace the variables with different values.

{
  "cells": [
  {
    "cell_type": "code",
    "execution_count": 1,
    "metadata": {
      "tags": [
        "Parameters"
      ]
    }, 
    ....
  },
  "source": [
    "var1 = 10",
    "var2 = 'adam'"
 ]