Clear uploaded files with button not working well

I have the for following code:

if 'key' not in st.session_state: st.session_state.key = str(randint(1000, 100000000))

upload_files = st.file_uploader("Choose files", accept_multiple_files=True, type=['pdf'], key=st.session_state.key, help="Rerun if cache error")

if st.button('Clear Uploaded File(s)', help='click twice to clear ALL'): for key in st.session_state.keys(): del st.session_state[key]

When I press the Clear… button, have to press twice to clear the uploaded listed files to be removed. And when I try adding new files, initial upload fails but repeat holds. Would love to get this fix if someone could tell me how.

Hi @philsgu ,

Welcome to the community. You can try this:

import streamlit as st
if st.button('Clear Uploaded File(s)') and 'key' in st.session_state.keys():
    st.session_state.pop('key')
    st.experimental_rerun()

Thanks,
Akshansh

1 Like

You’re awesome! It works flawless. Could I ask you one additional related code matter–I added a progress bar but it’s not real time when parsing is performed.

if st.button ("Analyze") and len(upload_files) > 0:
      latest_iteration = st.text('hang on...')
      my_bar = st.progress(0)
      for percent_complete in range(100):
        time.sleep(0.05)
        latest_iteration.text(f'{percent_complete + 1}%')
        my_bar.progress(percent_complete + 1)

      for file in upload_files: 
        with pf.open(file) as pdf:
          page = pdf.pages[0]
          text = page.chars[0]
          text_data = page.extract_text()
          comlex_fail = re.compile(r'Fail\s\d{2}/\d{2}/\d{4}')
          usmle_fail = re.compile(r'\d{2}/\d{2}/\d{4}\s+\bFail\b')
          if "COMLEX-USA" in text_data:
              ctext_list = text_data.split('COMLEX-USA')
              #print(text_list[0])
              cfail_list = comlex_fail.findall(text_data)
              if cfail_list:
                df = df.append({'Applicant ID': ctext_list[0], 'Total COMLEX Fails': len(cfail_list)}, ignore_index=True)   
          elif "USMLE" in text_data: 
              utext_list = text_data.split('USMLE')
              ufail_list = usmle_fail.findall(text_data)
              if ufail_list:
                df = df.append({'Applicant ID': utext_list[0], 'Total USMLE Fails': len(ufail_list)}, ignore_index=True)
          else:
              status.append(str(file))

If i got this right, this seems like a non deterministic task, so the proper UX here would be to use a spinner instead of a progress bar. Like this:

with st.spinner('Performing Analysis ...'):
      do_analysis()

Thanks again!

As per session_state and Download button, how do I use the key to make sure the button does not clear/disappear after clicked or is there additional method in the download_button.

if 'key' not in st.session_state: 
    st.session_state.key = str(randint(1000, 100000000))

st.download_button(
          label='Download CSV file', 
          data = csv, 
          mime='text/csv', 
          file_name ='failed_applicants.csv'
          )