Skip to content

Commit

Permalink
Added support for customizing tooltip opengeos#365 (opengeos#366)
Browse files Browse the repository at this point in the history
  • Loading branch information
giswqs authored Feb 15, 2023
1 parent 54147a9 commit cc9acd9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
7 changes: 6 additions & 1 deletion leafmap/foliumap.py
Original file line number Diff line number Diff line change
Expand Up @@ -1235,7 +1235,11 @@ def random_color(feature):
tooltip = None
popup = None
if info_mode is not None:
props = list(data["features"][0]["properties"].keys())
if "fields" in kwargs:
props = kwargs["fields"]
kwargs.pop("fields")
else:
props = list(data["features"][0]["properties"].keys())
if info_mode == "on_hover":
tooltip = folium.GeoJsonTooltip(fields=props)
elif info_mode == "on_click":
Expand Down Expand Up @@ -2435,6 +2439,7 @@ def add_data(
highlight_function = lambda feat: {"fillColor": feat["properties"]["color"]}
info_mode (str, optional): Displays the attributes by either on_hover or on_click. Any value other than "on_hover" or "on_click" will be treated as None. Defaults to "on_hover".
encoding (str, optional): The encoding of the GeoJSON file. Defaults to "utf-8".
**kwargs: Additional keyword arguments to pass to the GeoJSON class, such as fields, which can be a list of column names to be included in the popup.
"""

import warnings
Expand Down
22 changes: 19 additions & 3 deletions leafmap/leafmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -2137,6 +2137,7 @@ def add_geojson(
fill_colors=["black"],
info_mode="on_hover",
encoding="utf-8",
**kwargs,
):
"""Adds a GeoJSON file to the map.
Expand Down Expand Up @@ -2276,12 +2277,23 @@ def close_btn_click(change):

close_button.observe(close_btn_click, "value")

def update_html(feature, **kwargs):
if "fields" in kwargs:
fields = kwargs["fields"]
kwargs.pop("fields")
else:
fields = None

def update_html(feature, fields=fields, **kwargs):

if fields is None:
fields = list(feature["properties"].keys())
if "style" in fields:
fields.remove("style")

value = [
"<b>{}: </b>{}<br>".format(prop, feature["properties"][prop])
for prop in feature["properties"].keys()
][:-1]
for prop in fields
]

value = """{}""".format("".join(value))
html.value = value
Expand Down Expand Up @@ -2362,6 +2374,7 @@ def add_gdf(
info_mode="on_hover",
zoom_to_layer=True,
encoding="utf-8",
**kwargs
):
"""Adds a GeoDataFrame to the map.
Expand All @@ -2388,6 +2401,7 @@ def add_gdf(
fill_colors,
info_mode,
encoding,
**kwargs,
)

if zoom_to_layer:
Expand Down Expand Up @@ -3549,6 +3563,8 @@ def add_data(
style_callback = lambda feat: {"fillColor": feat["properties"]["color"]}
info_mode (str, optional): Displays the attributes by either on_hover or on_click. Any value other than "on_hover" or "on_click" will be treated as None. Defaults to "on_hover".
encoding (str, optional): The encoding of the GeoJSON file. Defaults to "utf-8".
**kwargs: Additional keyword arguments to pass to the GeoJSON class, such as fields, which can be a list of column names to be included in the popup.
"""

gdf, legend_dict = classify(
Expand Down

0 comments on commit cc9acd9

Please sign in to comment.