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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
|
---
jupytext:
formats: ipynb,md:myst
text_representation:
extension: .md
format_name: myst
format_version: 0.12
jupytext_version: 1.8.2
kernelspec:
display_name: Python 3
language: python
name: python3
---
# Jupyter notebooks
This is a page to demonstrate the look and feel of Jupyter Notebook elements.
## Hiding elements
### Hide cells
The following cell is hidden.
It also has a `thebe-init` tag which means it will be executed when you initialize Thebe.
```{code-cell} ipython3
:tags: [hide-cell, thebe-init]
# Generate some code that we'll use later on in the page
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(0)
```
### Hide inputs
```{code-cell} ipython3
:tags: [hide-input]
# Hide input
square = np.random.randn(100, 100)
wide = np.random.randn(100, 1000)
fig, ax = plt.subplots()
ax.imshow(square)
fig, ax = plt.subplots()
ax.imshow(wide)
plt.show()
```
### Hide outputs
```{code-cell} ipython3
:tags: [hide-output]
# Hide output
square = np.random.randn(100, 100)
wide = np.random.randn(100, 1000)
fig, ax = plt.subplots()
ax.imshow(square)
fig, ax = plt.subplots()
ax.imshow(wide)
plt.show()
```
### Hide markdown
````{toggle}
```{note}
This is a hidden markdown cell
It should be hidden!
```
````
```{admonition} And here's a toggleable note
:class: dropdown
With a body!
```
+++
### Hide both inputs and outputs
```{code-cell} ipython3
:tags: [hide-output, hide-input]
square = np.random.randn(100, 100)
wide = np.random.randn(100, 1000)
fig, ax = plt.subplots()
ax.imshow(square)
fig, ax = plt.subplots()
ax.imshow(wide)
plt.show()
```
### Hide the whole cell
```{code-cell} ipython3
:tags: [hide-cell]
square = np.random.randn(100, 100)
wide = np.random.randn(100, 1000)
fig, ax = plt.subplots()
ax.imshow(square)
fig, ax = plt.subplots()
ax.imshow(wide)
plt.show()
```
## Enriched outputs
### Math
```{code-cell} ipython3
# You can also include enriched outputs like Math
from IPython.display import Math
Math(r"\sum_{i=0}^n i^2 = \frac{(n^2+n)(2n+1)}{6}")
```
### Pandas DataFrames
```{code-cell} ipython3
import pandas as pd
df = pd.DataFrame([['hi', 'there'], ['this', 'is'], ['a', 'DataFrame']], columns=['Word A', 'Word B'])
df
```
Styled DataFrames (see [the Pandas Styling docs](https://pandas.pydata.org/pandas-docs/stable/user_guide/style.html)).
```{code-cell} ipython3
:tags: [hide-input]
import pandas as pd
from pandas.io.formats.style import Styler
np.random.seed(24)
df = pd.DataFrame({'A': np.linspace(1, 10, 10)})
df = pd.concat([df, pd.DataFrame(np.random.randn(10, 4), columns=list('BCDE'))],
axis=1)
df.iloc[3, 3] = np.nan
df.iloc[0, 2] = np.nan
def color_negative_red(val):
"""
Takes a scalar and returns a string with
the css property `'color: red'` for negative
strings, black otherwise.
"""
color = 'red' if val < 0 else 'black'
return 'color: %s' % color
def highlight_max(s):
'''
highlight the maximum in a Series yellow.
'''
is_max = s == s.max()
return ['background-color: yellow' if v else '' for v in is_max]
Styler(df, uuid="1").\
map(color_negative_red).\
apply(highlight_max).\
set_table_attributes('style="font-size: 10px"')
```
## Interactive outputs
## Stdout
```{code-cell} ipython3
# The ! causes this to run as a shell command
!jupyter -h
```
## Formatting code cells
### Scrolling cell outputs
```{code-cell} ipython3
:tags: [output_scroll]
for ii in range(40):
print(f"this is output line {ii}")
```
### Scrolling cell inputs
```{code-cell} ipython3
:tags: [scroll-input]
b = "This line has no meaning"
b = "This line has no meaning"
b = "This line has no meaning"
b = "This line has no meaning"
b = "This line has no meaning"
b = "This line has no meaning"
b = "This line has no meaning"
b = "This line has no meaning"
b = "This line has no meaning"
b = "This line has no meaning"
b = "This line has no meaning"
b = "This line has no meaning"
b = "This line has no meaning"
b = "This line has no meaning"
b = "This line has no meaning"
b = "This line has no meaning"
b = "This line has no meaning"
b = "This line has no meaning"
b = "This line has no meaning"
b = "This line has no meaning"
b = "This line has no meaning"
print(b)
```
|