From 344b30f843dd047cf6875b12f41fb44c5047e56b Mon Sep 17 00:00:00 2001 From: Hans Then Date: Wed, 1 Jan 2025 12:45:41 +0100 Subject: [PATCH 1/4] Cleanup make MacroElements from Elements This change involves several classes that require being added to a Figure. Since these classes follow the `render` semantics of a MacroElement it makes more sense to make them also inherit from MacroElement. Excluded from this change is actually making use of the template mechanics of the MacroElement. --- folium/elements.py | 4 ++-- folium/features.py | 9 +++++++-- folium/map.py | 2 +- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/folium/elements.py b/folium/elements.py index aa1dbd8846..2c8cf866c3 100644 --- a/folium/elements.py +++ b/folium/elements.py @@ -1,12 +1,12 @@ from typing import List, Tuple -from branca.element import CssLink, Element, Figure, JavascriptLink, MacroElement +from branca.element import CssLink, Figure, JavascriptLink, MacroElement from folium.template import Template from folium.utilities import JsCode -class JSCSSMixin(Element): +class JSCSSMixin(MacroElement): """Render links to external Javascript and CSS resources.""" default_js: List[Tuple[str, str]] = [] diff --git a/folium/features.py b/folium/features.py index cba87e8eff..a9319dcebf 100644 --- a/folium/features.py +++ b/folium/features.py @@ -101,7 +101,7 @@ def __init__( ) -class Vega(JSCSSMixin, Element): +class Vega(JSCSSMixin): """ Creates a Vega chart element. @@ -193,6 +193,8 @@ def render(self, **kwargs) -> None: name=self.get_name(), ) + # TODO: this could be rewritten to use the facilities of MacroElement + # and the facilities of JSCSSMixin figure = self.get_root() assert isinstance( figure, Figure @@ -224,7 +226,7 @@ def render(self, **kwargs) -> None: ) -class VegaLite(Element): +class VegaLite(MacroElement): """ Creates a Vega-Lite chart element. @@ -297,6 +299,9 @@ def render(self, **kwargs) -> None: name=self.get_name(), ) + # TODO: this could be rewritten to use the facilities of MacroElement + # and the facilities of JSCSSMixin + figure = self.get_root() assert isinstance( figure, Figure diff --git a/folium/map.py b/folium/map.py index 3c5302b8a6..201a89942f 100644 --- a/folium/map.py +++ b/folium/map.py @@ -409,7 +409,7 @@ def render(self) -> None: super().render() -class Popup(Element): +class Popup(MacroElement): """Create a Popup instance that can be linked to a Layer. Parameters From 125f26bcdc768d91a3c805c2ac844a08c30b0843 Mon Sep 17 00:00:00 2001 From: Hans Then Date: Wed, 1 Jan 2025 14:23:46 +0100 Subject: [PATCH 2/4] Fix missing import --- folium/elements.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/folium/elements.py b/folium/elements.py index 2c8cf866c3..198a20794c 100644 --- a/folium/elements.py +++ b/folium/elements.py @@ -1,6 +1,12 @@ from typing import List, Tuple -from branca.element import CssLink, Figure, JavascriptLink, MacroElement +from branca.element import ( + CssLink, + Element, # NoQA: F401 needed as a reexport + Figure, + JavascriptLink, + MacroElement, +) from folium.template import Template from folium.utilities import JsCode From fc1930b1983b7e24a34da2e65a021a8ea1c88629 Mon Sep 17 00:00:00 2001 From: Hans Then Date: Wed, 1 Jan 2025 20:20:18 +0100 Subject: [PATCH 3/4] Update folium/features.py Co-authored-by: Frank Anema <33519926+Conengmo@users.noreply.github.com> --- folium/features.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/folium/features.py b/folium/features.py index a9319dcebf..656edffe80 100644 --- a/folium/features.py +++ b/folium/features.py @@ -193,8 +193,6 @@ def render(self, **kwargs) -> None: name=self.get_name(), ) - # TODO: this could be rewritten to use the facilities of MacroElement - # and the facilities of JSCSSMixin figure = self.get_root() assert isinstance( figure, Figure From 9d4e827829fe9e77cee61fed10e21eb0d64e36d3 Mon Sep 17 00:00:00 2001 From: Hans Then Date: Wed, 1 Jan 2025 20:20:28 +0100 Subject: [PATCH 4/4] Update folium/features.py Co-authored-by: Frank Anema <33519926+Conengmo@users.noreply.github.com> --- folium/features.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/folium/features.py b/folium/features.py index 656edffe80..d3d96caca3 100644 --- a/folium/features.py +++ b/folium/features.py @@ -297,9 +297,6 @@ def render(self, **kwargs) -> None: name=self.get_name(), ) - # TODO: this could be rewritten to use the facilities of MacroElement - # and the facilities of JSCSSMixin - figure = self.get_root() assert isinstance( figure, Figure