Skip to content

Commit

Permalink
feat: use cache on InputFile widget to improve loading times
Browse files Browse the repository at this point in the history
  • Loading branch information
dfguerrerom committed Aug 8, 2023
1 parent fe45714 commit f7eda5a
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion sepal_ui/sepalwidgets/inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ class FileInput(v.Flex, SepalWidget):
folder: Path = Path.home()
"the current folder"

initial_folder: Path = Path.home()
"the starting point of the file input"

file: t.Unicode = t.Unicode("").tag(sync=True)
"the current file"

Expand Down Expand Up @@ -221,6 +224,7 @@ def __init__(
v_model: str = "",
clearable: bool = False,
root: Union[str, Path] = "",
cache=False,
**kwargs,
) -> None:
"""Custom input field to select a file in the sepal folders.
Expand All @@ -235,8 +239,10 @@ def __init__(
kwargs: any parameter from a v.Flex abject. If set, 'children' will be overwritten.
"""
self.extensions = extensions
self.initial_folder = folder
self.folder = Path(folder)
self.root = str(root) if isinstance(root, Path) else root
self.cache_dirs = {}

self.selected_file = v.TextField(
readonly=True,
Expand Down Expand Up @@ -329,7 +335,7 @@ def reset(self, *args) -> Self:
# time when multiple fileInput are reset at the same time as in the aoiView
if self.v_model is not None:
# move to root
self._on_file_select({"new": Path.home()})
self._on_file_select({"new": self.initial_folder})

# remove v_model
self.v_model = ""
Expand Down Expand Up @@ -406,6 +412,10 @@ def _get_items(self) -> List[v.ListItem]:
el for el in list_dir if el.is_dir() or el.suffix in self.extensions
]

if folder in self.cache_dirs:
if self.cache_dirs[folder]["files"] == list_dir:
return self.cache_dirs[folder]["items"]

folder_list = []
file_list = []

Expand Down Expand Up @@ -459,6 +469,10 @@ def _get_items(self) -> List[v.ListItem]:
folder_list.extend(file_list)
folder_list.insert(0, parent_item)

self.cache_dirs.setdefault(folder, {})
self.cache_dirs[folder]["files"] = list_dir
self.cache_dirs[folder]["items"] = folder_list

return folder_list

def _on_reload(self, *args) -> None:
Expand Down

0 comments on commit f7eda5a

Please sign in to comment.