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

Add streamlit support for lonboard #600

Merged
merged 1 commit into from
Nov 12, 2023
Merged
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
55 changes: 51 additions & 4 deletions leafmap/deckgl.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def __init__(
height: int = 600,
layers: List = [],
show_tooltip: bool = True,
**kwargs
**kwargs,
) -> None:
"""Initialize a Map object.

Expand Down Expand Up @@ -57,7 +57,7 @@ def add_gdf(
gdf: gpd.GeoDataFrame,
zoom_to_layer: bool = True,
pickable: bool = True,
**kwargs: Any
**kwargs: Any,
) -> None:
"""Adds a GeoPandas GeoDataFrame to the map.

Expand Down Expand Up @@ -104,7 +104,7 @@ def add_vector(
zoom_to_layer: bool = True,
pickable: bool = True,
open_args: dict = {},
**kwargs: Any
**kwargs: Any,
) -> None:
"""Adds a vector layer to the map.

Expand All @@ -130,7 +130,7 @@ def add_layer(
layer: Any,
zoom_to_layer: bool = True,
pickable: bool = True,
**kwargs: Any
**kwargs: Any,
) -> None:
"""Adds a layer to the map.

Expand All @@ -157,3 +157,50 @@ def add_layer(
self.add_vector(
layer, zoom_to_layer=zoom_to_layer, pickable=pickable, **kwargs
)

def to_html(self, filename: Optional[str] = None) -> None:
"""Saves the map as an HTML file.

Args:
filename (Optional[str], optional): The output file path to the HTML file. Defaults to None.

Returns:
str: The HTML content if filename is None.
"""

if filename is None:
filename = temp_file_path("html")
super().to_html(filename)
with open(filename) as f:
html = f.read()
return html
else:
super().to_html(filename)

def to_streamlit(
self,
width: Optional[int] = None,
height: Optional[int] = 600,
scrolling: Optional[bool] = False,
**kwargs,
):
"""Renders `deckgl.Map`in a Streamlit app. This method is a static Streamlit Component, meaning, no information is passed back from Leaflet on browser interaction.

Args:
width (int, optional): Width of the map. Defaults to None.
height (int, optional): Height of the map. Defaults to 600.
scrolling (bool, optional): Whether to allow the map to scroll. Defaults to False.

Returns:
streamlit.components: components.html object.
"""

try:
import streamlit.components.v1 as components

return components.html(
self.to_html(), width=width, height=height, scrolling=scrolling
)

except Exception as e:
raise e