Is there a way to annotate the code to run when the page has loaded?
e.g.
import streamlit as st
@st.cache()
@st.run_when_idle()
def load_data(...):
df = ...
return df
data = load_data() # not executed yet
st.title('Hello')
st.markdown("# Introduction: ...")
st.write(df) # waits until the rest of the page loads
st.makrdown('''# Conclusion: ...")
I’m interested by this thought, but to be sure of what you are trying to accomplish with the given annotation…
Do you want the st.markdown("Conclusion") to be displayed before, while load_data is running in the background, and then when the loading is over, display the dataframe before the # Conclusion part?
import pandas as pd
import streamlit as st
import time
def load_data():
time.sleep(5) # simulate a 5 seconds download
return pd.read_csv(
"https://raw.githubusercontent.com/datasciencedojo/datasets/master/titanic.csv"
)
st.markdown("# Introduction: ...")
container_for_dataframe = st.empty() # placeholder for your displayed dataframe for later
container_for_dataframe.warning("Loading data") # in the meantime display a warning
st.markdown("# Conclusion: ...")
df = load_data()
container_for_dataframe.dataframe(df) # now data is loaded, overwrite placeholder
If I’m mistaken in your problem, don’t hesitate to correct the purpose
I think placeholders probably work for the example I gave, but I didn’t express myself well.
I’m building an interactive report and some fairly large objects need to be loaded to offer full functionality.
To illustrate it better:
import streamlit as st
@st.cache
def load_model(model_id:str)->Model: # <- this takes ~3sec to run
# say something slow happens, like training the model or calculating shap values
return model
model_ids = ['A', 'B', 'C', ...., 'Z']
model_objects = {}
current_model_id = st.selectbox('What model do you want to use?', options=model_ids])
current_model = model_objects.get(current_model_id, load_model(current_model_id))
# some stuff that lets users use the model.
So, my situation is:
[1] Loading all the objects would make my app unresponsive for ~90 seconds.
i.e. doing model_objects = {model_id: load_model(model_id) for model_id in model_ids}
would be slow (but not a memory problem)
[2] Loading a particular object takes ~3seconds, so it’d be good to be able to do it ahead of time “when nothing else is happening”
Is this niche? Yes, probably, and caching means it only needs to happen when I restart the app/clear the cache.
Ok, just writing some thoughts here without having tested anything
If your “load_model” is mostly IO bound, I think that’s a good fit for asyncio (which then makes it more of a Python problem than a Streamlit problem :)). Using a coroutine would load the model in the background when nothing else is happening.
Now I don’t know about caching an async method though, there is a draft of something here: Caching asyncio functions but I personally did not have time to play with this. There are other posts relative to asyncio (eventually threading) in Streamlit if you want to check the search bar.
I’ll try to think about this more later on, just wanted to share this that will maybe get your on a new track
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.