Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

st_folium keeps reloading Streamlit page till page crashes #72

Closed
GSkrt opened this issue Jun 23, 2022 · 13 comments
Closed

st_folium keeps reloading Streamlit page till page crashes #72

GSkrt opened this issue Jun 23, 2022 · 13 comments

Comments

@GSkrt
Copy link

GSkrt commented Jun 23, 2022

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 ?

@randyzwitch
Copy link
Owner

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

@PaulShin83
Copy link

PaulShin83 commented Jul 18, 2022

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
Copy link

PaulShin83 commented Jul 19, 2022

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

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.

window.initComponent = (map: any) => {
map.on("click", onMapClick)
map.on("moveend", onMapMove)
for (let key in map._layers) {
let layer = map._layers[key]
layer.on("click", onLayerClick)
}
map.on("draw:created", onDraw)
map.on("draw:edited", onDraw)
map.on("draw:deleted", onDraw)
Streamlit.setFrameHeight()
updateComponentValue(map)
}

@randyzwitch
Copy link
Owner

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

@blackary
Copy link
Collaborator

@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?

@PaulShin83
Copy link

@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()

@blackary
Copy link
Collaborator

@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?

@kevinlinxc
Copy link

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.

@joaoambiente
Copy link

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.

@blackary
Copy link
Collaborator

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

@Berhinj
Copy link

Berhinj commented Nov 17, 2022

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

@tonkolviktor
Copy link

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.

@blackary
Copy link
Collaborator

@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 blackary closed this as completed Jul 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants