Skip to content

Commit

Permalink
Merge pull request #1 from randyzwitch/support-styles
Browse files Browse the repository at this point in the history
Support styles
  • Loading branch information
patrontheo authored Nov 6, 2023
2 parents 4365b6f + e351040 commit 453533b
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 3 deletions.
37 changes: 37 additions & 0 deletions examples/pages/geojson_styles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import folium
import geopandas as gpd
import shapely
import streamlit as st

from streamlit_folium import st_folium

st.title("GeoJSON Styling")

START_LOCATION = [37.7934347109497, -122.399077892527]
START_ZOOM = 18

wkt = (
"POLYGON ((-122.399077892527 37.7934347109497, -122.398922660838 "
"37.7934544916178, -122.398980265018 37.7937266504805, -122.399133972495 "
"37.7937070646238, -122.399077892527 37.7934347109497))"
)
polygon_ = shapely.wkt.loads(wkt)
gdf = gpd.GeoDataFrame(geometry=[polygon_]).set_crs(epsg=4326)

style_parcels = {"fillColor": "red", "fillOpacity": 0.2}

polygon_folium = folium.GeoJson(data=gdf, style_function=lambda x: style_parcels)

map = folium.Map(
location=START_LOCATION, zoom_start=START_ZOOM, tiles="OpenStreetMap", max_zoom=21
)
fg = folium.FeatureGroup(name="Parcels")
fg = fg.add_child(polygon_folium)

st_folium(
map,
width=800,
height=450,
feature_group_to_add=fg,
debug=True,
)
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
streamlit>=1.13.0
folium>=0.13
jinja2
branca
branca
geopandas
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setuptools.setup(
name="streamlit_folium",
version="0.15.0",
version="0.15.1",
author="Randy Zwitch",
author_email="[email protected]",
description="Render Folium objects in Streamlit",
Expand Down
17 changes: 17 additions & 0 deletions streamlit_folium/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import branca
import folium
import folium.plugins
import streamlit as st
import streamlit.components.v1 as components
from jinja2 import UndefinedError

Expand Down Expand Up @@ -153,6 +154,7 @@ def _get_feature_group_string(
) -> str:
feature_group_to_add._id = "feature_group"
feature_group_to_add.add_to(map)
feature_group_to_add.render()
feature_group_string = generate_leaflet_string(
feature_group_to_add, base_id="feature_group"
)
Expand Down Expand Up @@ -180,6 +182,7 @@ def st_folium(
feature_group_to_add: folium.FeatureGroup | None = None,
return_on_hover: bool = False,
use_container_width: bool = False,
debug: bool = False,
):
"""Display a Folium object in Streamlit, returning data as user interacts
with app.
Expand Down Expand Up @@ -218,6 +221,9 @@ def st_folium(
use_container_width: bool
If True, set the width of the map to the width of the current container.
This overrides the `width` parameter.
debug: bool
If True, print out the html and javascript code used to render the map with
st.code
Returns
-------
dict
Expand Down Expand Up @@ -298,6 +304,17 @@ def bounds_to_dict(bounds_list: List[List[float]]) -> Dict[str, Dict[str, float]
map=folium_map,
)

if debug:
with st.expander("Show generated code"):
st.info("HTML:")
st.code(html)
st.info("Main Map Leaflet js:")
st.code(leaflet)

if feature_group_string is not None:
st.info("Feature group js:")
st.code(feature_group_string)

component_value = _component_func(
script=leaflet,
html=html,
Expand Down
3 changes: 2 additions & 1 deletion tests/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ streamlit>1.11.1
pytest>=7.1.2
folium>=0.13
pytest-playwright
pytest-rerunfailures
pytest-rerunfailures
geopandas
8 changes: 8 additions & 0 deletions tests/test_frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,3 +234,11 @@ def test_responsiveness(page: Page):
assert new_bbox["width"] > initial_bbox["width"] + 300

page.set_viewport_size({"width": 2000, "height": 2000})


def test_geojson_styles(page: Page):
page.get_by_role("link", name="geojson styles").click()
page.get_by_role("link", name="geojson styles").click()

page.get_by_text("Show generated code").click()
expect(page.get_by_text('"fillOpacity"')).to_be_visible()

0 comments on commit 453533b

Please sign in to comment.