Skip to content
Draft
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
8 changes: 8 additions & 0 deletions folium/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -972,6 +972,9 @@ class TopoJson(JSCSSMixin, Layer):
tooltip: GeoJsonTooltip, Tooltip or str, default None
Display a text when hovering over the object. Can utilize the data,
see folium.GeoJsonTooltip for info on how to do that.
popup: GeoJsonPopup, Popup or str, default None
Show a popup when clicking on the object. Pass a GeoJsonPopup to show
a different popup for each feature.

Examples
--------
Expand Down Expand Up @@ -1032,6 +1035,7 @@ def __init__(
show: bool = True,
smooth_factor: Optional[float] = None,
tooltip: Union[str, Tooltip, None] = None,
popup: Union[str, Popup, None] = None,
):
super().__init__(name=name, overlay=overlay, control=control, show=show)
self._name = "TopoJson"
Expand Down Expand Up @@ -1059,6 +1063,10 @@ def __init__(
self.add_child(tooltip)
elif tooltip is not None:
self.add_child(Tooltip(tooltip))
if isinstance(popup, (GeoJsonPopup, Popup)):
self.add_child(popup)
elif popup is not None:
self.add_child(Popup(popup))

def style_data(self) -> None:
"""Applies self.style_function to each feature of self.data."""
Expand Down
27 changes: 27 additions & 0 deletions tests/test_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,33 @@ def test_topojson_non_collection_geometry_detail(detail_type):
assert "name" in map_.get_root().render()


@pytest.mark.parametrize(
"popup", [folium.GeoJsonPopup(fields=["name"]), Popup("hello"), "hello"]
)
def test_topojson_popup(popup):
"""TopoJson accepts a popup, like GeoJson does."""
topology = {
"type": "Topology",
"objects": {
"shape": {
"type": "Polygon",
"arcs": [[0]],
"properties": {"name": "A"},
}
},
"arcs": [[[0, 0], [1, 0], [0, 1], [-1, -1]]],
"transform": {"scale": [1, 1], "translate": [0, 0]},
}

map_ = folium.Map()
topojson = folium.TopoJson(topology, "objects.shape", popup=popup).add_to(map_)

children = list(topojson._children.values())
assert len(children) == 1
assert isinstance(children[0], (folium.GeoJsonPopup, Popup))
assert f"{topojson.get_name()}.bindPopup(" in map_.get_root().render()


@pytest.fixture
def tmpl():
yield ("""
Expand Down
Loading