First of all big kudos to the Streamlit team for this awesome piece of software.
I have run into a mild annoyance. I am creating an app to do image labeling (with multiple label classes). The categories are binary, and each label category is represented by a checkbox. The labels are stored and the next image is shown when upon clicking a ‘Submit’ button.
The problem:
Whenever a checkbox is checked, the app makes a new call to st.image(picture) rerenders the (same) picture, which makes the app ‘fade’ for a moment. I would like to avoid this fading if possible, or at least minimize it. Right now, it seems that the caching isn’t really doing a lot for the speed. I see some approaches.
1: Somehow pause rerendering until submit button is hit. I.e. ability to change the value of the widget without impacting the state of the app before clicking a ‘submit’ button (preferred option).
2: Make the caching work properly so that the image does not rerender (perhaps I am making some mistake regarding how caching is used?)
Can any of you good people give me some pointers?
Minimal example:
from PIL import Image
from collections import OrderedDict
import streamlit as st
categories= ['a', 'b', 'c']
checkboxes = OrderedDict({category: st.sidebar.checkbox(category) for category in categories})
### Omitted code with submit button and writing labels to file ###
@st.cache
def load_image(img_id):
image = Image.open(f"images/{img_id}.jpg")
return image
image = load_image("example")
st.image(image, use_column_width=True)
Hey @PeterT, welcome to Streamlit, and thanks for the kind words!
As you’ve noted, this fade happens every time your app re-runs (and therefore re-renders), if it takes longer than 1 second to complete the re-render. There isn’t a real workaround for this right now, but we have an open GitHub issue that you can follow. There have been a number of requests for “form-like” widget behavior, and it’s something we’re discussing internally!
In the meantime, there are several sub-optimal workarounds if this is a real show-stopper for you:
Make your app run faster. (This may not be possible! It looks like you’re already using caching. Are there other long-running operations that could be cached?)
Modify the .stale-element CSS selector in ReportView.scss, extending the duration of the opacity transition (or removing the opacity change altogether). This would require you to build your own Streamlit, which is more work, so I’d certainly not recommend it to most users, but it’s an escape hatch if needed.
Thanks a lot for the answer @tim! It is exactly a form-like widget that I need even though I never thought of it that way - so I will be following the updates closely
The second option actually sounds like a great hack for now, as I think it would suffice to remove the opacity transition for now. I will look into it. Thank you!
Not really, but I got to a point where it worked OK with some performance improvement and additional use of the @st.cache decorator. True ‘form-like’ behavior where reloading is disabled is as far as I am aware not a part of Streamlit yet.
I just noticed that it seems to be WIP with form-like behaviour! See this pull request. Don’t know when we can expect it to be released though. @tim I can see you did a lot of work on this. Do you have a rough time estimate on when we can expect it to be released?
What I noticed was the opacity of the elements with the .element-container class were getting a class added to them while the streamlit app was rerendering, which contained the css styling of opacity{0.33}, which was causing the fading. To override this, I just added the following code, which is working for now:
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.