Skip to content

Commit 83eec9e

Browse files
authored
Controlled data return (#89)
* Control which kinds of data are returned (and which trigger reruns) * Convert example app into MPA * Add limited return example * Standardize layout * Bump version * Don't replace functionality of folium_static yet
1 parent 68526ea commit 83eec9e

File tree

9 files changed

+254
-136
lines changed

9 files changed

+254
-136
lines changed

examples/draw_support.py

Lines changed: 0 additions & 18 deletions
This file was deleted.

examples/pages/draw_support.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import streamlit as st
2+
3+
st.set_page_config(
4+
page_title="streamlit-folium documentation: Draw Support",
5+
page_icon=":pencil:",
6+
layout="wide",
7+
)
8+
9+
"""
10+
# streamlit-folium: Draw Support
11+
12+
Folium supports some of the [most popular leaflet plugins](https://python-visualization.github.io/folium/plugins.html). In this example,
13+
we can add the [`Draw`](https://python-visualization.github.io/folium/plugins.html#folium.plugins.Draw) plugin to our map, which allows for drawing geometric shapes on the map.
14+
15+
When a shape is drawn on the map, the coordinates that represent that shape are passed back as a geojson feature via
16+
the `all_drawings` and `last_active_drawing` data fields.
17+
18+
Draw something below to see the return value back to Streamlit!
19+
"""
20+
21+
with st.echo():
22+
23+
import folium
24+
import streamlit as st
25+
from folium.plugins import Draw
26+
27+
from streamlit_folium import st_folium
28+
29+
m = folium.Map(location=[39.949610, -75.150282], zoom_start=5)
30+
Draw(export=True).add_to(m)
31+
32+
c1, c2 = st.columns(2)
33+
with c1:
34+
output = st_folium(m, width=700, height=500)
35+
36+
with c2:
37+
st.write(output)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import streamlit as st
2+
3+
st.set_page_config(
4+
page_title="streamlit-folium documentation: Limit Data Return",
5+
page_icon="🤏",
6+
layout="wide",
7+
)
8+
9+
"""
10+
# streamlit-folium: Limit Data Return
11+
12+
By default, st_folium returns quite a few data fields (zoom, bounds, last active drawing,
13+
all drawings, etc). If you only need a subset of these fields, you can pass a list of
14+
the fields you want returned to the `returned_objects` parameter.
15+
"""
16+
17+
"""
18+
### Example: Only return the last object clicked on the map
19+
20+
Try clicking on the tooltips below. Note that clicking elsewhere on the map, or zooming
21+
or scrolling will not cause the app to rerun.
22+
23+
"""
24+
25+
with st.echo():
26+
import folium
27+
import streamlit as st
28+
29+
from streamlit_folium import st_folium
30+
31+
m = folium.Map(location=[39.949610, -75.150282], zoom_start=13)
32+
33+
folium.Marker(
34+
[39.949610, -75.150282], popup="Liberty Bell", tooltip="Liberty Bell"
35+
).add_to(m)
36+
folium.Marker(
37+
[39.95887, -75.150026],
38+
popup="Independence Hall",
39+
tooltip="Independence Hall",
40+
).add_to(m)
41+
folium.Marker(
42+
[39.965570, -75.180966],
43+
popup="Philadelphia Museum of Art",
44+
tooltip="Philadelphia Museum of Art",
45+
).add_to(m)
46+
47+
c1, c2 = st.columns(2)
48+
with c1:
49+
output = st_folium(
50+
m, width=700, height=500, returned_objects=["last_object_clicked"]
51+
)
52+
53+
with c2:
54+
st.write(output)

examples/pages/static_map.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import streamlit as st
2+
3+
st.set_page_config(
4+
page_title="streamlit-folium documentation: Static Map",
5+
page_icon=":ice:",
6+
layout="wide",
7+
)
8+
9+
"""
10+
# streamlit-folium: Non-interactive Map
11+
12+
If you don't need any data returned from the map, you can just
13+
pass returned_objects=[] to st_folium. The streamlit app will not rerun
14+
when the user interacts with the map, and you will not get any data back from the map.
15+
16+
---
17+
18+
"""
19+
"### Basic `returned_objects=[]` Example"
20+
21+
with st.echo():
22+
23+
import folium
24+
import streamlit as st
25+
26+
from streamlit_folium import st_folium
27+
28+
# center on Liberty Bell, add marker
29+
m = folium.Map(location=[39.949610, -75.150282], zoom_start=16)
30+
folium.Marker(
31+
[39.949610, -75.150282], popup="Liberty Bell", tooltip="Liberty Bell"
32+
).add_to(m)
33+
34+
# call to render Folium map in Streamlit, but don't get any data back
35+
# from the map (so that it won't rerun the app when the user interacts)
36+
st_folium(m, width=725, returned_objects=[])
File renamed without changes.

examples/streamlit_app.py

Lines changed: 32 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,121 +1,68 @@
11
import streamlit as st
2-
import streamlit_folium
32

4-
st.set_page_config(page_title="streamlit-folium documentation")
5-
6-
page = st.sidebar.radio(
7-
"Select page", ["Home", "Bi-directional data model", "Plugin: Draw"], index=0
3+
st.set_page_config(
4+
page_title="streamlit-folium documentation",
5+
page_icon=":world_map:️",
6+
layout="wide",
87
)
98

109
"# streamlit-folium"
1110

12-
if page == "Home":
13-
"streamlit-folium integrates two great open-source projects in the Python ecosystem: [Streamlit](https://streamlit.io) and [Folium](https://python-visualization.github.io/folium/)!"
14-
15-
"""
16-
Currently, there are two functions defined:
11+
"streamlit-folium integrates two great open-source projects in the Python ecosystem: [Streamlit](https://streamlit.io) and [Folium](https://python-visualization.github.io/folium/)!"
1712

18-
- `st_folium()`: a bi-directional Component, taking a Folium/Branca object and plotting to the Streamlit app. Upon mount/interaction with the Streamlit app, st_folium() returns a Dict with selected information including the bounding box and items clicked on
19-
20-
- `folium_static()`: takes a folium.Map, folium.Figure, or branca.element.Figure object and displays it in a Streamlit app.
21-
22-
_Note: `folium_static()` is based on the `_repr_html()` representation created in Folium. This function should be a strict subset the of functionality of the newer `st_folium()` function._
23-
24-
It is recommended that users switch to `st_folium()` as soon as possible, as `folium_static()` will likely be deprecated. If there is a reason why `folium_static()` needs to remain, please leave a [GitHub issue](https://github.com/randyzwitch/streamlit-folium/issues) describing your use-case.
25-
"""
13+
"""
14+
Currently, there are two functions defined:
2615
16+
- `st_folium()`: a bi-directional Component, taking a Folium/Branca object and plotting to the Streamlit app. Upon mount/interaction with the Streamlit app, st_folium() returns a Dict with selected information including the bounding box and items clicked on
2717
28-
"---"
29-
"### Basic `st_folium()` Example"
30-
31-
with st.echo():
32-
33-
import streamlit as st
34-
from streamlit_folium import st_folium
35-
import folium
36-
37-
# center on Liberty Bell, add marker
38-
m = folium.Map(location=[39.949610, -75.150282], zoom_start=16)
39-
folium.Marker(
40-
[39.949610, -75.150282],
41-
popup="Liberty Bell",
42-
tooltip="Liberty Bell"
43-
).add_to(m)
18+
- `folium_static()`: takes a folium.Map, folium.Figure, or branca.element.Figure object and displays it in a Streamlit app.
19+
"""
4420

45-
# call to render Folium map in Streamlit
46-
st_data = st_folium(m, width = 725)
21+
"""
22+
On its own, Folium is limited to _display-only_ visualizations; the Folium API generates the proper [leaflet.js](https://leafletjs.com/) specification,
23+
as HTML and displays it. Some interactivity is provided (depending on how the Folium API is utilized), but the biggest drawback
24+
is that the interactivity from the visualization isn't passed back to Python, and as such, you can't make full use of the functionality
25+
provided by the leaflet.js library.
4726
48-
elif page == "Bi-directional data model":
49-
"""
50-
On its own, Folium is limited to _display-only_ visualizations; the Folium API generates the proper [leaflet.js](https://leafletjs.com/) specification,
51-
as HTML and displays it. Some interactivity is provided (depending on how the Folium API is utilized), but the biggest drawback
52-
is that the interactivity from the visualization isn't passed back to Python, and as such, you can't make full use of the functionality
53-
provided by the leaflet.js library.
27+
`streamlit-folium` builds upon the convenient [Folium API](https://python-visualization.github.io/folium/modules.html)
28+
for building geospatial visualizations by adding a _bi-directional_ data transfer functionality. This not only allows for increased interactivity between
29+
the web browser and Python, but also the use of larger datasets through intelligent querying.
5430
55-
`streamlit-folium` builds upon the convenient [Folium API](https://python-visualization.github.io/folium/modules.html)
56-
for building geospatial visualizations by adding a _bi-directional_ data transfer functionality. This not only allows for increased interactivity between
57-
the web browser and Python, but also the use of larger datasets through intelligent querying.
31+
### Bi-directional data model
32+
"""
33+
left, right = st.columns(2)
5834

59-
### Bi-directional data model
6035

61-
If we take a look at the example from the Home page, it might seem trivial. We define a single point with a marker and pop-up and display it:
36+
with left:
37+
"""
38+
If we take a look at the example from the Home page, it might seem trivial. We define a single point with a marker and pop-up and display it:
6239
"""
6340
with st.echo():
6441

42+
import folium
6543
import streamlit as st
44+
6645
from streamlit_folium import st_folium
67-
import folium
68-
46+
6947
# center on Liberty Bell, add marker
7048
m = folium.Map(location=[39.949610, -75.150282], zoom_start=16)
7149
folium.Marker(
72-
[39.949610, -75.150282],
73-
popup="Liberty Bell",
74-
tooltip="Liberty Bell"
50+
[39.949610, -75.150282], popup="Liberty Bell", tooltip="Liberty Bell"
7551
).add_to(m)
7652

7753
# call to render Folium map in Streamlit
78-
st_data = st_folium(m, width = 725)
54+
st_data = st_folium(m, width=725)
7955

56+
with right:
8057
"""
81-
But behind the scenes, a lot more is happening _by default_. The return value of `st_folium` is set to
58+
But behind the scenes, a lot more is happening _by default_. The return value of `st_folium` is set to
8259
`st_data`, and within this Python variable is information about what is being displayed on the screen:
8360
"""
8461

85-
with st.expander("Expand to see data returned to Python"):
86-
st_data
62+
st_data
8763

8864
"""
8965
As the user interacts with the data visualization, the values for `bounds` are constantly updating, along
9066
with `zoom`. With these values available in Python, we can now limit queries based on bounding box, change
9167
the marker size based on the `zoom` value and much more!
9268
"""
93-
94-
elif page == "Plugin: Draw":
95-
96-
"""
97-
Folium supports some of the [most popular leaflet plugins](https://python-visualization.github.io/folium/plugins.html). In this example,
98-
we can add the [`Draw`](https://python-visualization.github.io/folium/plugins.html#folium.plugins.Draw) plugin to our map, which allows for drawing geometric shapes on the map.
99-
100-
When a shape is drawn on the map, the coordinates that represent that shape are passed back as a geojson feature via
101-
the `all_drawings` and `last_active_drawing` data fields.
102-
103-
Draw something below to see the return value back to Streamlit!
104-
"""
105-
106-
with st.echo():
107-
108-
import folium
109-
import streamlit as st
110-
from folium.plugins import Draw
111-
from streamlit_folium import st_folium
112-
113-
m = folium.Map(location=[39.949610, -75.150282], zoom_start=5)
114-
Draw(export=True).add_to(m)
115-
116-
c1, c2 = st.columns(2)
117-
with c1:
118-
output = st_folium(m, width = 700, height=500)
119-
120-
with c2:
121-
st.write(output)

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.6.15",
5+
version="0.7.0",
66
author="Randy Zwitch",
77
author_email="[email protected]",
88
description="Render Folium objects in Streamlit",

0 commit comments

Comments
 (0)