Create dynamic columns depending on number of uploaded files

I want to allow the user to upload multiple csv files and prompt to input start_date for each csv file. Depending on the number of uploaded files, I would like to have the start_date field to be in separate columns instead of being vertically stacked on one another.

Can you please help me to build this logic?
Here is my current code (which is not working)

uploaded_files = st.sidebar.file_uploader('Upload file(s) here', accept_multiple_files=True)

def uploadFile(upload_file):
   if uploaded_file is not None:
      df = pd.read_csv(upload_file)

df = uploadFile(upload_file)

def process_csv(): 
    if uploaded_files:
       ncol = len(uploaded_files)
       cols = st.columns(ncol)

        for i in range(ncol):
           with cols[ncol[i]]:
              for uploaded_file in uploaded_files:                                
                 with st.spinner(text= f"Loading file: {uploaded_file.name}"):      
                    df = uploadFile(uploaded_file)
                    start_date = st.date_input(f'Enter start date for {uploaded_file.name}', value=None, key=uploaded_file.name)
                    """further processing here"""

Try this:

uploaded_files = st.sidebar.file_uploader('Upload file(s) here', accept_multiple_files=True)
st.write(uploaded_files)

if uploaded_files == None or uploaded_files == []:
    pass
else:
    ncol = len(uploaded_files)
    cols = st.columns(ncol)

    start_date = [None]*ncol
    for i in range(ncol):
        with cols[i]:
            with st.spinner(text= f"Loading file: {uploaded_files[i].name}"):      
                df = pd.read_csv(uploaded_files[i])
                st.dataframe(df.head())
                start_date[i] = st.date_input(f'Enter start date for {uploaded_files[i].name}', key=uploaded_files[i].name)
                """further processing here"""

Note that the date picker won’t accept None as an initial value and it will become today. So you’ll need some tweaking there depending on what flow you want.

1 Like

Hi, thanks it worked well. Regarding the date picker, I can just input any date, but today’s date also works for me.
Thanks

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