A blue spiral line plotted on a polar axis with gridlines at 0.5, 1.0, 1.5, and 2.0

Let’s make something Shiny and Interactive

Interactive maps and plots in Quarto with ipyleaflet and matplotlib, straight from a Jupyter kernel.
tech
python
jupyter
quarto
visualization
Author

Carol Willing

Published

June 4, 2023

Quarto can run Python cells through a Jupyter kernel and render the results right into a post. Here are three small experiments: an interactive map, a polar plot, and a widget. The code below is shown as it was run; the outputs are captured as images so the page renders without a live kernel.

An interactive map with ipyleaflet

from ipyleaflet import Map, Marker, basemaps, basemap_to_tiles

m = Map(
    basemap=basemap_to_tiles(
        basemaps.NASAGIBS.ModisTerraTrueColorCR, "2017-04-08"
    ),
    center=(52.204793, 360.121558),
    zoom=4,
)
m.add_layer(Marker(location=(52.204793, 360.121558)))
m

In a notebook this produces a pannable, zoomable map with a NASA GIBS true-color basemap and a marker at the center point.

A polar plot with matplotlib

import numpy as np
import matplotlib.pyplot as plt

r = np.arange(0, 2, 0.01)
theta = 2 * np.pi * r
fig, ax = plt.subplots(subplot_kw={"projection": "polar"})
ax.plot(theta, r)
ax.set_rticks([0.5, 1, 1.5, 2])
ax.grid(True)
plt.show()
A blue spiral line plotted on a polar axis with gridlines at 0.5, 1.0, 1.5, and 2.0
Figure 1: A line plot on a polar axis

A widget with ipywidgets

import ipywidgets as widgets

slider = widgets.IntSlider(value=5, max=10)
slider

Widgets need a running kernel to be interactive, so they are best enjoyed in JupyterLab or in a Quarto document rendered with jupyter: python3 while a kernel is available.

Back to top