Will there be any additional event_listeners other than on_change?

Summary

Will there be any event listeners other than on_change for objects different than st.button?

Steps to reproduce

st.multiselect(label, default, on_click=load_options)

Expected behavior:

The goal is to load multiselect options when it is clicked. Options are viewable after they are loaded

Actual behavior:

Only on_change event listener available

Hi @dwalkusz

Thanks for your question. It seems you want to be able to click a button that will pre-load the options for a multi-select box.

Here’s a simple implementation:

Demo app

Screen Recording 2566-06-23 at 10_scaling-0.5_fps-20_speed-10.0_duration-0-17

Code

import streamlit as st

# Initialize 'options' variable
options = []

# Create a button that populates the 'options' variable when clicked
if st.button('Load options'):
    options = ['A', 'B', 'C', 'D', 'E', 'F', 'G']

# Create a button that clears the 'options' variable when clicked
if st.button('Clear options'):
    options = []

# Use 'options' as input to st.multiselect
selected_options = st.multiselect('Select options', options)

# Display the selected options
if selected_options:
    st.write(f"You have selected {selected_options}")

    # Also print each selected option
    for option in selected_options:
        st.write(f"Option: {option}")

More info on on_click and on_change

The Documentations page on Session States provides a list of all input widgets that supports either the on_click or on_change events.

The following are excerpt from the above documentation page:

Widgets which support the on_change event:

  • st.checkbox
  • st.color_picker
  • st.date_input
  • st.multiselect
  • st.number_input
  • st.radio
  • st.select_slider
  • st.selectbox
  • st.slider
  • st.text_area
  • st.text_input
  • st.time_input
  • st.file_uploader

Widgets which support the on_click event:

  • st.button
  • st.download_button
  • st.form_submit_button

Hope this helps!

Best regards,
Chanin

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