Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion folium/__init__.py

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes are irrelevant

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
CssLink,
Div,
Element,
Figure,
Html,
IFrame,
JavascriptLink,
Expand All @@ -29,6 +28,7 @@
Vega,
VegaLite,
)
from folium.figure import Figure
from folium.folium import Map
from folium.map import (
FeatureGroup,
Expand Down
12 changes: 12 additions & 0 deletions folium/elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,18 @@ def __init__(self, element_name: str, element_parent_name: str):
self.element_name = element_name
self.element_parent_name = element_parent_name

def render(self, **kwargs):
figure = self.get_root()
assert isinstance(
figure, Figure
), "You cannot render this Element if it is not in a Figure."
script = self._template.module.__dict__.get("script", None)
if script is not None:
figure.script.add_child(
Element(script(self, kwargs)),
name=f"{self.element_name}_add_to_{self.element_parent_name}",
)


class IncludeStatement(MacroElement):
"""Generate an include statement on a class."""
Expand Down
10 changes: 10 additions & 0 deletions folium/figure.py

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes are irrelevant

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"""Folium-specific Figure subclass."""

from branca.element import Figure as BrancaFigure

# Re-export branca Figure for folium maps. Rendering must not clear
# figure.html children that were added directly (e.g. geopandas legends).


class Figure(BrancaFigure):
"""Figure used as the root container for folium maps."""
3 changes: 2 additions & 1 deletion folium/folium.py

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes are irrelevant

Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
from collections.abc import Sequence
from typing import Any, Optional, Union

from branca.element import Element, Figure
from branca.element import Element

from folium.elements import JSCSSMixin
from folium.figure import Figure
from folium.map import Evented, FitBounds, Layer
from folium.raster_layers import TileLayer
from folium.template import Template
Expand Down
14 changes: 13 additions & 1 deletion folium/map.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,18 @@ def __init__(
self.marker = marker
self.icon = icon

def render(self, **kwargs):
figure = self.get_root()
assert isinstance(
figure, Figure
), "You cannot render this Element if it is not in a Figure."
script = self._template.module.__dict__.get("script", None)
if script is not None:
figure.script.add_child(
Element(script(self, kwargs)),
name=f"{self.marker.get_name()}_set_icon",
)

def __init__(
self,
location: Optional[Sequence[float]] = None,
Expand Down Expand Up @@ -557,7 +569,7 @@ def render(self):
f"{self._name} location must be assigned when added directly to map."
)
if self.icon:
self.add_child(self.SetIcon(marker=self, icon=self.icon))
self.add_child(self.SetIcon(marker=self, icon=self.icon), name="set_icon")
super().render()

def set_icon(self, icon):
Expand Down
3 changes: 2 additions & 1 deletion folium/plugins/dual_map.py

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes are irrelevant

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from branca.element import Figure, MacroElement
from branca.element import MacroElement

from folium.elements import EventHandler, JSCSSMixin
from folium.figure import Figure
from folium.folium import Map
from folium.map import LayerControl
from folium.template import Template
Expand Down
24 changes: 24 additions & 0 deletions tests/test_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,3 +290,27 @@ def test_icon_invalid_marker_colors():
pytest.warns(UserWarning, Icon, color="lila")
pytest.warns(UserWarning, Icon, color=42)
pytest.warns(UserWarning, Icon, color=None)


def test_repeated_save_produces_identical_html(tmp_path):
"""Regression test for https://github.com/python-visualization/folium/issues/2237"""
m = Map(location=[40.75, -73.98], zoom_start=13)
locations = [
[40.7829, -73.9654],
[40.7484, -73.9857],
[40.7580, -73.9855],
]
for lat, lon in locations:
Marker([lat, lon], icon=Icon(color="blue", icon="info-sign")).add_to(m)

path1 = tmp_path / "1.html"
path2 = tmp_path / "2.html"
path3 = tmp_path / "3.html"
m.save(path1)
m.save(path2)
m.save(path3)

html1 = path1.read_text()
html2 = path2.read_text()
html3 = path3.read_text()
assert html1 == html2 == html3
Loading