import geopandas
import pandas as pd
import matplotlib.pyplot as plt
import streamlit as st
import folium
from streamlit_folium import st_folium
= geopandas.read_file("https://files.catbox.moe/atzk26.gpkg")
gp_list_gdf_sw
# Filter out instances with no geometry
= gp_list_gdf_sw[~gp_list_gdf_sw['geometry'].is_empty]
gp_list_gdf_sw
# Create a geometry list from the GeoDataFrame
= [[point.xy[1][0], point.xy[0][0]] for point in gp_list_gdf_sw.geometry]
geo_df_list
= folium.Map(
gp_map_tooltip =[50.7, -4.2],
location=8,
zoom_start='openstreetmap',
tiles
)
for i, coordinates in enumerate(geo_df_list):
= gp_map_tooltip.add_child(
gp_map_tooltip
folium.Marker(=coordinates,
location=gp_list_gdf_sw['name'].values[i],
tooltip=folium.Icon(icon="user-md", prefix='fa', color="black")
icon
)
)
= st_folium(gp_map_tooltip)
returned_map_data
st.write(returned_map_data)
17 Bidirectional Inputs - Maps
For maps, we need to use the external streamlit_folium
library, which must be installed via pip
before use - it doesn’t come bundled with Streamlit itself.
17.1 Filtering with the bidirectional Folium Component
When using this component, data is constantly being returned as the map is updated.
Let’s take a look at what is being returned as the map is updated.
17.2 Using the returned data
Let’s get the bounds of the map to filter a dataframe to just contain the points within the area the user has zoomed to.
import geopandas
import pandas as pd
import matplotlib.pyplot as plt
import streamlit as st
import folium
from streamlit_folium import st_folium
= geopandas.read_file(
gp_list_gdf_sw "https://files.catbox.moe/atzk26.gpkg"
)
# Filter out instances with no geometry
= gp_list_gdf_sw[~gp_list_gdf_sw['geometry'].is_empty]
gp_list_gdf_sw
# Create a geometry list from the GeoDataFrame
= [[point.xy[1][0], point.xy[0][0]] for point in gp_list_gdf_sw.geometry]
geo_df_list
= folium.Map(
gp_map_tooltip =[50.7, -4.2],
location=8,
zoom_start='openstreetmap',
tiles
)
for i, coordinates in enumerate(geo_df_list):
= gp_map_tooltip.add_child(
gp_map_tooltip
folium.Marker(=coordinates,
location=gp_list_gdf_sw['name'].values[i],
tooltip=folium.Icon(icon="user-md", prefix='fa', color="black")
icon
)
)
= st_folium(gp_map_tooltip)
returned_map_data
= returned_map_data['bounds']['_southWest']['lng']
xmin = returned_map_data['bounds']['_northEast']['lng']
xmax = returned_map_data['bounds']['_southWest']['lat']
ymin = returned_map_data['bounds']['_northEast']['lat']
ymax = gp_list_gdf_sw.cx[xmin:xmax, ymin:ymax]
gp_list_gdf_filtered
f"Returning data for {len(gp_list_gdf_filtered)} practices")
st.write(
st.dataframe('name', 'address_1', 'postcode', 'Total List Size']]
gp_list_gdf_filtered[[ )