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

Calling get_geolocation() Multiple times causes duplicate widget error #11

Open
GSeyfried opened this issue Jun 18, 2024 · 1 comment
Open

Comments

@GSeyfried
Copy link

GSeyfried commented Jun 18, 2024

In my app I get the users location off the bat, and store it in a session_state. I also have a button that refreshes the location, but when it's clicked I get error: raise DuplicateWidgetID(
streamlit.errors.DuplicateWidgetID: There are multiple widgets with the same key='getLocation()'.

Init:
loc = get_geolocation(key='init') if loc: st.session_state["latitude"] = loc['coords']['latitude'] st.session_state["longitude"] = loc['coords']['longitude']

Refresh:
if st.button("Refresh Location"): loc = get_geolocation() if loc: st.session_state["latitude"] = loc['coords']['latitude'] st.session_state["longitude"] = loc['coords']['longitude'] st.rerun

In the docs I see that get_geolocation(component_key=None): I can create a unique key from a datetime object, but this seems roundabout. I just want a return of the current location without creating new widget objects. whatever.

@aghasemi
Copy link
Owner

Please check if the following code works:

import streamlit as st, random
from streamlit_js_eval import streamlit_js_eval, get_geolocation
import datetime, time

# Streamlit app
def main():
    loc = get_geolocation() 
    if loc: 
        st.session_state["latitude"] = loc['coords']['latitude'] 
        st.session_state["longitude"] = loc['coords']['longitude']        
        
        
    if st.button("Refresh Location"): 
        loc = get_geolocation(component_key=f"{time.time()}") 
        if loc:     
            st.session_state["latitude"] = loc['coords']['latitude'] 
            st.session_state["longitude"] = loc['coords']['longitude'] 
            st.rerun()
        

main()

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

2 participants