I am displaying a list of 100 items as the result of a model computation. With each result I have a checkbox associated with it that the user can tick. The idea is that the user is building a list of items it want to save at the end. How can I let the user tick these checkboxes without re-displaying all the results individually. I was able to cache the model computation result, however I am unsure as to how to do avoid each result to be re-rerendered on each tickbox click. (results are shown as images).
Have you tried using a form? For example, here’s a little sample code with checkboxes. There’s a timestamp at the top so you can visually see when the page is loading or not. The two checkboxes above/outside of the form will reload the page with each interaction. The checkboxes inside the form won’t cause the page to reload until you click Submit to send them in batch.
import streamlit as st
import time
st.write(time.time()%10)
st.checkbox('thing1', key='thing1')
st.checkbox('thing2', key='thing2')
with st.form('thing_form'):
st.checkbox('thing3', key='thing3')
st.checkbox('thing4', key='thing4')
st.form_submit_button('Submit')
Thanks. I ended up going with native HTML as this is better for my use-case. I want to have some dynamic update of the list of selected items. I hit another road block, it doesn’t let me inject javascript in the main scope of the page, only creates it inside iFrames which is not useful (using the components API or simply doesn’t work with the st.markdown API). Is there any non-hacky way to include a javascript script at the top-level scope?
I see what you are saying about st.components.v1.html creating iframes and st.markdown('''...''', unsafe_allow_html=True) not working as desired with <script>...</script>.
I am not aware of any straightforward way to add scripts to <head>. The two above mentioned methods are the only entry points that I have learned about so far. When I stick a simple script definition in with the components API, it ran for me, but if it’s not workable in your use case, I’m afraid I don’t have a fundamentally different direction to go. There’s another discussion thread on injecting javascript here, but it’s just a discussion about the components API and creating a custom component if a simple script embedding isn’t enough, so not sure if there’s any new information there to glean. Sorry I can’t be more help.
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.