File: hiding.md

package info (click to toggle)
myst-nb 1.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 2,384 kB
  • sloc: python: 6,066; xml: 1,434; makefile: 33
file content (174 lines) | stat: -rw-r--r-- 4,546 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
---
file_format: mystnb
kernelspec:
  name: python3
---

# Hide cell contents

You can use Jupyter Notebook **cell tags** to control some of the behavior of
the rendered notebook.[^download]
If you are using cell tags for the first time, you can read more about them in this tutorial <https://jupyterbook.org/en/stable/content/metadata.html#add-metadata-to-notebooks>

[^download]: This notebook can be downloaded as
            **{nb-download}`hiding.ipynb`** and {download}`hiding.md`

(use/hiding/code)=

## Hide code cells

You can use **cell tags** to control the content hidden with code cells at the cell level.
Add the following tags to a cell's metadata to control
what to hide in code cells:

* **`hide-input`** tag to hide the cell inputs
* **`hide-output`** to hide the cell outputs
* **`hide-cell`** to hide the entire cell

For example, we'll show cells with each below.

```{code-cell} ipython3
:tags: [remove-cell]

import matplotlib.pyplot as plt
import numpy as np
data = np.random.rand(2, 100) * 100
```

Here is a cell with a `hide-input` tag.

```{code-cell} ipython3
:tags: [hide-input]

# This cell has a hide-input tag
fig, ax = plt.subplots()
points =ax.scatter(*data, c=data[0], s=data[0])
```

Here's a cell with a `hide-output` tag:

```{code-cell} ipython3
:tags: [hide-output]

# This cell has a hide-output tag
fig, ax = plt.subplots()
points =ax.scatter(*data, c=data[0], s=data[0])
```

Here's a cell with both `hide-input` and `hide-output` tags:

```{code-cell} ipython3
:tags: [hide-input, hide-output]

# This cell has a hide-output tag
fig, ax = plt.subplots()
points =ax.scatter(*data, c=data[0], s=data[0])
```

Here's a cell with a `hide-cell` tag:

```{code-cell} ipython3
:tags: [hide-cell]

# This cell has a hide-cell tag
fig, ax = plt.subplots()
points =ax.scatter(*data, c=data[0], s=data[0])
```

Finally, a cell with both `remove-input` (see below) and `hide-output` tags:

```{code-cell} ipython3
:tags: [remove-input, hide-output]

fig, ax = plt.subplots()
points = ax.scatter(*data, c=data[0], s=data[0])
```

You can control the hide/show prompts by using the `code_prompt_show` and `code_prompt_hide` configuration options.
The optional `{type}` placeholder will be replaced with `content`, `source`, or `outputs`, depending on the hide tag.
See the {ref}`config/intro` section for more details.

````markdown

```{code-cell} ipython3
:tags: [hide-cell]
:mystnb:
:  code_prompt_show: "My show prompt for {type}"
:  code_prompt_hide: "My hide prompt for {type}"

print("hallo world")
```
````

```{code-cell} ipython3
:tags: [hide-cell]
:mystnb:
:  code_prompt_show: "My show prompt for {type}"
:  code_prompt_hide: "My hide prompt for {type}"

print("hallo world")
```

(use/hiding/markdown)=

## Hide markdown cells

You cannot hide an entire markdown cell, but you can hide sections of markdown **content** by using roles and directives.

For information on how to hide / toggle markdown content in Sphinx, see either [the `sphinx-togglebutton` documentation](https://sphinx-togglebutton.readthedocs.io/en/latest/) or the [`sphinx-design` dropdowns documentation](https://sphinx-design.readthedocs.io/en/latest/dropdowns.html).

(use/removing)=

## Remove parts of cells

Sometimes, you want to entirely remove parts of a cell so that it doesn't make it into the output at all.

To do this at the global level, use the `nb_remove_code_source` or `nb_remove_code_outputs` configuration options, or at a per-file level, e.g.

```yaml
---
mystnb:
  remove_code_source: true
  remove_code_outputs: true
---
```

See the [configuration section](config/intro) for more details.

At a per-cell level you can use the same tag pattern described above,
but with the word `remove_` instead of `hide_`. Use the following tags:

* **`remove-input`** tag to remove the cell inputs
* **`remove-output`** to remove the cell outputs
* **`remove-cell`** to remove the entire cell

+++

Here is a cell with a `remove-input` tag. The inputs will not make it into
the page at all.

```{code-cell} ipython3
:tags: [remove-input]

fig, ax = plt.subplots()
points =ax.scatter(*data, c=data[0], s=data[0])
```

Here's a cell with a `remove-output` tag:

```{code-cell} ipython3
:tags: [remove-output]

fig, ax = plt.subplots()
points = ax.scatter(*data, c=data[0], s=data[0])
```

And the following cell has a `remove-cell` tag (there should be nothing
below, since the cell will be gone).

```{code-cell} ipython3
:tags: [remove-cell]

fig, ax = plt.subplots()
points = ax.scatter(*data, c=data[0], s=data[0])
```