Selectbox on_change update list of values

I am trying to build a autocomplete functionality using selectbox. I have a fields where user needs to enter the place of birth and based on user input I need to show the valid selections that can be done by user.

I need to call google api based on the text input by the user and then return the valid selections.
For example if user enters
New
Then they would see the list of cities that have New in them as returned by Google API.
I believe that on_change would allow me to do that but following code is not working for me.

import streamlit as st
import pandas as pd

def get_place():
    print("I am called")
    print("Received option", st.session_state['place'])
st.set_page_config(page_title="Code of Cosmos",layout="wide")
with st.sidebar:
    print("I am called from sidebar")
    native_name = st.text_input("Name")
    native_place = st.selectbox("Place of Birth",options=[],on_change=get_place, key="place")
    native_dob = st.date_input("Date of Birth")
    native_tob = st.time_input("Time of Birth","now")

I expect the function get_place to be called when user starts typing in the selectbox but nothing happens. What am I missing here??

Unfortunately, the on_change parameter is not designed to handle this use case.

Instead, you can use the on_change event of the underlying st.selectbox widget, which is an instance of streamlit.elements.selectbox_proto:

import streamlit as st
import pandas as pd

def get_place(value):
    print("I am called")
    print("Received option", value)

st.set_page_config(page_title="Code of Cosmos", layout="wide")

with st.sidebar:
    print("I am called from sidebar")
    native_name = st.text_input("Name")
    native_place = st.selectbox("Place of Birth", options=[], key="place").on_change(get_place)
    native_dob = st.date_input("Date of Birth")
    native_tob = st.time_input("Time of Birth", "now")

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.