Skip to content

Conversation

@lukasmasuch
Copy link
Collaborator

@lukasmasuch lukasmasuch commented Jun 19, 2023

Describe your changes

This PR attempts to fix the behavior of st.spinner so that it does not cause stale elements. Instead of hiding the spinner in the frontend via dg.empty(), this PR uses dg.container(). An empty container gets ignored on the frontend; in comparison, an dg.empty() placeholder is treated as an empty element in the front end, resulting in unwanted stale elements in some situations. The stale issue is caused by a scenario where in the first run, we are using a spinner, but in the second run, we are just showing the data, for example:

Open in Streamlit Cloud

import streamlit as st
import pandas as pd
import time

if "df" not in st.session_state:
    with st.spinner("Load data"):
        time.sleep(5)
        df = pd.DataFrame({"A": [1, 2, 3]})
        st.session_state.df = df

st.dataframe(st.session_state.df)

# Do something else that takes some time:
time.sleep(5)

The first run uses two elements, while the second one uses only one. This will cause a duplicated greyed-out version of the dataframe until the script has fully finished.

Alternatively, we could introduce some kind of dg.clear() or dg.empty(clear=True) command that removes the current element from the frontend.

Testing Plan

Updated unit tests related to st.spinner. I also had to deactivate the spinners in some caching tests since the testing utility does not work yet with st.container as mentioned here:

# TODO does not work for `st.container` which has no block proto


Contribution License Agreement

By submitting this pull request you agree that all contributions to this project are made under the Apache 2.0 license.

@lukasmasuch lukasmasuch added security-assessment-completed Security assessment has been completed for PR impact:internal PR changes only affect internal code change:bugfix PR contains bug fix implementation labels Jun 19, 2023
@lukasmasuch lukasmasuch marked this pull request as ready for review June 19, 2023 12:33
Copy link
Contributor

@tconkling tconkling left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ship it!

import streamlit as st
@st.experimental_memo(experimental_allow_widgets=True)
@st.cache_data(experimental_allow_widgets=True, show_spinner=False)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are these tests changed to use show_spinner=False?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The testing utilities are not able to handle containers, which seems to be a known problem:

# TODO does not work for `st.container` which has no block proto

That's why I had to deactivate the show_spinner in those examples, otherwise there would be a container in the element tree -> causing an exception with this testing utilities.

@lukasmasuch lukasmasuch merged commit 7481b4e into develop Jun 20, 2023
@vdonato vdonato deleted the fix/stale-elements-from-spinners branch November 1, 2023 23:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

change:bugfix PR contains bug fix implementation impact:internal PR changes only affect internal code security-assessment-completed Security assessment has been completed for PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants