streamlit-folium icon indicating copy to clipboard operation
streamlit-folium copied to clipboard

st_folium keeps reloading Streamlit page till page crashes

Open GSkrt opened this issue 2 years ago • 7 comments

Streamlit page keeps reloading every 3 seconds till it crashes. Now I use st_static that is not recommended ?

st_data = st_folium(m, key='map',width = 650, height = 600)

Any idea what is happening ?

GSkrt avatar Jun 23 '22 13:06 GSkrt

What version are you using? Can you also provide the code that creates your m object?

randyzwitch avatar Jun 23 '22 15:06 randyzwitch

I have same issue. Rendering folium using st_folium itself triggers streamlit reloading. So, streamlit reloades the page infinitely. I'm using streamlie 1.11.0, streamlit-folium 0.6.13

Below is my code that creates m object

def draw_folium_map():
    center = [37.541, 126.986]
    tiles = ['cartodbpositron', 'Stamen Toner', 'OpenStreetMap']
    map = folium.Map(
        location = [center[0], center[1]],
        zoom_start = 10,
        zoom_control=   True,
        scrollWheelZoom=False,
        tiles = tiles[0]
    )
    for coord, loc in zip(st.session_state['coordinates'], st.session_state['locations']):
        if coord:
            folium.Marker(
                location=[coord[1], coord[0]],
                popup=loc,
                icon=folium.Icon(color='blue', icon='star')
            ).add_to(map)
    return map

PaulShin83 avatar Jul 18 '22 06:07 PaulShin83

I found this line does not work. windows.map is undefined still after the map has been created. So, it re-renders infinitely.

https://github.com/randyzwitch/streamlit-folium/blob/52c7b88d1a0b64a542699effa18101151bcae383/streamlit_folium/frontend/src/index.tsx#L111

By removing updateComponentValue(map) in window.initComponent, I fixed the problem of my use case. But I dont' know it is applicable for all other cases.

https://github.com/randyzwitch/streamlit-folium/blob/52c7b88d1a0b64a542699effa18101151bcae383/streamlit_folium/frontend/src/index.tsx#L83-L96

PaulShin83 avatar Jul 19 '22 09:07 PaulShin83

Thanks for digging into this Paul, we'll take a look at this to see if it solves the problem more globally!

randyzwitch avatar Jul 19 '22 12:07 randyzwitch

@PaulShin83 I don't see that issue when I fun the following script with streamlit==1.11.0 and streamlit-folium==0.6.13

import streamlit as st
from streamlit_folium import st_folium
import folium

def draw_folium_map():
    center = [39.5, -98.5]
    tiles = ["cartodbpositron", "Stamen Toner", "OpenStreetMap"]
    map = folium.Map(
        location=[center[0], center[1]],
        zoom_start=10,
        zoom_control=True,
        scrollWheelZoom=False,
        tiles=tiles[0],
    )

    folium.Marker(
        location=[39.5, -98.5],
        popup=f"A location!",
        icon=folium.Icon(color="blue", icon="star"),
    ).add_to(map)

    return map


m = draw_folium_map()

output = st_folium(m, key="map", width=650, height=600)

st.write(output)

I know this isn't exactly the same as your script, but I'm unable to run your exact example without the full script. It's possible that the issue you're encountering is related to something happening with session state (e.g. perhaps rendering the map -> updates session state -> rerendering the map -> ...). Would you be able to share the full script, or a complete subset of it that shows the infinite-reload issue?

blackary avatar Jul 19 '22 15:07 blackary

@blackary I found it while I am trying to reproduce the issue that using st.empty with st.columns cuases the problem. Now you can reproduce the rerendering issue with following code.

import streamlit as st
from streamlit_folium import st_folium
import folium

def draw_folium_map():
    center = [39.5, -98.5]
    tiles = ["cartodbpositron", "Stamen Toner", "OpenStreetMap"]
    map = folium.Map(
        location=[center[0], center[1]],
        zoom_start=10,
        zoom_control=True,
        scrollWheelZoom=False,
        tiles=tiles[0],
    )
    folium.Marker(
        location=[39.5, -98.5],
        popup=f"A location!",
        icon=folium.Icon(color="blue", icon="star"),
    ).add_to(map)

    return map


def main():
    map_placeholder = st.empty()

    col1, col2, col3 = st.columns(3)
    # check1 = col1.checkbox("check1", True)

    col4, col5, col6 = st.columns(3)
    # check1 = col4.checkbox("check2", True)

    map = draw_folium_map()
    with map_placeholder.container():
        output = st_folium(map, width=700, height=450, key='foliumMap1')

    st.write(output)


if __name__ == '__main__':
    main()

PaulShin83 avatar Jul 20 '22 07:07 PaulShin83

@PaulShin83 I tried reproducing what you are seeing, and I am not seeing any rerendering issue. Here is a hosted version of your app https://blackary-folium-bug-streamlit-app-rpsil3.streamlitapp.com/. Are you seeing the repeated rerendering there?

blackary avatar Jul 20 '22 14:07 blackary

Sorry I can't provide more detail, but I also had this problem and had it fixed by using the static function, and would like to advocate for it to not be deprecated.

kevinlinxc avatar Oct 24 '22 22:10 kevinlinxc

Streamlit page keeps reloading every 3 seconds till it crashes. Now I use st_static that is not recommended ?

st_data = st_folium(m, key='map',width = 650, height = 600)

Any idea what is happening ?

Same problem here.

joaoambiente avatar Oct 25 '22 17:10 joaoambiente

@joaoambiente Can you share a reproducible code snippet that shows this behavior?

blackary avatar Oct 26 '22 00:10 blackary

I believe I might be facing the same issue, has it been fixed in recent updates ?

Berhinj avatar Nov 17 '22 13:11 Berhinj

How about:

m = folium.Map(location=[47.113007058936485, 9.1385482558749599], zoom_start = 14, control_scale=False)
for i in range(200):
    line_coords = []
    for j in range(20):
        line_coords.append([47.113007058936485 + (i* 0.001), 9.138548255874959 + (j * 0.001)])
    folium.Circle(location=line_coords[-1], radius = 40, color='black', fill_color='black', popup='popupasasfasfs\nasdfasf', tooltip = 'popupasasfasfs\nasdfasf').add_to(m)
    m.add_child(folium.PolyLine(line_coords, color='black', line_weight=1, popup='popupasasfasfs\nasdfasf', tooltip = 'Test looooong toooltip:'))
st_folium(m)

The problem on my end is that loading this much stuff takes time. Which would be ok once, however after each map interaction the same load happens the guy starts runing on the corner, sometimes the map becomes gray...so it's not really end-user ready

Regarding: "If there is a reason why folium_static() needs to remain, please leave a GitHub issue describing your use case." So yeah with folium_static it works.

tonkolviktor avatar Nov 24 '22 21:11 tonkolviktor

@tonkolviktor Thanks for sharing that example code! In this case, it seems to be behaving as expected -- I don't see it reloading over and over again. The "running guy" does trigger, but that is because the map is returning different values. If you want to avoid this, you can pass returned_objects=[] to st_folium, to make it return nothing (and essentially behave like folium_static. But, you can also still have it return some data if you want it to. If you want to avoid the "running guy", the returned_objects=[] will always be available. I may even keep folium_static and simply make it call st_folium with that argument passed in. But, if there are cases where st_folium produces an infinite-rerun, I definitely want to fix those before changing folium_static to work that way.

blackary avatar Dec 21 '22 21:12 blackary