Skip to content

Commit 8fc5e0a

Browse files
adarshsmclaudehansthen
authored
Add a popup argument to TopoJson (#2307)
TopoJson takes a tooltip but no popup, so per-feature popups need a separate GeoJsonPopup(...).add_to(topojson) call. Accept popup the same way tooltip is accepted: a GeoJsonPopup or Popup is added as a child and a string is wrapped in a Popup. Closes #1461 Co-authored-by: adarshsm <24850536+adarshsm@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Hans Then <hans.then@gmail.com>
1 parent f24d3eb commit 8fc5e0a

2 files changed

Lines changed: 33 additions & 0 deletions

File tree

‎folium/features.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -971,6 +971,9 @@ class TopoJson(JSCSSMixin, Layer):
971971
tooltip: GeoJsonTooltip, Tooltip or str, default None
972972
Display a text when hovering over the object. Can utilize the data,
973973
see folium.GeoJsonTooltip for info on how to do that.
974+
popup: GeoJsonPopup, Popup or str, default None
975+
Show a popup when clicking on the object. Pass a GeoJsonPopup to show
976+
a different popup for each feature.
974977
975978
Examples
976979
--------
@@ -1031,6 +1034,7 @@ def __init__(
10311034
show: bool = True,
10321035
smooth_factor: float | None = None,
10331036
tooltip: str | Tooltip | None = None,
1037+
popup: str | Tooltip | None = None,
10341038
):
10351039
super().__init__(name=name, overlay=overlay, control=control, show=show)
10361040
self._name = "TopoJson"
@@ -1058,6 +1062,10 @@ def __init__(
10581062
self.add_child(tooltip)
10591063
elif tooltip is not None:
10601064
self.add_child(Tooltip(tooltip))
1065+
if isinstance(popup, (GeoJsonPopup, Popup)):
1066+
self.add_child(popup)
1067+
elif popup is not None:
1068+
self.add_child(Popup(popup))
10611069

10621070
def style_data(self) -> None:
10631071
"""Applies self.style_function to each feature of self.data."""

‎tests/test_features.py‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,31 @@ def test_topojson_non_collection_geometry_detail(detail_type):
8686
assert "name" in map_.get_root().render()
8787

8888

89+
@pytest.mark.parametrize(
90+
"popup", [folium.GeoJsonPopup(fields=["name"]), Popup("hello"), "hello"]
91+
)
92+
def test_topojson_popup(popup):
93+
"""TopoJson accepts a popup, like GeoJson does."""
94+
topology = {
95+
"type": "Topology",
96+
"objects": {
97+
"shape": {
98+
"type": "Polygon",
99+
"arcs": [[0]],
100+
"properties": {"name": "A"},
101+
}
102+
},
103+
"arcs": [[[0, 0], [1, 0], [0, 1], [-1, -1]]],
104+
"transform": {"scale": [1, 1], "translate": [0, 0]},
105+
}
106+
107+
map_ = folium.Map()
108+
topojson = folium.TopoJson(topology, "objects.shape", popup=popup).add_to(map_)
109+
110+
children = list(topojson._children.values())
111+
assert len(children) == 1
112+
assert isinstance(children[0], (folium.GeoJsonPopup, Popup))
113+
assert f"{topojson.get_name()}.bindPopup(" in map_.get_root().render()
89114
@pytest.mark.parametrize("detail_type", [folium.GeoJsonPopup, folium.GeoJsonTooltip])
90115
@pytest.mark.parametrize(
91116
"properties, fields",

0 commit comments

Comments
 (0)