Skip to content

Commit 453533b

Browse files
authored
Merge pull request #1 from randyzwitch/support-styles
Support styles
2 parents 4365b6f + e351040 commit 453533b

File tree

6 files changed

+67
-3
lines changed

6 files changed

+67
-3
lines changed

examples/pages/geojson_styles.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import folium
2+
import geopandas as gpd
3+
import shapely
4+
import streamlit as st
5+
6+
from streamlit_folium import st_folium
7+
8+
st.title("GeoJSON Styling")
9+
10+
START_LOCATION = [37.7934347109497, -122.399077892527]
11+
START_ZOOM = 18
12+
13+
wkt = (
14+
"POLYGON ((-122.399077892527 37.7934347109497, -122.398922660838 "
15+
"37.7934544916178, -122.398980265018 37.7937266504805, -122.399133972495 "
16+
"37.7937070646238, -122.399077892527 37.7934347109497))"
17+
)
18+
polygon_ = shapely.wkt.loads(wkt)
19+
gdf = gpd.GeoDataFrame(geometry=[polygon_]).set_crs(epsg=4326)
20+
21+
style_parcels = {"fillColor": "red", "fillOpacity": 0.2}
22+
23+
polygon_folium = folium.GeoJson(data=gdf, style_function=lambda x: style_parcels)
24+
25+
map = folium.Map(
26+
location=START_LOCATION, zoom_start=START_ZOOM, tiles="OpenStreetMap", max_zoom=21
27+
)
28+
fg = folium.FeatureGroup(name="Parcels")
29+
fg = fg.add_child(polygon_folium)
30+
31+
st_folium(
32+
map,
33+
width=800,
34+
height=450,
35+
feature_group_to_add=fg,
36+
debug=True,
37+
)

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
streamlit>=1.13.0
22
folium>=0.13
33
jinja2
4-
branca
4+
branca
5+
geopandas

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
setuptools.setup(
44
name="streamlit_folium",
5-
version="0.15.0",
5+
version="0.15.1",
66
author="Randy Zwitch",
77
author_email="rzwitch@gmail.com",
88
description="Render Folium objects in Streamlit",

streamlit_folium/__init__.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import branca
1111
import folium
1212
import folium.plugins
13+
import streamlit as st
1314
import streamlit.components.v1 as components
1415
from jinja2 import UndefinedError
1516

@@ -153,6 +154,7 @@ def _get_feature_group_string(
153154
) -> str:
154155
feature_group_to_add._id = "feature_group"
155156
feature_group_to_add.add_to(map)
157+
feature_group_to_add.render()
156158
feature_group_string = generate_leaflet_string(
157159
feature_group_to_add, base_id="feature_group"
158160
)
@@ -180,6 +182,7 @@ def st_folium(
180182
feature_group_to_add: folium.FeatureGroup | None = None,
181183
return_on_hover: bool = False,
182184
use_container_width: bool = False,
185+
debug: bool = False,
183186
):
184187
"""Display a Folium object in Streamlit, returning data as user interacts
185188
with app.
@@ -218,6 +221,9 @@ def st_folium(
218221
use_container_width: bool
219222
If True, set the width of the map to the width of the current container.
220223
This overrides the `width` parameter.
224+
debug: bool
225+
If True, print out the html and javascript code used to render the map with
226+
st.code
221227
Returns
222228
-------
223229
dict
@@ -298,6 +304,17 @@ def bounds_to_dict(bounds_list: List[List[float]]) -> Dict[str, Dict[str, float]
298304
map=folium_map,
299305
)
300306

307+
if debug:
308+
with st.expander("Show generated code"):
309+
st.info("HTML:")
310+
st.code(html)
311+
st.info("Main Map Leaflet js:")
312+
st.code(leaflet)
313+
314+
if feature_group_string is not None:
315+
st.info("Feature group js:")
316+
st.code(feature_group_string)
317+
301318
component_value = _component_func(
302319
script=leaflet,
303320
html=html,

tests/requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ streamlit>1.11.1
22
pytest>=7.1.2
33
folium>=0.13
44
pytest-playwright
5-
pytest-rerunfailures
5+
pytest-rerunfailures
6+
geopandas

tests/test_frontend.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,3 +234,11 @@ def test_responsiveness(page: Page):
234234
assert new_bbox["width"] > initial_bbox["width"] + 300
235235

236236
page.set_viewport_size({"width": 2000, "height": 2000})
237+
238+
239+
def test_geojson_styles(page: Page):
240+
page.get_by_role("link", name="geojson styles").click()
241+
page.get_by_role("link", name="geojson styles").click()
242+
243+
page.get_by_text("Show generated code").click()
244+
expect(page.get_by_text('"fillOpacity"')).to_be_visible()

0 commit comments

Comments
 (0)