Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

feat: support default file browser selection (#10255)

Yair Chuchem6faa194dda2d
diff --git a/marimo/_plugins/ui/_impl/file_browser.py b/marimo/_plugins/ui/_impl/file_browser.py
index 4a9a487a92..cabe4e32be 100644
--- a/marimo/_plugins/ui/_impl/file_browser.py
+++ b/marimo/_plugins/ui/_impl/file_browser.py
@@ -182,6 +182,8 @@ class file_browser(
files. Defaults to True.
restrict_navigation (bool, optional): If True, prevent the user from
navigating any level above the given path. Defaults to False.
+ value (str | Path | Sequence[str | Path], optional): File or directory
+ path, or sequence of paths, selected by default. Defaults to None.
ignore_empty_dirs (bool, optional): If True, hide directories that contain
no files (recursively). Directories are scanned up to 100 levels deep
to prevent stack overflow from deeply nested structures. Directory
@@ -206,6 +208,7 @@ class file_browser(
| Sequence[Literal["file", "directory"]] = "file",
multiple: bool = True,
restrict_navigation: bool = False,
+ value: str | Path | Sequence[str | Path] | None = None,
*,
filter: str | re.Pattern[str] | Callable[[Path], bool] | None = None, # noqa: A002
limit: int | None = None,
@@ -283,9 +286,43 @@ class file_browser(
else:
(wire_selection_mode,) = self._selection_mode
+ if value is None:
+ values: Sequence[str | Path] = ()
+ elif isinstance(value, Sequence) and not isinstance(value, str):
+ values = value
+ else:
+ values = (value,)
+
+ if not multiple and len(values) > 1:
+ raise ValueError(
+ "File browser with multiple=False cannot have more than one "
+ "default value."
+ )
+
+ initial_value: list[TypedFileBrowserFileInfo] = []
+ for selected_value in values:
+ selected_path = self._create_path(
+ normalize_path(self._create_path(selected_value))
+ )
+ is_directory = selected_path.is_dir()
+ kind = "directory" if is_directory else "file"
+ if kind not in self._selection_mode:
+ raise ValueError(
+ f"Default value {selected_path} is a {kind}, but "
+ f"selection_mode {self._selection_mode} does not allow it."
+ )
+ initial_value.append(
+ TypedFileBrowserFileInfo(
+ id=str(selected_path),
+ path=str(selected_path),
+ name=selected_path.name,
+ is_directory=is_directory,
+ )
+ )
+
super().__init__(
component_name=file_browser._name,
- initial_value=[],
+ initial_value=initial_value,
label=label,
args={
"initial-path": str(self._initial_path),
diff --git a/tests/_plugins/ui/_impl/test_file_browser.py b/tests/_plugins/ui/_impl/test_file_browser.py
index 69c6916e8c..d49ed4b1d3 100644
--- a/tests/_plugins/ui/_impl/test_file_browser.py
+++ b/tests/_plugins/ui/_impl/test_file_browser.py
@@ -40,6 +40,53 @@ def test_file_browser_init(tmp_path: Path) -> None:
assert fb._restrict_navigation is True
+def test_file_browser_default_value(tmp_path: Path) -> None:
+ first = tmp_path / "first.txt"
+ second = tmp_path / "second.txt"
+ first.touch()
+ second.touch()
+
+ fb = file_browser(initial_path=tmp_path, value=[first, str(second)])
+
+ assert [file.path for file in fb.value] == [first, second]
+ assert [file.name for file in fb.value] == ["first.txt", "second.txt"]
+
+
+def test_file_browser_single_default_value(tmp_path: Path) -> None:
+ selected = tmp_path / "selected.txt"
+ selected.touch()
+
+ fb = file_browser(initial_path=tmp_path, value=selected, multiple=False)
+
+ assert fb.path() == selected
+
+
+def test_file_browser_rejects_multiple_default_values(
+ tmp_path: Path,
+) -> None:
+ first = tmp_path / "first.txt"
+ second = tmp_path / "second.txt"
+ first.touch()
+ second.touch()
+
+ with pytest.raises(ValueError, match="multiple=False"):
+ file_browser(
+ initial_path=tmp_path,
+ value=[first, second],
+ multiple=False,
+ )
+
+
+def test_file_browser_rejects_default_value_of_wrong_kind(
+ tmp_path: Path,
+) -> None:
+ directory = tmp_path / "directory"
+ directory.mkdir()
+
+ with pytest.raises(ValueError, match="selection_mode"):
+ file_browser(initial_path=tmp_path, value=directory)
+
+
def test_list_directory() -> None:
fb = file_browser(
initial_path=Path.cwd(), filetypes=[".txt"], selection_mode="file"