Folium reload map after retrieving new objects based on draw

Hi,

For a hobby project I would like to draw objects based on provided polygon by user. However, I’m facing issues with overwriting the existing geojson/map.

  1. Creating map with draw function works fine.
    m = folium.Map(location=center, zoom_start=zoom)
    Draw(export=True).add_to(m)
    map_data = st_folium(m, center=st.session_state[“center”], zoom=st.session_state[“zoom”],
    width=600, height=600)

  2. Retrieve drawing as WKT and return objects based on polygon works fine.
    if st.button(“Get results”):
    last_drawing = map_data[“last_active_drawing”]
    wkt = shape(last_drawing[‘geometry’]).wkt

    gdf = load_and_prepare_data(wkt)

    m = folium.Map(location=center, zoom_start=zoom)
    popup_fields = [col for col in gdf.columns if col != ‘geometry’]
    folium.GeoJson(
    gdf,
    name=‘geojson’,
    tooltip=folium.GeoJsonTooltip(fields=popup_fields, labels=True, sticky=False),
    ).add_to(m)

    map_data = st_folium(m, center=st.session_state[“center”],zoom=st.session_state[“zoom”],width=600, height=600)

Issue is with the last part. If I press the button a second map is showing up for a few miliseconds (with the correct objects plotted), however I want to overwrite the map that is initially used to draw a polygon (search area).

Struggling with this for hours so help would be really appreciated :slight_smile:

Greets.

you can use a if else statement to render the map then rerun to draw a new map

if st.session_state['put_new_map_boolean']: 
  map_out = st.folium( mymap, key='new map')
else:
  map_out = st.folium(mymap, key='old map')

if st.button('do something'):
  # do something 
  st.session_state['put_new_map_boolean'] = not 
  st.session_state['put_new_map_boolean']
  st.rerun()

I’m using something like this to redraw my map whenever I need a fresh map to use