I’m confused how to immediately get the most recent values of the multiselect after a callback (using on_change).
Using the below script, if I remove any of the items from the multiselect through the interface, the values being printed through the callback are showing the old values (before removal).
It’s constantly lagging by one item. It does show the right values if I write them out after it passes the part in the script again where the multiselect is declared. However, it is mandatory to declare the callback (on_change) function BEFORE.
import streamlit as st
def main():
items= [1,2,3,4]
def get_new_values_list():
st.write(values) # < returns list before removal
values = st.multiselect('issue', items, items, on_change=get_new_values_list)
st.write(values) # < returns correct list
if __name__ == '__main__':
main()
Great! That works. So if I understand it well, the key argument gives a user named handle that can be consumed through the session state at any point in time?
Well, yes. Though the callback pattern is typically used to set values before the script that comes after the widget is run. It’s useful for instance to set other session state variables to use as the values of widgets that will be displayed based on the value of the widget making the callback. You may have noticed for example that a checkbox’s state doesn’t “stick” through a rerun unless it is clicked twice. If you set its current value session state via its callback, then you can indeed make the state “stick”. E.g. you can also change the contents of other widgets in the same callback, so when Streamlit reruns, these new values will be displayed. It’s a little convoluted, but once you get your head around it, its pretty powerful.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.