I'm using python shiny core, making an application that will be run locally. One of the outputs from the application is sample code that, ideally, references files specified by the user. Right now, I'm using ui.input_file
, and so the path I have is in the upload directory, not the original location of the file.
Has anyone made a widget that allows the selection of a local file path? Things I've considered:
- I'm not going to be able to use the browser's native file selector: File name is available, but the rest of the path is not available, by design.
- This thread proposes
choose.dir
, but I think that is only for windows, and only for R. - Similarly with this thread.
I've started on a home-grown solution, and this may be the best path. If I do finish this, are there any repos collecting third-party widgets where I could contribute this?
def get_file_selector_choices(cwd: Path):
return [None, parent_name] + sorted(
path.name + ("/" if path.is_dir() else "")
for path in cwd.iterdir()
if path.name.endswith(".csv") or path.is_dir()
)
@reactive.event(input.public_csv_name)
def _on_public_csv_name_change():
name = input.public_csv_name()
if not name:
pass
elif name == "../":
new_dir = public_csv_dir().parent
public_csv_dir.set(new_dir)
public_csv_path.set(None)
ui.update_select(
"public_csv_name", choices=get_file_selector_choices(new_dir)
)
elif name.endswith("/"):
new_dir = public_csv_dir() / name
public_csv_dir.set(new_dir)
public_csv_path.set(None)
ui.update_select(
"public_csv_name", choices=get_file_selector_choices(new_dir)
)
else:
new_path = public_csv_dir() / name
public_csv_path.set(new_path)