How to Make Folder Picker with Streamlit?
Asked Answered
K

1

7

Can I pick folder with Streamlit?

file_uploader provides to choose a file, but not a folder.

enter image description here I can not select directory with file_uploader in Streamlit. But I want to save the dataframe to the folder which the user decides. If there is a way to do this, I would be very happy if you tell me.

Kenwee answered 25/9, 2022 at 18:22 Comment(0)
R
1

Streamlit's file_uploader does not yet natively support directory selection.

Here's a workaround for saving files in a directory specified by the user. Here we allow users to input the directory path as a string.

import streamlit as st
import pandas as pd
import os

def save_df_to_folder(df, folder_path, file_name):
    """Saves dataframe to the provided folder."""
    if not os.path.isdir(folder_path):
        st.error('The provided folder does not exist. Please provide a valid folder path.')
        return

    file_path = os.path.join(folder_path, file_name)
    df.to_csv(file_path, index=False)
    st.success(f'Successfully saved dataframe to {file_path}')

df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})  # example dataframe

folder_path = st.text_input('Enter the folder path where you want to save the dataframe:')
file_name = st.text_input('Enter the filename for the dataframe (should end in .csv):')

if st.button('Save Dataframe'):
    save_df_to_folder(df, folder_path, file_name)

Here's how it looks:

enter image description here

Let me know if that helps! :)

Charly

Rinee answered 17/7, 2023 at 14:59 Comment(1)
Hello, I noticed this and did it this way. It's very sad that there is no support. Thanks for the comment. It will give people ideas.Kenwee

© 2022 - 2024 — McMap. All rights reserved.