Repository navigation
Add TileLayerColorFilter plugin - #2381
Open
StrikeRougeAgent wants to merge 1 commit into
Open
StrikeRougeAgent wants to merge 1 commit into
StrikeRougeAgent wants to merge 1 commit into
Conversation
Wraps Leaflet.TileLayer.ColorFilter so a tile layer can be painted with CSS filters, which turns a single light basemap into a dark or grayscale one instead of requiring a separate dark tile provider. Refs python-visualization#1635
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds
folium.plugins.TileLayerColorFilter, a thin wrapper around theLeaflet.TileLayer.ColorFilter
plugin, so a tile layer can be painted through CSS filters:
Closes #1635, which asks for dark mode by wrapping exactly this plugin. A CSS
filter covers the wider request in that thread too:
"grayscale:100%"for aneutral background under a choropleth, or a
contrast/saturatetweak on animagery basemap, without a second tile provider.
Files:
folium/plugins/tile_layer_color_filter.py— the plugin classfolium/plugins/__init__.py— exporttests/plugins/test_tile_layer_color_filter.py— 11 testsdocs/user_guide/plugins/tile_layer_color_filter.mdplus itsplugins.rsttoctree entry and table row
Why this path
A CSS filter is the only client-side mechanism that works for any tile source,
and this plugin is the maintained implementation of it (101 stars, v2.0.0
released 2025-09).
L.tileLayer.colorFilter(url, {filter: "..."})factory that only exists forLeaflet 1.x and has had no release since. v2.0.0 is current and still supports
Leaflet 1.x through its
-globalbuild, which is what folium pins(
leaflet@1.9.4). The script is pinned to an exact version and loaded fromjsDelivr; note the npm package name is
leaflet.tilelayer.colorfilter(dots) — there is no
leaflet-tilelayer-colorfilterpackage.TileLayerinstead of emitting a custom factory. v2 patchesL.TileLayer.prototype.onAddand reads thecolorFilterlayer option, so thewrapper can keep folium's stock
L.tileLayer(...)template and only has toget the plugin
<script>into the page header before the map script. That isthe same shape as the existing
HeatMap(JSCSSMixin, Layer)andMarkerCluster(JSCSSMixin, Layer)plugins, and it keeps everyTileLayerargument (
tiles,min_zoom,max_zoom,attr,detect_retina,tms,opacity, controls) working unchanged.filter it cannot parse, so a typo (
"invert:100deg","grey:100") produces amap that looks unfiltered with no error anywhere. The wrapper raises
ValueErrorlisting the valid filter names, accepts the plugin's short names(
inv,gray,hue-rotation, ...) and appends the unit, so the renderedoptions are always canonical. Passing
colorFilter=[...]as aTileLayerkwarg would technically work today, but it still needs the user to add the
script link by hand and still fails silently.
Validation
RED (default branch
dc7bf7c, before the change):folium.plugins.TileLayerColorFilter→AttributeError: module 'folium.plugins' has no attribute 'TileLayerColorFilter'TileLayer(..., colorFilter=["invert:100%", "hue-rotate:180deg"])renders the option but no plugin script, and in a browser the tile container is
class="leaflet-layer " style="z-index: 1; opacity: 1;"— nofilter.GREEN (this branch), same map through
TileLayerColorFilter, rendered inheadless Chromium 156 with Leaflet 1.9.4 from the CDN folium pins and real OSM
tiles:
Screenshots from those two runs: standard light OSM tiles before, dark tiles
(teal water, black land) after, tiles fully loaded in both cases.
Test suite —
python -m pytest --ignore=tests/selenium --ignore=tests/playwright --ignore=tests/snapshots(the Code Tests invocation):
dc7bf7c(before)The 3 failures are
tests/test_repr.py::test__repr_png_is_bytes,::test_valid_png,::test_valid_png_size, which need a Chrome driver and failthe same way on the unmodified branch in this environment. The new tests cover
the rendered
colorFilteroptions, that the plugin script is loaded afterLeaflet, that ordinary
TileLayers get no plugin script and nocolorFilterkey, alias/unit normalisation, and the validation errors.
Lint —
ruff0.17.5,black26.10.1 andcodespell(the exact.pre-commit-config.yamlrevs) all clean on the changed files.Limitations
tests/snapshots,tests/seleniumand the Sphinx/nbsphinx docs build were notrun locally: the first two need a Chrome driver, the docs build needs the full
docs stack. The two docs code cells were executed as plain Python instead.
updateColorFilter()method is not wrapped; filters arefixed at render time. Adding a JS-side updater would need a new folium element
and is out of scope here.
controls drawn on top of them — that is the intended behaviour and is
documented, but it means a "fully dark" map also needs dark control styling.
filtersupport (all currentbrowsers).
docs).