mapframe 0.3.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,236 @@
1
+ Metadata-Version: 2.5
2
+ Name: mapframe
3
+ Version: 0.3.0
4
+ Summary: MapLibre GL JS maps for Jupyter, Voila and Solara: Python publishes the whole map and every view follows it.
5
+ Project-URL: Homepage, https://github.com/dfguerrerom/mapframe
6
+ Project-URL: Repository, https://github.com/dfguerrerom/mapframe
7
+ Project-URL: Issues, https://github.com/dfguerrerom/mapframe/issues
8
+ Project-URL: Changelog, https://github.com/dfguerrerom/mapframe/blob/main/CHANGELOG.md
9
+ Author-email: Daniel Guerrero <dfgm2006@gmail.com>
10
+ License: MIT
11
+ License-File: LICENSE
12
+ License-File: THIRD_PARTY_NOTICES.md
13
+ Keywords: anywidget,jupyter,map,maplibre,solara,voila,widget
14
+ Classifier: Development Status :: 3 - Alpha
15
+ Classifier: Framework :: Jupyter
16
+ Classifier: Intended Audience :: Developers
17
+ Classifier: Intended Audience :: Science/Research
18
+ Classifier: License :: OSI Approved :: MIT License
19
+ Classifier: Programming Language :: Python :: 3
20
+ Classifier: Programming Language :: Python :: 3.10
21
+ Classifier: Programming Language :: Python :: 3.11
22
+ Classifier: Programming Language :: Python :: 3.12
23
+ Classifier: Programming Language :: Python :: 3.13
24
+ Classifier: Programming Language :: Python :: 3.14
25
+ Classifier: Topic :: Scientific/Engineering :: GIS
26
+ Requires-Python: >=3.10
27
+ Requires-Dist: anywidget>=0.9
28
+ Requires-Dist: traitlets
29
+ Provides-Extra: dev
30
+ Requires-Dist: black>=26; extra == 'dev'
31
+ Requires-Dist: commitizen>=3.29; extra == 'dev'
32
+ Requires-Dist: jupyterlab; extra == 'dev'
33
+ Requires-Dist: mypy>=1.10; extra == 'dev'
34
+ Requires-Dist: nox>=2024.4.15; extra == 'dev'
35
+ Requires-Dist: pre-commit>=3.7; extra == 'dev'
36
+ Requires-Dist: pytest; extra == 'dev'
37
+ Requires-Dist: pytest-asyncio; extra == 'dev'
38
+ Requires-Dist: pytest-ipywidgets; extra == 'dev'
39
+ Requires-Dist: ruff>=0.17; extra == 'dev'
40
+ Requires-Dist: voila; extra == 'dev'
41
+ Requires-Dist: xyzservices; extra == 'dev'
42
+ Provides-Extra: test
43
+ Requires-Dist: jupyterlab; extra == 'test'
44
+ Requires-Dist: pytest; extra == 'test'
45
+ Requires-Dist: pytest-asyncio; extra == 'test'
46
+ Requires-Dist: pytest-ipywidgets; extra == 'test'
47
+ Requires-Dist: voila; extra == 'test'
48
+ Requires-Dist: xyzservices; extra == 'test'
49
+ Description-Content-Type: text/markdown
50
+
51
+ # mapframe
52
+
53
+ MapLibre GL JS maps for Jupyter, Voila and Solara. Python describes the whole map and publishes it, and every view in the browser makes its map match.
54
+
55
+ mapframe is an [anywidget](https://anywidget.dev) and depends only on `anywidget` and `traitlets`. MapLibre, PMTiles, Terra Draw and the toolbar icons are bundled in the wheel, so a map loads nothing from a CDN and works offline.
56
+
57
+ ## Install
58
+
59
+ ```bash
60
+ pip install mapframe
61
+ ```
62
+
63
+ It needs Python 3.10 or later. The wheel carries the built JavaScript, so installing it needs no Node.
64
+
65
+ ## Quick start
66
+
67
+ ```python
68
+ import json
69
+
70
+ from mapframe import Basemap, GeoJSONSource, Interaction, Layer, MapState, MapWidget, VectorPaint
71
+
72
+ widget = MapWidget(center=(12.5, 41.9), zoom=4) # center is (lng, lat)
73
+
74
+ cities = {
75
+ "type": "FeatureCollection",
76
+ "features": [
77
+ {
78
+ "type": "Feature",
79
+ "properties": {"name": "Rome"},
80
+ "geometry": {"type": "Point", "coordinates": [12.5, 41.9]},
81
+ },
82
+ ],
83
+ }
84
+ digest = widget.add_payload(json.dumps(cities).encode())
85
+
86
+ state = MapState(
87
+ layers=(
88
+ Layer(
89
+ "cities",
90
+ GeoJSONSource(digest),
91
+ paint=VectorPaint(marker="pin"),
92
+ interaction=Interaction(popup=()), # a click shows every property
93
+ ),
94
+ ),
95
+ basemap=Basemap(
96
+ ("https://tile.openstreetmap.org/{z}/{x}/{y}.png",), "© OpenStreetMap contributors"
97
+ ),
98
+ )
99
+ widget.publish(state)
100
+ widget.on_click(lambda event: print(event.lng, event.lat, event.hits("cities")))
101
+ widget
102
+ ```
103
+
104
+ ## How it works
105
+
106
+ Python owns what the map shows. The browser owns what the user does with it.
107
+
108
+ - **From Python to the browser**, there are no commands such as "add this layer" or "move that one". A `MapState` describes the whole map: the layers, top first, the basemap, the theme, the controls and the compare mode. `publish` sends it, and each view works out what changed and applies it. A view shown later, a re-display or a reloaded page shows the same map.
109
+ - **From the browser to Python**, each view reports what happens there: the camera, clicks, drawn shapes and the loading status of each layer. The widget keeps the latest report and calls your callbacks. It ignores reports that no longer apply, such as a click on a layer that a later publish replaced.
110
+
111
+ States are frozen dataclasses. To change one thing, build a new state and publish it:
112
+
113
+ ```python
114
+ from dataclasses import replace
115
+
116
+ layers = tuple(replace(l, opacity=0.5) if l.id == "cities" else l for l in state.layers)
117
+ state = replace(state, layers=layers)
118
+ widget.publish(state)
119
+ ```
120
+
121
+ A layer keeps its loaded tiles or data while its source, attribution and token stay the same. Changing its visibility, opacity, paint, zoom range or position only restyles it.
122
+
123
+ Many fast changes can never leave the map in a wrong state, since each publish is complete and the views end at the last one. [`examples/random_ops.py`](examples/random_ops.py) tests exactly that.
124
+
125
+ ## Layers and sources
126
+
127
+ A `Layer` has an id, a source and how to show it: `visible`, `opacity`, `min_zoom` and `max_zoom`, `zone` (`"overlay"` layers draw above every `"data"` layer), `paint`, `interaction` and `side` (for compare). The source says where the data comes from:
128
+
129
+ | Source | For | How the data arrives |
130
+ | ------------------------------------- | ------------------------------------ | ------------------------------------------------------------- |
131
+ | `RasterSource(tiles)` | XYZ tiles, a WMS, Earth Engine tiles | the browser fetches the tile URLs |
132
+ | `GeoJSONSource(digest, cluster=None)` | small and medium vector data | the bytes stay in Python and the browser pulls them by digest |
133
+ | `PMTilesSource(url, source_layer)` | large vector data in one archive | the browser reads the parts of the file it needs |
134
+
135
+ **Tile URLs** are `{z}/{x}/{y}` templates. A Leaflet-style `{s}` expands to the subdomains a, b and c and `{r}` is dropped; `tile_urls(template, subdomains)` expands one with other subdomains. A WMS takes MapLibre's `{bbox-epsg-3857}` placeholder in a GetMap URL. Tile servers must send CORS headers, since WebGL cannot draw cross-origin images without them.
136
+
137
+ **GeoJSON payloads** never travel inside the state. `add_payload(data)` keeps the bytes and returns their sha256 digest, which the source names. Each publish drops the payloads its state does not use, so add a payload right before every publish that uses it. Adding the same bytes again is cheap: the digest is the same, so nothing is reloaded. A payload over 64 KB travels gzipped: 100k points take about 2.4 MB instead of 15.5 MB. That keeps a few hundred thousand unclustered points practical as GeoJSON; beyond that, build a PMTiles archive.
138
+
139
+ **Basemaps** are `Basemap(tiles, attribution, max_zoom)`. `Basemap.from_xyz(provider)` builds one from an [xyzservices](https://github.com/geopandas/xyzservices) `TileProvider`, such as `xyz.OpenStreetMap.Mapnik`. It raises `ValueError` for a provider MapLibre cannot draw: a TMS provider, or a URL with a placeholder left unfilled, such as a missing API key.
140
+
141
+ **Vector paint** is `VectorPaint(stroke, fill, fill_opacity, width, point_radius, marker, color_by, class_colors)`. `marker="pin"` draws points as pins, and `color_by` with `class_colors` colours features by the value of a property.
142
+
143
+ **Clusters**: a `GeoJSONSource` with `Cluster(radius, max_zoom)` groups its points into count bubbles, and a click on a bubble zooms in. A clustered source holds only points, and the property `cluster: true` is reserved on it. `publish` raises `ValueError` otherwise, and `check_clusterable(geojson)` runs the same check before the data reaches the map.
144
+
145
+ ## What comes back
146
+
147
+ | What | Where to read it | Notes |
148
+ | ------------ | ------------------------------------------------------------ | ------------------------------------------------------------------- |
149
+ | Camera | `widget.view` (`center`, `zoom`, `bounds`), `on_view(cb)` | reported when a move ends |
150
+ | Clicks | `on_click(cb)` with a `ClickEvent` | the features under the point, or within 3 px of it; no hover events |
151
+ | Layer status | `widget.layer_status`, `on_status(cb)` | `loading`, `ready` or `error`, with the error message |
152
+ | Drawings | `widget.draw.begin(modes, on_change=cb)`, `session.features` | the shapes a user drew or edited |
153
+
154
+ Python can ask for a camera move with `set_center(lng, lat, zoom)` and `fit_bounds(bounds, padding)`, which jump there or fly with `animate=True`. The camera still belongs to the browser, and the next report says where it ended.
155
+
156
+ ## Features
157
+
158
+ - **Popups and tooltips** from a layer's `Interaction(popup=..., tooltip=...)`: `()` shows every property, a tuple names the ones to show, and a mapping gives them labels. Values are inserted as text.
159
+ - **Drawing** with the bundled [Terra Draw](https://github.com/JamesLMilner/terra-draw): polygon, rectangle, circle, line and point tools, plus `select` to edit, in a toolbar on the map. One session at a time; `session.set`, `session.clear` and `session.end` control it from Python.
160
+ - **Swipe compare**: `MapState(compare=True)` shows a second map behind a divider, which you drag or move with the arrow keys. Each layer's `side` (`"left"`, `"right"` or `"both"`) says which map draws it.
161
+ - **Linked maps**: maps with the same `link_group` keep one camera. The browser moves them together, with no round trip to Python.
162
+ - **Globe**: `Controls(globe=True)` adds a button that turns the map into a globe and back, and `set_projection("globe")` does it from Python.
163
+ - **Controls and cursor**: `Controls(zoom, fullscreen, scale, coordinates, globe)`, a highlight marker with `set_marker(lng, lat)` and the pointer with `widget.cursor`.
164
+ - **Insets**: after `set_insets(left=300)`, the map centers and fits inside the part that a side panel leaves free.
165
+ - **Host hooks**: the fullscreen control fills the closest ancestor marked `data-mapframe-fullscreen-root`, and each view sets `--mapframe-controls-*` CSS variables there so overlays can stay clear of the controls. Sources, layers and images the widget owns have ids that start with `mapframe:` or `mapframe-`, and it never touches others.
166
+
167
+ [`state.py`](src/mapframe/state.py) declares and validates the state. The module docstring of [`widget.py`](src/mapframe/widget.py) holds the full contract: layer instances, style ids, the messages between Python and the views, and the rules for the camera, compare, linked maps and drawing.
168
+
169
+ ## Why mapframe
170
+
171
+ mapframe was built for [pysepal](https://github.com/openforis/pysepal), the toolkit for [SEPAL](https://sepal.io) apps, to replace its ipyleaflet map. It knows nothing about SEPAL.
172
+
173
+ **The browser could drift from Python.** ipyleaflet drives the browser with a stream of small commands: detach a layer, move it to another pane, attach it again. Nothing checks the browser against Python. In manual testing, a layer ended up in the wrong place after many fast operations, and only a page refresh fixed it. mapframe publishes the whole state instead, so a view can always rebuild the map from it.
174
+
175
+ **MapLibre GL JS fits the maps these apps show.** It draws with WebGL and handles raster tiles, vector tiles, PMTiles and a style spec, with smooth camera moves. It works with Terra Draw for drawing, and a second map clipped by a divider gives a swipe compare.
176
+
177
+ **It had to run anywhere the apps run**: under `solara run`, Voila and plain Jupyter, with no JavaScript from a CDN, and with tile URLs as plain strings an app can route through a proxy.
178
+
179
+ Other libraries are good projects with other goals:
180
+
181
+ - **ipyleaflet** mirrors Leaflet's objects as widgets and syncs each change as a command. Its `PMTilesLayer` has no `visible` or `opacity`.
182
+ - **anymap-ts** (opengeos) is feature-rich. Its sync replays a growing list of method calls in the browser, the command-stream pattern mapframe avoids.
183
+ - **py-maplibregl** (`maplibre` on PyPI) sends one-off messages and keeps no map state in Python, and draws with MapboxDraw rather than Terra Draw.
184
+ - **leafmap** offers one API over several map backends and shares their sync models.
185
+ - **folium** writes a Leaflet map to an HTML page. It has no channel back to Python, so clicks and drawings never reach the kernel.
186
+ - **lonboard** draws large datasets fast on the GPU with deck.gl, from GeoArrow and GeoParquet.
187
+
188
+ ## Develop
189
+
190
+ Development uses Python 3.10 or later, Node.js with npm, and [nox](https://nox.thea.codes). CI uses Node 22. Use 22.12 or later on that release line to run the JavaScript tests.
191
+
192
+ ```bash
193
+ pip install -e ".[dev]" # build the widget and install the test and lint tools
194
+ pre-commit install # run the hooks on each commit, and check commit messages
195
+ npm run dev # rebuild on change
196
+ ANYWIDGET_HMR=1 jupyter lab # reload the widget without restarting the kernel
197
+ ```
198
+
199
+ `npm run build` writes `src/mapframe/static/widget.js` and `widget.css`, which git ignores. `pip install .` and `pip install -e .` run `npm ci --include=dev` and `npm run build` through `hatch_build.py` on every source or editable build, including when a previous build left assets in the checkout. The hook includes npm development dependencies even under `NODE_ENV=production`. A wheel built from an sdist reuses the files the sdist carries and needs no Node.
200
+
201
+ The draw toolbar uses seven Font Awesome Free icons: polygon `draw-polygon`, rectangle `square`, circle `circle`, line `bezier-curve`, point `location-dot`, select `arrow-pointer` and clear `trash` (`square` and `circle` from the regular set, the others solid). [`THIRD_PARTY_NOTICES.md`](THIRD_PARTY_NOTICES.md) holds the licenses of everything bundled.
202
+
203
+ ### Checks
204
+
205
+ The nox sessions that install the widget rebuild its JavaScript through the package build hook.
206
+
207
+ - `nox -s lint` runs the pre-commit hooks on every file: black, ruff, prettier (TypeScript, JavaScript, CSS, Markdown, JSON and YAML) and codespell.
208
+ - `nox -s typecheck` runs `tsc` and mypy.
209
+ - `nox -s js` runs the Vitest unit tests of `js/test/`.
210
+ - `nox -s tests` runs the Python unit tests on Python 3.10 to 3.14. `nox -s tests-3.12` picks one version.
211
+ - `nox -s browser` runs the Playwright browser tests of `tests/browser/` in Chromium, under the Solara, Voila and JupyterLab runners, with the Jupyter user directories in a temporary folder. Arguments after `--` go to pytest, for example `nox -s browser -- tests/browser/test_view.py`.
212
+ - `nox -s release-check` builds the sdist from the working copies of its Git-tracked build inputs, builds a wheel from that sdist on a PATH with no Node, installs the wheel in a clean environment and runs the offline render test against it. It also checks the bundle inputs and MapLibre’s main and worker source maps against the third-party notices. To save the tested distributions, pass `-- --outdir dist` with an empty or new directory. The check needs network access for PyPI and npm.
213
+
214
+ `nox` alone runs `lint`, `typecheck`, `js` and `tests`. GitHub Actions runs all six on every pull request and every push to `main` (`.github/workflows/tests.yml`).
215
+
216
+ ### Demo
217
+
218
+ `examples/random_ops.py` is a Solara page for testing by hand the bug that started this package, a layer drawn in the wrong place after many fast operations. "Random burst" fires 8 operations (add, remove, move, show or hide, opacity, paint, theme, basemap) with no waiting between them, and the list beside the map shows the layer order and status Python expects. The page uses generated data only, so it works offline.
219
+
220
+ ```bash
221
+ solara run examples/random_ops.py
222
+ ```
223
+
224
+ ## Release
225
+
226
+ The version lives in `pyproject.toml`. Commits follow [Conventional Commits](https://www.conventionalcommits.org), and [commitizen](https://commitizen-tools.github.io/commitizen/) bumps the version from them.
227
+
228
+ 1. Bump the version: `cz bump`. It updates `pyproject.toml`, `src/mapframe/__init__.py`, the version test and `CHANGELOG.md`, commits, and tags `vX.Y.Z`. For the first release, 0.3.0, skip this step: set the date in `CHANGELOG.md` and tag the release commit with `git tag -a v0.3.0 -m v0.3.0`.
229
+ 2. Push the release commit and its tag with `git push origin HEAD` and `git push origin vX.Y.Z`, substituting the release version.
230
+ 3. Publish a GitHub release for the tag. `.github/workflows/release.yml` then runs the full test workflow on the tagged commit, checks the tag against the package version, and publishes the smoke-tested sdist and wheel to PyPI using trusted publishing.
231
+
232
+ Before the first release, add a trusted publisher on PyPI for this repository, with the workflow `release.yml` and the environment `pypi`, and create that environment in the repository settings.
233
+
234
+ ## License
235
+
236
+ MIT, see [`LICENSE`](LICENSE). The bundled JavaScript packages keep their own licenses, listed in [`THIRD_PARTY_NOTICES.md`](THIRD_PARTY_NOTICES.md).
@@ -0,0 +1,14 @@
1
+ mapframe/__init__.py,sha256=XgTj7m1HOMVtAdqK-KCMUmTXx38bQ5DPnSt0A54PKDo,796
2
+ mapframe/_callbacks.py,sha256=vzrvYI_gC-tVUiZ0-CYXUWQ15ziOTIzb4xaUq1Wy0r8,6144
3
+ mapframe/_camera.py,sha256=MpCNM-H5ZtOvmgMNQDg_r3tSR7PxwJH_6NFwyng8XfU,2483
4
+ mapframe/_draw.py,sha256=MTEXkorkimKx-zL1gNbvdsGRY-o3NtSmXGfPH5ViCHE,16260
5
+ mapframe/_events.py,sha256=vnZIbT31IxPn7UAocCR0tdoLOnHAOUHFD17gCZe-iZg,1481
6
+ mapframe/state.py,sha256=u8C7zBn5hQaxnxHbiFzjlcxVNKNl5ND-pc5zgfYdPsE,21902
7
+ mapframe/widget.py,sha256=Qro0dSgPeeIA_62HSDhUB4E7oxoIgx6YcrMZFgK_lLE,71064
8
+ mapframe/static/widget.css,sha256=B8ASwPWtZa3IATtD0p2JLIqjW6NkZo9cDZ2KvR4yn2I,75719
9
+ mapframe/static/widget.js,sha256=7X7Ly8xJ4ALKX0gPy7mk3_TvbEXa9I5-JtuwkHew3gY,1921545
10
+ mapframe-0.3.0.dist-info/METADATA,sha256=KvKo5FihkwYsqgJCQb3yYh8gedlF78KfihOVD3XUQyU,17365
11
+ mapframe-0.3.0.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
12
+ mapframe-0.3.0.dist-info/licenses/LICENSE,sha256=2WjpyKOyngapOQRy2PabcFLRC-EN96JUq5_K5uMp6pY,1068
13
+ mapframe-0.3.0.dist-info/licenses/THIRD_PARTY_NOTICES.md,sha256=8pD710SpKGTi6zB_IPihK2-2FwAl9b_4ExhB3PM7KaM,70490
14
+ mapframe-0.3.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.33.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,17 @@
1
+ MIT License
2
+ Copyright (c) 2026 Daniel Guerrero
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+ The above copyright notice and this permission notice shall be included in all
10
+ copies or substantial portions of the Software.
11
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17
+ SOFTWARE.