File_uploader DuplicateWidgetID key error

I want to receive different data sheets from users.First, users need to click to determine their own data table type; Then upload the file; After that, the program performs a series of operations.
But the problem I encounter is that when the user changes the options, the previously uploaded files will not disappear.When the file is manually closed and uploaded again, an error will be reported.
The following is my code. In order to facilitate understanding, I simplified some programs, but the functions are consistent

def getFile():
    uploaded_file = st.file_uploader("Please")
    if uploaded_file is not None:
        df = pd.read_excel(uploaded_file)
        AgGrid(
            df.head(3),
            fit_columns_on_grid_load=False,
            height=120,
            editable=False,
        )

        return df

if __name__ == '__main__':
    a=input()
    if a=='a':
        data=getFile()
        # ... ...
    else:
        data=getFile()
        # ... ...

Hi @Dcclandbest, welcome to the Streamlit community!

When you place uploaded_file = st.file_uploader("Please") inside of a function that is called over and over again, you are creating a new instance of file_uploader each time rather than re-using the same one over and over. And because you create several instances of file_uploader, it’s yelling that you don’t have a key argument to make each of them have a unique identifier.

I suspect moving that line outside of the function will fix your issue.

Best,
Randy

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