Callback function of widget being called every time I interact with the app

Hello everyone,
I’m trying to clear the cache of my app whenever a new file is uploaded through the file uploader widget, I tried using the on_change attribute which, if I understood correctly, calls my clear_cache function to clear the cache only when the file is changed. However, the function is being called every time I interact with the app. I tested this by making a new function which increases the value of a session_state variable whenever it gets called, and a button with no function, every time I click on the button the variable increases.

if 'acount' not in st.session_state:
        st.session_state.acount = 0
def plusf():
    st.session_state.acount += 1

# File uploader
st.file_uploader('Upload fluid file:', type=('xlsx', 'json'), on_change=plusf())

st.button("Press me")

Here’s a ss showing the issue, I click on the button only (without uploading a file) and the value of “acount” increases

Streamlit version is 1.13.0
Python version is 3.9.12
I’m using conda and vscode
Windows 10
Google Chrome Version 105.0.5195.127 (Official Build) (64-bit)

Thank you in advance

This is probably the same issue described here. Basically, the on_change argument expects a function object, you are just passing None to it.

Yes, that was the issue, I have to pass plusf to it. Thank you

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