MappView

Manual/Guides

Python Console

The Python Console runs Python right inside the app, against the map you are looking at. Open it from Processing → Python Console. It docks as a resizable panel at the bottom of the window (drag its top edge to resize, and the button to close it) — it does not block the rest of the app.

Python runs in your browser via Pyodide (CPython compiled to WebAssembly), so it works in both the web and desktop builds with nothing to install. The console exposes a single object, mappview, that drives the live app — its methods mirror the mappview Python package, so what you learn here transfers to notebooks and back.

First run

The Python runtime downloads the first time you open the console (a one-time download of a few MB, shown as a progress message in the header). After that it stays warm, and your variables persist as long as the app is open — even if you close and reopen the panel.

Type code in the input box and press Ctrl/Cmd + Enter (or click Run) to execute it. Output, return values, and errors appear in the scrollback above.

Editing shortcuts

KeyAction
Ctrl/Cmd + EnterRun the current code
EnterNewline (multi-line editing)
↑ / ↓Recall previous / next command (when the caret is on the first / last line)
Tab or Ctrl + SpaceAutocomplete the name or attribute at the caret

Autocomplete introspects the live runtime, so mappview. lists its real methods, and your own variables and any imported modules complete too. When more than one candidate matches, a list appears — use ↑/↓ to choose and Enter/Tab to accept, or Esc to dismiss.

mappview.get_center()        # [lng, lat] of the current view
mappview.get_bounds()        # [west, south, east, north]
mappview.fly_to(-122.4, 37.8, zoom=10)

Script editor

For multi-line scripts you want to keep, click Show editor (the panel icon in the header) to open a script editor beside the console — like QGIS’s Python editor. Drag the divider to resize it, and click the icon again to hide it.

The editor shares the console’s interpreter, so a variable or function you define in a script is immediately usable in the console, and vice-versa.

Driving the map

# Add data (a GeoJSON dict, a geometry, or anything with __geo_interface__)
layer_id = mappview.add_geojson(
    {"type": "Point", "coordinates": [-122.4, 37.8]}, name="Pin"
)

# Style, toggle, and inspect layers
layer = mappview.get_layer(layer_id)
layer.opacity = 0.6
layer.visible = False
layer.set_style(circleRadius=8, fillColor="#ff0000")

for layer in mappview.layers:
    print(layer.name, layer.type, layer.visible)

# Identify features at a point (like clicking the map)
hits = mappview.identify(-122.4, 37.8)

Async operations

The console supports top-level await. A few operations are asynchronous and must be awaited:

# Fetch a remote GeoJSON URL and add it
await mappview.load_geojson("https://example.com/data.geojson", name="Data")

# Run a processing algorithm; result layers are added to the map
mappview.list_algorithms()            # discover ids + parameters
result = await mappview.run_algorithm("buffer", {"layer": layer_id, "distance": 1000})
print(result["logs"], result["resultLayerIds"])

Loading more packages

To keep startup fast, only the base runtime loads up front. Pull in additional packages on demand:

await mappview.load_package("numpy")
import numpy as np
np.array([1, 2, 3]).mean()

await mappview.load_package("geopandas")   # also pulls shapely/pyproj/pandas

Any package in the Pyodide distribution is available this way.

mappview API reference

MethodDescription
get_view()Live camera {center, zoom, bearing, pitch, bbox}.
get_center()Live map center [lng, lat].
get_bounds()Live viewport bounds [west, south, east, north].
fly_to(lng, lat, zoom=, bearing=, pitch=, duration=)Animate the camera; only the given fields change.
fit_bounds([w, s, e, n])Fit the camera to a bounding box.
set_basemap(url)Set the basemap style (an http(s) or root-relative URL).
identify(lng, lat, layer_id=None)Query rendered features at a point (like a click).
add_geojson(data, name=)Add a layer from a GeoJSON dict / geometry / __geo_interface__; returns the layer id.
await load_geojson(url, name=)Fetch a GeoJSON URL and add it; returns the layer id.
layersList of Layer objects, in draw order.
get_layer(layer_id)The Layer with that id (raises if absent).
remove_layer(layer_id)Remove a layer by id.
list_algorithms()Available processing algorithms (id, name, group, parameters).
await run_algorithm(id, parameters=None)Run an algorithm; adds result layers and returns {logs, resultLayerIds}.
to_image()Capture the current map as PNG bytes.
await load_package(name)Load a Pyodide package on demand.

Layer

A handle returned by mappview.layers / mappview.get_layer(...).

MemberDescription
id, name, typeIdentity (read-only).
visibleGet/set visibility.
opacityGet/set opacity (0–1).
set_style(**style)Merge style overrides (e.g. fillColor="#ff0000").
get_features()The layer’s features as Feature objects.
zoom_to()Fit the camera to the layer’s extent.
remove()Remove the layer from the map.

Feature

A GeoJSON feature returned by Layer.get_features(). It is a plain dict (so it serializes and feeds into geopandas.GeoDataFrame.from_features), with convenience accessors .geometry, .properties, .id, and __geo_interface__.

Notes & limitations