Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for customizing tooltip #366

Merged
merged 1 commit into from
Feb 15, 2023
Merged
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
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