Click checkbox once and unchecking it and clikc again causes the data to be preserved

I have created a checkbox. WHen i select that checkbox the st.file_uploader comes up normally and I can check the location adn category and when I uncheck it, they disappear, which is right. But when I tick it again, the file_uploader loads up but the previous data which I selected (location adn category) also shows up. I dont want them to be preserved. How can I get rid of them? Below is my code:

st.sidebar.header(‘Select Type:’)

checkbox_staticguarding = st.sidebar.checkbox(“Static Guarding”)

checkbox_mobilepatrol = st.sidebar.checkbox(“Mobile Patrol”)

@st.cache_data
def read_data(file1, file2):
if file1 is not None and file2 is not None:
df1 = pd.read_excel(file1, sheet_name=“Source Data”, skiprows=3)
df2 = pd.read_excel(file2)

    return df1, df2
elif file1 is not None:
    st.warning("Please upload the second Excel file.")
elif file2 is not None:
    st.warning("Please upload the first Excel file.")
else:
    st.warning("Please upload all the Excel files.")
return None, None

uploaded_file1 = None
uploaded_file2 = None

if checkbox_staticguarding:
with st.expander(“Upload the XLSX Files”, expanded=False):
uploaded_file1 = st.file_uploader(“Upload Static Guarding Billable Hours and Revenue File”, type=[“xlsx”])
uploaded_file2 = st.file_uploader(“Upload Master Customer File”, type=[“xlsx”])

df1, df2 = read_data(uploaded_file1, uploaded_file2)

if df1 is not None:
    df1.replace(' ', pd.NA, inplace=True)
    df1.rename(columns={'Total': 'Revenue'}, inplace=True)
    df1.dropna(how='all', axis=1, inplace=True)
    columns_to_drop = ['Category', 'Batch #', 'Invoice #', 'Line #', 'Employee #', 'First Name', 'Middle Name', 'Last Name', 'Classification', 'Rate']
    df1.drop(columns=columns_to_drop, inplace=True)

if df1 is not None and df2 is not None:
    merged_df = pd.merge(df1, df2, on=['Customer Number'], how="outer")

    cust_no_to_delete = 9200
    merged_df = merged_df[merged_df['Customer Number'] != cust_no_to_delete]

Can anyone help me please regarding this?

Hi @Sujith_Pisharody! :wave:

I’m not able to reproduce what I think you’re describing. I created a couple Excel files. I can check “Static Guarding,” upload two files, and get a result. If I uncheck “Static Guarding” and recheck it, I get empty file uploaders. If I load two different files, I get a commensurate, different result. (I added a line to display the merged dataframe at the end.)

A couple things that will help get you an answer:

  1. Can you format your code by surrounding it with triple backticks or using the code format option in the UI?

  2. Can you simplify your code to just the part that isn’t behaving as expected and provide some text that can be pasted to create the associated data files to upload? It doesn’t sound like the column dropping and renaming is part of the problem, for example.

  3. Is there some other part of the code? The snippet provided doesn’t display anything except the file uploaders, which do indeed empty if I uncheck and recheck “Static Guarding.” I’m wondering if there’s some other function, maybe cached that’s involved here and affecting some result you are seeing…

HI,

The problem is solved. Actually I enclosed the related code in the check_staticguarding boolean and now the issue has been fixed. Thank you so much for taking ur time and replying to me. Appreciate it. :smile:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.