Hi!
I have updated Adrien’s code to make it work with recursive subdirectories.
import os
import streamlit as st
def st_file_selector(st_placeholder, path='.', label='Please, select a file/folder...'):
# get base path (directory)
base_path = '.' if path is None or path is '' else path
base_path = base_path if os.path.isdir(
base_path) else os.path.dirname(base_path)
base_path = '.' if base_path is None or base_path is '' else base_path
# list files in base path directory
files = os.listdir(base_path)
if base_path is not '.':
files.insert(0, '..')
files.insert(0, '.')
selected_file = st_placeholder.selectbox(
label=label, options=files, key=base_path)
selected_path = os.path.normpath(os.path.join(base_path, selected_file))
if selected_file is '.':
return selected_path
if os.path.isdir(selected_path):
selected_path = st_file_selector(st_placeholder=st_placeholder,
path=selected_path, label=label)
return selected_path
It uses the following code to handle the persistence of session states:
The st_file_selector
function can be invoked in the following way:
st_session_state = get(input_path='.')
st_session_state.input_path = st_file_selector(st_placeholder=st.empty(), path=st_session_state.input_path, label=f'Input path')
st.text(f'> Selected \'{st_session_state.input_path}\'.')
Hope it helps.
Cheers,
Mark