Selectbox issue

dict1 = {pathlib.PurePath(d).name: d for d in pathlib.Path(file).parent.iterdir() if d.is_dir()}

dict = st.selectbox(‘please choose Directory’, [*dict1])

files = {pathlib.Path(f).stem: f for f in pathlib.Path(dict).iterdir() if f.is_file()}

file = st.selectbox(‘please choose files’, [*files])

I have two selectbox’s, the first one is selecting directory, and the second one is selecting file under that directory selected by first selectbox. but always at the first time, after I select the file in the second selectbox, it will display the default files from the default directory by first selectbox. I have to select first one again, then works afterwards.

Hi Calvin,

unfortunately, I can’t reproduce this bug. Can you maybe give more details on the specific setup of your app?

The following setup is working for me:

Folder structure

  • app.py
  • foo/foo1.txt
  • foo/foo2.txt
  • bar/bar1.txt
  • bar/bar2.txt

The app.py scripts looks like yours:

import streamlit as st
import pathlib

dict1 = {
    pathlib.PurePath(d).name: d
    for d in pathlib.Path(__file__).parent.iterdir()
    if d.is_dir()
}

dict = st.selectbox('Directory', [*dict1])

files = {
    pathlib.Path(f).stem: f
    for f in pathlib.Path(dict).iterdir()
    if f.is_file()
}

file = st.selectbox('Files', [*files])

It gives me the expected result: https://streamable.com/zp1823

1 Like

sorry. i tried them as sidebar. no problem on pc, but it always happens at the first time on the server.

dict1 = {pathlib.PurePath(d).name: d for d in pathlib.Path(__file__).parent.iterdir() if d.is_dir()}

dict = st.sidebar.selectbox('please choose Directory', [*dict1])

files = {pathlib.Path(f).stem: f for f in pathlib.Path(dict).iterdir() if f.is_file()}


file = st.sidebar.selectbox('please choose files', [*files])

looks like at the first time, the last widget is always by default.