Skip to content
Snippets Groups Projects

Updated File explorer to work with large data directories

1 file
+ 28
23
Compare changes
  • Side-by-side
  • Inline
+ 28
23
@@ -56,17 +56,28 @@ class Interface:
"""Used to reset outputs with the clear button"""
return None
def update_explorer(self, new_path):
def update_directory(self, new_path):
""" Updates the current directory for ui components
Args:
new_path (str): path to the new directory
Raises:
ValueError: In case the path is invalid
Returns:
gr.Update: Returns a gr.Update object for both explorer and base_path components
"""
new_path = os.path.expanduser(new_path)
# In case we have a directory
if os.path.isdir(new_path):
return gr.update(root_dir=new_path, label=new_path)
return gr.update(root_dir=new_path, label=new_path), gr.update(value=new_path, choices=[os.path.join(new_path, folder) for folder in os.listdir(new_path) if os.path.isdir(os.path.join(new_path, folder))])
elif os.path.isfile(new_path):
parent_dir = os.path.dirname(new_path)
file_name = str(os.path.basename(new_path))
return gr.update(root_dir=parent_dir, label=parent_dir, value=file_name)
return gr.update(root_dir=parent_dir, label=parent_dir, value=file_name), gr.update(value=parent_dir, choices=[os.path.join(parent_dir, folder) for folder in os.listdir(parent_dir) if os.path.isdir(os.path.join(parent_dir, folder))])
else:
raise ValueError("Invalid path")
@@ -107,35 +118,29 @@ class Interface:
gr.Markdown("### File selection")
with gr.Row():
with gr.Column(scale=99, min_width=128):
base_path = gr.Textbox(
max_lines=1,
base_path = gr.Dropdown(
container=False,
label="Base path",
elem_classes="h-36",
value=os.getcwd(),
choices=[os.path.join(os.getcwd(), folder) for folder in os.listdir(os.getcwd()) if os.path.isdir(os.path.join(os.getcwd(), folder))],
interactive=True,
allow_custom_value=True,
)
with gr.Column(scale=1, min_width=36):
reload_base_path = gr.Button(
value="", elem_classes="btn-html h-36"
)
def new_fileexplorer():
return gr.FileExplorer(
glob="**/**",
ignore_glob="**/.*",
root_dir=os.getcwd(),
label=os.getcwd(),
render=True,
file_count="single",
interactive=True,
elem_classes="h-256 hide-overflow",
)
explorer = new_fileexplorer()
# ! FileExplorer is not updating when the root_dir changes to the same path only when it changes to a different path
gradio_interface.load(
lambda: new_fileexplorer(), None, explorer, every=1
explorer = gr.FileExplorer(
#glob=".*",
#ignore_glob="*/",
root_dir=os.getcwd(),
label=os.getcwd(),
render=True,
file_count="single",
interactive=True,
elem_classes="h-256 hide-overflow",
)
with gr.Column(scale=1):
@@ -259,7 +264,7 @@ class Interface:
spinner_loading = gr.Text("Loading data...", visible=False)
spinner_operations = gr.Text("Running pipeline...", visible=False)
# fmt: off
reload_base_path.click(fn=self.update_explorer,inputs=base_path, outputs=explorer)
reload_base_path.click(fn=self.update_directory,inputs=base_path, outputs=[explorer, base_path])
btn_run.click(
fn=self.set_spinner, inputs=spinner_session, outputs=btn_run).then(
Loading