Widgets appear twice during @st.cache_data process phase

Summary
When a function (decorated with @cache_data) is running and a spinner is automatically shown, some widgets are displayed twice. The duplicates are disabled and are only visible until the processing phase is finished:

Debugging Info

  • I am running the app locally
  • I managed to reproduce the error on a simple streamlit script without any external library requirements
  • Python 3.9.5
  • Streamlit, version 1.42.0

Reproduce Sample

  1. Run the following streamlit code:
import streamlit as st
import time

# Simulate long-running cached functions
@st.cache_data
def long1(i):
  print(f'long1({i})')
  return i

@st.cache_data
def long2(i):
  print(f'long2({i})')
  time.sleep(5)  # Simulate delay
  return i * 2

if __name__ == '__main__':
  if 'step' not in st.session_state:
    st.session_state['step'] = 0

  if st.button('button1'):
    st.session_state['step'] = 1

  if st.session_state['step'] > 0:
    st.write(sum([long1(x) for x in range(5)]))
    with st.expander('first'):
      st.write('text 1')
    with st.expander('second'):
      st.write('text 2')

    if st.button('button2'):
      st.write(sum([long2(x) for x in range(2)]))

  1. Open the webpage and click on button1 → You should see a st.write and 2 simple st.exapnder widgets
  2. Click on button2 → While long2() is running, the st.write and the st.expanders are duplicated under the spinner in disabled mode.
  3. When the processing finishes, the duplicate widgets disappear and the resuling st.write is shown.