I found that, in jupyter notebook, there is a tag tool over the cell, which can be activated by "View - Cell Toolbar - Tags". But I can not figure out, why we need these tags. Can someone give some suggestions or usage examples?
-
1For people looking for tags in JupyterLab: it was added in [v2.0](https://jupyterlab.readthedocs.io/en/stable/getting_started/changelog.html#v2-0-0) (at the Release Candidate stage as of March 2020). Prior to that (JupyterLab ≤ 1.x), the [jupyterlab-celltags](https://github.com/jupyterlab/jupyterlab-celltags/) was needed. – Pierre H. Mar 05 '20 at 12:28
-
One usage of tags is explained in https://stackoverflow.com/a/48084050/2822346: how to hide specific cells using tags and `nbconvert`. – Pierre H. Mar 05 '20 at 12:30
4 Answers
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.

- 2,038
- 2
- 22
- 28
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'"
]

- 701
- 7
- 6
Another usage is for papermill.
Papermill is a useful tool you can use to run notebooks from the CLI (as part of a script or from CRON or such).
Papermill uses tags to be able to parametrize a notebook so you can have some global parameters in your notebook and then run that notebook multiple times with different parameter values automatically
source: https://papermill.readthedocs.io/en/latest/usage-parameterize.html

- 11
- 2