File size: 5,333 Bytes
ef2f056
 
a7916dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# Copied from https://github.com/randyzwitch/streamlit-folium

"""
This sample code has been copied from
https://github.com/randyzwitch/streamlit-folium/blob/master/examples/streamlit_app.py
"""

import streamlit as st

st.set_page_config(page_title="streamlit-folium documentation")

page = st.sidebar.radio(
    "Select page", ["Home", "Bi-directional data model", "Plugin: Draw"], index=0
)

"# streamlit-folium"

if page == "Home":
    "streamlit-folium integrates two great open-source projects in the Python ecosystem: [Streamlit](https://streamlit.io) and [Folium](https://python-visualization.github.io/folium/)!"

    """
    Currently, there are two functions defined:

    - `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

    - `folium_static()`: takes a folium.Map, folium.Figure, or branca.element.Figure object and displays it in a Streamlit app.

    _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._

    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.
    """


    "---"
    "### Basic `st_folium()` Example"

    with st.echo():

        import streamlit as st
        from streamlit_folium import st_folium
        import folium

        # center on Liberty Bell, add marker
        m = folium.Map(location=[39.949610, -75.150282], zoom_start=16)
        folium.Marker(
            [39.949610, -75.150282],
            popup="Liberty Bell",
            tooltip="Liberty Bell"
        ).add_to(m)

        # call to render Folium map in Streamlit
        st_data = st_folium(m, width = 725)

elif page == "Bi-directional data model":
    """
    On its own, Folium is limited to _display-only_ visualizations; the Folium API generates the proper [leaflet.js](https://leafletjs.com/) specification,
    as HTML and displays it. Some interactivity is provided (depending on how the Folium API is utilized), but the biggest drawback
    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
    provided by the leaflet.js library.

    `streamlit-folium` builds upon the convenient [Folium API](https://python-visualization.github.io/folium/modules.html)
    for building geospatial visualizations by adding a _bi-directional_ data transfer functionality. This not only allows for increased interactivity between
    the web browser and Python, but also the use of larger datasets through intelligent querying.

    ### Bi-directional data model

    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:
    """
    with st.echo():

        import streamlit as st
        from streamlit_folium import st_folium
        import folium

        # center on Liberty Bell, add marker
        m = folium.Map(location=[39.949610, -75.150282], zoom_start=16)
        folium.Marker(
            [39.949610, -75.150282],
            popup="Liberty Bell",
            tooltip="Liberty Bell"
        ).add_to(m)

        # call to render Folium map in Streamlit
        st_data = st_folium(m, width = 725)

    """
    But behind the scenes, a lot more is happening _by default_. The return value of `st_folium` is set to
    `st_data`, and within this Python variable is information about what is being displayed on the screen:
    """

    with st.expander("Expand to see data returned to Python"):
        st_data

    """
    As the user interacts with the data visualization, the values for `bounds` are constantly updating, along
    with `zoom`. With these values available in Python, we can now limit queries based on bounding box, change
    the marker size based on the `zoom` value and much more!
    """

elif page == "Plugin: Draw":

    """
    Folium supports some of the [most popular leaflet plugins](https://python-visualization.github.io/folium/plugins.html). In this example,
    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.

    When a shape is drawn on the map, the coordinates that represent that shape are passed back as a geojson feature via
    the `all_drawings` and `last_active_drawing` data fields.

    Draw something below to see the return value back to Streamlit!
    """

    with st.echo():

        import folium
        import streamlit as st
        from folium.plugins import Draw
        from streamlit_folium import st_folium

        m = folium.Map(location=[39.949610, -75.150282], zoom_start=5)
        Draw(export=True).add_to(m)

        c1, c2 = st.columns(2)
        with c1:
            output = st_folium(m, width = 700, height=500)

        with c2:
            st.write(output)