Caching Folium Maps with new st.experimental_memo?

I know basically this thread already exists, but it was closed a long time ago, here: Caching Folium Maps.

One comment I found mentioned the hope this might fix itself with the new, currently experimental cachings, but if so I haven’t figured out how yet.
st.cache, while not throwing an error, also doesn’t seem to speed the app up at all.

What I have is a map being displayed, and the only thing that I have folium return is “last_object_clicked”. Things like zoom and with it the bounding box can also change between runs though, so that might have something to do with it.

Basic code here, albeit stripped down:

def create_map_with_track(GeoDataFrame):
    # again, this is technically pseudo-code,
    # but I hope it is sufficient to convey the gist of this function.
    map = folium.Map(x,y,z)
    for x, y in GeoDataFrame:
        folium.Circle(x, y).add_to(map)
        # the amount of circle markers created here is decently-sized, 
        # around 2000 points for the current dataset. Marker Cluster isn't an option.
   return map

trackdf = geopandas.GeoDataFrame(...)

map_item = create_map_with_track(trackdf)

st_map = st_folium(map_item, key="map", width=1400, returned_objects=["last_object_clicked"])

So how and what in that could I cache, to have the map quickly loaded again after re-runs?


Edit: I realized it was possible to put the entire thing in a form, preventing it from reloading unless I actually wanted it to. I’m still curious how I would go about caching this though, even if it isn’t nearly as dire anymore for my particular use-case.

2 Likes

Hey, I’m facing the same issue here, may I know how did you put everything in a form?

Hello, wow, it’s been a long time since I last looked at this code. But after checking it again:

I think I had issues with a form assigned to a variable, so I used the “with st.form” variant to do it. So:

with st.form(key="smth"):
  ...
  st_folium(...
  ...
2 Likes

wowwwwww it works, amazing! ಠ_ಠ !
Thank you so much!!!

This works like a charm, thank you so much!

Thank you! It worked!