streamlit-folium Documentation

repository·master·Indexed 20 days ago

https://github.com/randyzwitch/streamlit-folium

A Streamlit component for rendering Folium and Branca objects. It provides st_folium() for bi-directional interactive maps that return interaction data (such as clicks, bounds, and drawings) as a dictionary, and folium_static() for rendering static map displays.

Tokens
1.3K
Snippets
5
Records
6
Agent score
20%

What's inside streamlit-folium

  1. Understand the returned data from st_folium()

    master

    When a user interacts with the map, st_folium() returns a dictionary. The keys in this dictionary represent different types of interactions. Common keys include:

    • last_clicked: Coordinates of the last click.
    • last_object_clicked: The specific Folium object that was clicked.
    • last_object_clicked_count: How many times the object was clicked.
    • last_object_clicked_tooltip: The tooltip of the clicked object.
    • last_object_clicked_popup: The popup content of the clicked object.
    • all_drawings: Data for all drawn shapes.
    • last_active_drawing: The most recent drawing.
    • bounds: The current map bounds in a dictionary format containing _southWest and _northEast lat/lng.
    • zoom: The current zoom level.
    • selected_layers: Information about selected layers.
    • last_geocoder_result: Result from a geocoder interaction.
  2. Use folium_static() for static map displays

    master

    The folium_static() function is used to display a map in a Streamlit app using its _repr_html() representation. This is a simpler, non-interactive way to render maps compared to st_folium().

    It accepts the following object types:

    • folium.Map
    • folium.Figure
    • branca.element.Figure

    This function is useful for testing to ensure your Folium syntax is correct without the overhead of bi-directional component logic.

    # Conceptual usage pattern
    folium_static(your_folium_map_object)
  3. Use st_folium() for bi-directional maps

    master

    The st_folium() function is a bi-directional Streamlit component. It takes a Folium or Branca object and plots it in your Streamlit application.

    Unlike static displays, st_folium() allows for interaction: when a user interacts with the map in the Streamlit app, the function returns a Dict containing selected information, such as the current bounding box and the specific items that were clicked.

    # Conceptual usage pattern
    output = st_folium(your_folium_map_object)
    # output is a Dict containing interaction data like bounding box or clicked items
  4. Render static maps with folium_static()

    master

    The folium_static() function renders a folium.Figure or folium.Map as a static Streamlit component.

    Warning: This method is deprecated and will be removed in a future release. It is recommended to use st_folium() instead, as folium_static() does not pass any interaction data back from the browser to Streamlit.

    Parameters

    • fig: The folium.Map or folium.Figure to render.
    • width: Width of the result.
    • height: Height of the result. Note that if height is already set on the folium.Map or folium.Figure object, that value will supersede this parameter.
    import folium
    from streamlit_folium import folium_static
    
    m = folium.Map(location=[45.5236, -122.6750])
    folium_static(m)
  5. Display interactive maps with st_folium()

    master

    The primary way to use this library is via st_folium(). This function displays a Folium object (like folium.Map or folium.Figure) in a Streamlit app and returns a dictionary containing data from user interactions (e.g., clicks, drawings, or bounds changes).

    Key Parameters

    • fig: The folium.Map or folium.Figure to render.
    • key: An optional unique identifier. If provided, the component's state is tied to this key in st.session_state.
    • returned_objects: A list of specific Folium object keys you want returned. If None, all available interaction data is returned. This is useful for controlling when the app reruns (e.g., only rerun when a specific layer is clicked).
    • zoom / center: Dynamically set the zoom level or center of the map. Note that changing these does not reload the map, but updates it dynamically.
    • feature_group_to_add: A list of folium.FeatureGroup objects to add to the map dynamically without a full reload.
    • return_on_hover: If True, the app reruns when the user hovers over the map. Use with caution as it can impact performance.
    • use_container_width: If True, the map expands to the width of the Streamlit container, overriding the width parameter.
    • layer_control: Pass a folium.LayerControl object to enable dynamic layer management.
    • pixelated: If True, applies CSS to render images with crisp pixels instead of blurring them.
    • wrap_longitude: If True, normalizes longitude values to the -180 to 180 degree range.
    import folium
    from streamlit_folium import st_folium
    
    m = folium.Map(location=[45.5236, -122.6750], zoom_start=13)
    st_folium(m, key="my_map")