Interaction that survives export¶
Notebook callbacks are useful while exploring, but a shared HTML file no longer has a Python kernel behind it. This tutorial therefore focuses on browser-native interaction: feature popups, a year filter, live restyling, measurement, and coordinate inspection.
The project locations and scores are deterministic synthetic data. Rerunning the notebook always produces the same map.
from random import Random
from maplibreum import Map
from maplibreum.controls import LayerColorControl, SliderControl
rng = Random(2026)
categories = ["crossing", "footway", "kerb"]
features = []
for index in range(60):
year = 2022 + index % 5
category = categories[index % len(categories)]
score = rng.randint(35, 98)
features.append({
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [-49.2733 + rng.uniform(-0.045, 0.045), -25.4284 + rng.uniform(-0.035, 0.035)],
},
"properties": {
"name": f"Project {index + 1:02d}",
"category": category,
"year": year,
"score": score,
},
})
projects = {"type": "FeatureCollection", "features": features}
print(f"Prepared {len(features)} projects spanning 2022–2026")
Prepared 60 projects spanning 2022–2026
Compose several interactions¶
The slider applies an exact year filter to the layer. The color control changes paint properties without rebuilding the map. Click a project for its structured popup; use the ruler to measure; click empty map space to inspect coordinates.
interactive = Map(
center=[-49.2733, -25.4284],
zoom=12,
title="Interactive accessibility projects",
height="620px",
)
interactive.add_source("projects", {"type": "geojson", "data": projects})
interactive.add_circle_layer(
"projects",
source="projects",
paint={
"circle-color": [
"match", ["get", "category"],
"crossing", "#e63946",
"footway", "#457b9d",
"kerb", "#2a9d8f",
"#6c757d",
],
"circle-radius": ["interpolate", ["linear"], ["get", "score"], 35, 5, 100, 12],
"circle-opacity": 0.85,
"circle-stroke-color": "#ffffff",
"circle-stroke-width": 1.5,
},
)
interactive.add_feature_popup(
"projects",
fields=["name", "category", "year", "score"],
aliases=["Project", "Type", "Completed", "Impact score"],
title="Accessibility investment",
)
interactive.add_tooltip(layer_id="projects", prop="name")
interactive.add_control(
SliderControl(
layer_ids="projects",
property_name="year",
min_value=2022,
max_value=2026,
step=1,
initial_value=2026,
label="Completed in {value}",
title="Filter by year",
)
)
interactive.add_control(
LayerColorControl(
layers={"projects": "Project markers"},
colors=["#e63946", "#f4a261", "#2a9d8f", "#457b9d", "#6d28d9"],
title="Override layer color",
),
position="top-right",
)
interactive.add_measure_control(position="top-left", units="kilometers")
interactive.add_lat_lng_popup()
interactive.add_control("navigation", options={"showZoom": True})
interactive.add_control("fullscreen")
interactive
Choose the right interaction boundary¶
Use browser-native controls for interactions that must work in exported HTML. Use Python callbacks when the notebook kernel is intentionally part of the application—for example, to run an analysis after a click. Keeping that boundary explicit makes maps much easier to publish and maintain.
Next: stress the same browser renderer with thousands of points and native clustering.