-
Notifications
You must be signed in to change notification settings - Fork 0
/
dir_picker.py
53 lines (42 loc) · 1.59 KB
/
dir_picker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from pathlib import Path
import streamlit as st
def st_directory_picker(initial_path=Path()):
st.markdown("#### Directory picker")
if "path" not in st.session_state:
st.session_state.path = initial_path.absolute()
manual_input = st.text_input("Selected directory:", st.session_state.path)
manual_input = Path(manual_input)
if manual_input != st.session_state.path:
st.session_state.path = manual_input
st.experimental_rerun()
_, col1, col2, col3, _ = st.columns([3, 1, 3, 1, 3])
with col1:
st.markdown("#")
if st.button("⬅️") and "path" in st.session_state:
st.session_state.path = st.session_state.path.parent
st.experimental_rerun()
with col2:
subdirectroies = [
f.stem
for f in st.session_state.path.iterdir()
if f.is_dir()
and (not f.stem.startswith(".") and not f.stem.startswith("__"))
]
if subdirectroies:
st.session_state.new_dir = st.selectbox(
"Subdirectories", sorted(subdirectroies)
)
else:
st.markdown("#")
st.markdown(
"<font color='#FF0000'>No subdir</font>", unsafe_allow_html=True
)
with col3:
if subdirectroies:
st.markdown("#")
if st.button("➡️") and "path" in st.session_state:
st.session_state.path = Path(
st.session_state.path, st.session_state.new_dir
)
st.experimental_rerun()
return st.session_state.path