From 5ce3bd56dbb0eb0bbdbdbb1cf6313c57ca0fbf54 Mon Sep 17 00:00:00 2001 From: Kakarot35 Date: Fri, 29 May 2026 12:12:51 +0530 Subject: [PATCH 1/6] fix: use viewport units for percentage map dimensions (fixes #2186) --- folium/folium.py | 52 ++++++++------- tests/test_issue_2186.py | 141 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 168 insertions(+), 25 deletions(-) create mode 100644 tests/test_issue_2186.py diff --git a/folium/folium.py b/folium/folium.py index e8c000fa8c..7c9b4d48b5 100644 --- a/folium/folium.py +++ b/folium/folium.py @@ -5,8 +5,7 @@ import time import webbrowser -from collections.abc import Sequence -from typing import Any, Optional, Union +from typing import Any, List, Optional, Sequence, Union from branca.element import Element, Figure @@ -64,12 +63,14 @@ class GlobalSwitches(Element): - _template = Template(""" + _template = Template( + """ - """) + """ + ) def __init__(self, no_touch=False, disable_3d=False): super().__init__() @@ -176,15 +177,26 @@ class Map(JSCSSMixin, Evented): """ # noqa - _template = Template(""" + _template = Template( + """ {% macro header(this, kwargs) %} - - - """ - ) + """) def __init__(self, no_touch=False, disable_3d=False): super().__init__() @@ -177,8 +176,7 @@ class Map(JSCSSMixin, Evented): """ # noqa - _template = Template( - """ + _template = Template(""" {% macro header(this, kwargs) %} @@ -252,8 +250,7 @@ class Map(JSCSSMixin, Evented): {%- endif %} {% endmacro %} - """ - ) + """) # use the module variables for backwards compatibility default_js = _default_js @@ -342,7 +339,7 @@ def __init__( **kwargs, ) - self.objects_to_stay_in_front: List[Layer] = [] + self.objects_to_stay_in_front: list[Layer] = [] if isinstance(tiles, TileLayer): self.add_child(tiles) diff --git a/tests/test_issue_2186.py b/tests/test_issue_2186.py index 103bc9ffd8..d225734db5 100644 --- a/tests/test_issue_2186.py +++ b/tests/test_issue_2186.py @@ -30,15 +30,16 @@ def _get_map_css(m: folium.Map) -> str: # Percentage → viewport units # --------------------------------------------------------------------------- + class TestPercentageUsesViewportUnits: def test_height_100_percent_emits_vh(self): """The default height='100%' should use 100vh, not 100%.""" m = folium.Map(location=[0, 0], height="100%") css = _get_map_css(m) assert "100.0vh" in css, f"Expected vh unit, got:\n{css}" - assert "100.0%" not in css.split("position")[1], ( - "Should not emit bare % for height when using viewport units" - ) + assert ( + "100.0%" not in css.split("position")[1] + ), "Should not emit bare % for height when using viewport units" def test_width_100_percent_emits_vw(self): """width='100%' should use 100vw.""" @@ -64,6 +65,7 @@ def test_percentage_height_no_min_height(self): # Pixel values → min-height / min-width guards # --------------------------------------------------------------------------- + class TestPixelValuesGetMinConstraints: def test_pixel_string_height_gets_min_height(self): """height='1000px' should also emit min-height: 1000px.""" @@ -90,6 +92,7 @@ def test_pixel_string_width_gets_min_width(self): # Dead CSS rule removal # --------------------------------------------------------------------------- + class TestDeadMapRuleRemoved: def test_dead_map_id_rule_absent(self): """The stale `#map { position:absolute; ... }` block must not appear.""" @@ -97,15 +100,16 @@ def test_dead_map_id_rule_absent(self): html = m.get_root().render() # The old rule targeted the literal id="map" which never matched the # hashed IDs folium actually generates. - assert "