Summary
Iām trying to create a web app that takes location input from the user and then uses googlemaps places_autocomplete function to provide autocomplete suggestions. Iām then using an st.button to have the user select his preferred location.
Iām getting two text_input from the user for both the pick-up location & the drop-off location.
I will then further use(scrape) the places_id to get the Location latitude & longitude.
But the problem is that when the user clicks on letās say a suggestion for pick-up location and then clicks another button for drop-off location, I loose the previous st.button content. Likewise if I click a pick-up st.button, I loose the drop-off button content.
I just get the most current content and the previous st.button content doesnāt persist on page. I donāt want that happening
Steps to reproduce
Code snippet:
import streamlit as st
import googlemaps
import requests
# Create a Google Maps client
api_key = 'Google-Map-API-key'
gmaps = googlemaps.Client(key=api_key)
# -------------
#Place showcase & get Latitude + Longitude
# Update the search input field with the clicked suggestion
def get_location(input, place_id):
#st.text("Pick-up Point:")
st.write(input)
st.text("Latitude, Longitude")
try:
url = f"https://maps.googleapis.com/maps/api/place/details/json?placeid={place_id}&key={api_key}"
response = requests.get(url)
data = response.json()
if data['status'] == 'OK':
latitude = data["result"]["geometry"]["location"]["lat"]
longitude = data["result"]["geometry"]["location"]["lng"]
return st.write([latitude, longitude])
else:
print('Error Occured')
except:
pass
# --------------
# Pick-up point
# Create a search box using Streamlit
search_input = st.text_input("Enter a Pick-up location")
# Call the places_autocomplete function as the user types
if search_input:
predictions = gmaps.places_autocomplete(search_input)
# Display the suggestions
for prediction in predictions:
if st.button(prediction['description']):
search_input = prediction['description']
place_id = prediction['place_id']
st.write(get_location(search_input, place_id))
drop_input = st.text_input("Enter a Drop-off location")
# Call the places_autocomplete function as the user types
if drop_input:
predictions = gmaps.places_autocomplete(drop_input)
# Display the suggestions
for prediction in predictions:
if st.button(prediction['description']):
drop_input = prediction['description']
place_id = prediction['place_id']
st.write(get_location(drop_input, place_id))
If applicable, please provide the steps we should take to reproduce the error or specified behavior.
Expected behavior:
I want the pick-up content to stick/persist on page even if I click on a drop-off button. I want both st.button content to stay on page.
Actual behavior:
Only one st.button content is being displayed on page
I need help on this