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.

fix: ui.file_browser() abs paths (#2314)

Akshay Agrawal9def20828ea9
diff --git a/frontend/src/plugins/impl/FileBrowserPlugin.tsx b/frontend/src/plugins/impl/FileBrowserPlugin.tsx
index 2a64c13888..0b3f86c269 100644
--- a/frontend/src/plugins/impl/FileBrowserPlugin.tsx
+++ b/frontend/src/plugins/impl/FileBrowserPlugin.tsx
@@ -122,6 +122,8 @@ interface FileBrowserProps extends Data, PluginFunctions {
/**
* File browser component.
+ *
+ * Only works for absolute paths.
*/
export const FileBrowser = ({
value,
@@ -163,7 +165,7 @@ export const FileBrowser = ({
files = [];
}
- const pathBuilder = PathBuilder.guessDeliminator(path);
+ const pathBuilder = PathBuilder.guessDeliminator(initialPath);
const delimiter = pathBuilder.deliminator;
const selectedPaths = new Set(value.map((x) => x.path));
@@ -356,15 +358,23 @@ export const FileBrowser = ({
);
}
- // Get list of parent directories
+ // Get list of parent directories.
+ //
+ // Assumes that path contains at least one delimiter, which is true
+ // only if this is an absolute path.
const directories = path.split(delimiter).filter((x) => x !== "");
directories.push(path);
- const parentDirectories = directories.map((dir, index) => {
+ let parentDirectories = directories.map((dir, index) => {
const dirList = directories.slice(0, index);
return `/${dirList.join(delimiter)}`;
});
+ if (restrictNavigation) {
+ parentDirectories = parentDirectories.filter((x) =>
+ x.startsWith(initialPath),
+ );
+ }
parentDirectories.reverse();
const selectionKindLabel =
diff --git a/marimo/_plugins/ui/_impl/input.py b/marimo/_plugins/ui/_impl/input.py
index 9fa0188403..762427ab29 100644
--- a/marimo/_plugins/ui/_impl/input.py
+++ b/marimo/_plugins/ui/_impl/input.py
@@ -4,6 +4,7 @@ from __future__ import annotations
import base64
import dataclasses
import os
+import pathlib
import traceback
from dataclasses import dataclass
from typing import (
@@ -1360,6 +1361,16 @@ class file_browser(UIElement[List[Dict[str, Any]], Sequence[FileInfo]]):
if not initial_path:
initial_path = os.getcwd()
+ # frontend plugin can't handle relative paths
+ initial_path = os.path.realpath(os.path.expanduser(initial_path))
+ # initial path must be a directory
+ if not os.path.isdir(initial_path):
+ raise ValueError(
+ f"Initial path {initial_path} is not a directory."
+ )
+
+ self.restrict_navigation = restrict_navigation
+ self.initial_path = initial_path
super().__init__(
component_name=file_browser._name,
initial_value=[],
@@ -1382,6 +1393,18 @@ class file_browser(UIElement[List[Dict[str, Any]], Sequence[FileInfo]]):
)
def list_directory(self, args: ListDirectoryArgs) -> ListDirectoryResponse:
+ # When navigation is restricted, the navigated-to path cannot be
+ # be a parent of the initial path
+ if (
+ self.restrict_navigation
+ and pathlib.Path(args.path)
+ in pathlib.Path(self.initial_path).parents
+ ):
+ raise RuntimeError(
+ "Navigation is restricted; navigating to a "
+ "parent of initial path is not allowed."
+ )
+
files = []
files_in_path = OSFileSystem().list_files(args.path)