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.

Hide code cell content
# Generate some code that we'll use later on in the page
import numpy as np
import matplotlib.pyplot as plt

Hide inputs#

Hide code cell source
# 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)
<matplotlib.image.AxesImage at 0x7ffb143155a0>
../_images/2e09b5c83f33b5a1b2e04862ac96d983edd2807262e3e4c67c8fbd27449c9439.png ../_images/cf852232b7506f1cca53fabfefa700104de366e6f0a37c13b1e467f93885e64f.png

Hide outputs#

# 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)
Hide code cell output
<matplotlib.image.AxesImage at 0x7ffb1428c430>
../_images/f684a94209dc3f307573ee475c05e72154c5f107ad223dfb43de797adb6d12d1.png ../_images/07871c7c5169292d0375cb992fe1dfa4dbef8fddc91fc864298689690f99b6ec.png

Hide markdown#

Note

This is a hidden markdown cell

It should be hidden!

Hide both inputs and outputs#

Hide code cell source
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)
Hide code cell output
<matplotlib.image.AxesImage at 0x7ffb12196890>
../_images/667c245390e862a16200b242ef4770decfbf854055a68fc72623c6047a6975c6.png ../_images/8538d83f4657cce8406ded682e7533e449224e651f8b0a6b25474254fe83b049.png

Hide the whole cell#

Hide code cell content
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)
<matplotlib.image.AxesImage at 0x7ffb10828cd0>
../_images/c8ec130024e4be21c52a26dfb34bcf2387ae1e4002a2c1d66cc38769727233d7.png ../_images/7a26245429e10b6742f710a4705213f8449106314f7954a408f1dddfc64a0c43.png

Enriched outputs#

Math#

# You can also include enriched outputs like Math
from IPython.display import Math
Math("\sum_{i=0}^n i^2 = \frac{(n^2+n)(2n+1)}{6}")
\[\displaystyle \sum_{i=0}^n i^2 = rac{(n^2+n)(2n+1)}{6}\]

Pandas DataFrames#

import pandas as pd
df = pd.DataFrame([['hi', 'there'], ['this', 'is'], ['a', 'DataFrame']], columns=['Word A', 'Word B'])
df
Word A Word B
0 hi there
1 this is
2 a DataFrame

Styled DataFrames (see the Pandas Styling docs).

Hide code cell source
import pandas as pd

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]

df.style.\
    applymap(color_negative_red).\
    apply(highlight_max).\
    set_table_attributes('style="font-size: 10px"')
/tmp/ipykernel_827/645148677.py:27: FutureWarning: Styler.applymap has been deprecated. Use Styler.map instead.
  applymap(color_negative_red).\
  A B C D E
0 1.000000 1.329212 nan -0.316280 -0.990810
1 2.000000 -1.070816 -1.438713 0.564417 0.295722
2 3.000000 -1.626404 0.219565 0.678805 1.889273
3 4.000000 0.961538 0.104011 nan 0.850229
4 5.000000 1.453425 1.057737 0.165562 0.515018
5 6.000000 -1.336936 0.562861 1.392855 -0.063328
6 7.000000 0.121668 1.207603 -0.002040 1.627796
7 8.000000 0.354493 1.037528 -0.385684 0.519818
8 9.000000 1.686583 -1.325963 1.428984 -2.089354
9 10.000000 -0.129820 0.631523 -0.586538 0.290720

Interactive outputs#

Folium#

import folium
m = folium.Map(
    location=[45.372, -121.6972],
    zoom_start=12,
    tiles='Stamen Terrain'
)

folium.Marker(
    location=[45.3288, -121.6625],
    popup='Mt. Hood Meadows',
    icon=folium.Icon(icon='cloud')
).add_to(m)

folium.Marker(
    location=[45.3311, -121.7113],
    popup='Timberline Lodge',
    icon=folium.Icon(color='green')
).add_to(m)

folium.Marker(
    location=[45.3300, -121.6823],
    popup='Some Other Location',
    icon=folium.Icon(color='red', icon='info-sign')
).add_to(m)


m
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[10], line 1
----> 1 m = folium.Map(
      2     location=[45.372, -121.6972],
      3     zoom_start=12,
      4     tiles='Stamen Terrain'
      5 )
      7 folium.Marker(
      8     location=[45.3288, -121.6625],
      9     popup='Mt. Hood Meadows',
     10     icon=folium.Icon(icon='cloud')
     11 ).add_to(m)
     13 folium.Marker(
     14     location=[45.3311, -121.7113],
     15     popup='Timberline Lodge',
     16     icon=folium.Icon(color='green')
     17 ).add_to(m)

File ~/checkouts/readthedocs.org/user_builds/sphinx-book-theme/envs/775/lib/python3.10/site-packages/folium/folium.py:303, in Map.__init__(self, location, width, height, left, top, position, tiles, attr, min_zoom, max_zoom, zoom_start, min_lat, max_lat, min_lon, max_lon, max_bounds, crs, control_scale, prefer_canvas, no_touch, disable_3d, png_enabled, zoom_control, **kwargs)
    301     self.add_child(tiles)
    302 elif tiles:
--> 303     tile_layer = TileLayer(
    304         tiles=tiles, attr=attr, min_zoom=min_zoom, max_zoom=max_zoom
    305     )
    306     self.add_child(tile_layer, name=tile_layer.tile_name)

File ~/checkouts/readthedocs.org/user_builds/sphinx-book-theme/envs/775/lib/python3.10/site-packages/folium/raster_layers.py:145, in TileLayer.__init__(self, tiles, min_zoom, max_zoom, max_native_zoom, attr, detect_retina, name, overlay, control, show, no_wrap, subdomains, tms, opacity, **kwargs)
    143     self.tiles = tiles
    144     if not attr:
--> 145         raise ValueError("Custom tiles must have an attribution.")
    147 self.options = parse_options(
    148     min_zoom=min_zoom,
    149     max_zoom=max_zoom,
   (...)
    157     **kwargs
    158 )

ValueError: Custom tiles must have an attribution.

Stdout#

# The ! causes this to run as a shell command
!jupyter -h

Formatting code cells#

Scrolling cell outputs#

for ii in range(40):
    print(f"this is output line {ii}")

Scrolling cell inputs#

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)