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.

add storage file viewer and unify components (#8500)

Shahmir Varqha7b4d96444fb0
diff --git a/frontend/src/components/editor/file-tree/file-header.tsx b/frontend/src/components/editor/file-tree/file-header.tsx
new file mode 100644
index 0000000000..0ccf97f8f5
--- /dev/null
+++ b/frontend/src/components/editor/file-tree/file-header.tsx
@@ -0,0 +1,61 @@
+/* Copyright 2026 Marimo. All rights reserved. */
+
+import { ArrowLeftIcon, DownloadIcon, RefreshCwIcon } from "lucide-react";
+import { Button } from "@/components/ui/button";
+import { Tooltip } from "@/components/ui/tooltip";
+
+interface FilePreviewHeaderProps {
+ filename?: string;
+ filenameIcon?: React.ReactNode;
+ onBack?: () => void;
+ onRefresh?: () => void;
+ onDownload?: () => void;
+ /** Extra action buttons placed before the download button. */
+ actions?: React.ReactNode;
+}
+
+export const FilePreviewHeader: React.FC<FilePreviewHeaderProps> = ({
+ filename,
+ filenameIcon,
+ onBack,
+ onRefresh,
+ onDownload,
+ actions,
+}) => {
+ return (
+ <div className="flex items-center shrink-0 border-b px-1 gap-1">
+ {onBack && (
+ <Tooltip content="Back to file list">
+ <Button variant="text" size="xs" onClick={onBack}>
+ <ArrowLeftIcon className="h-4 w-4" />
+ </Button>
+ </Tooltip>
+ )}
+ {filename ? (
+ <span className="flex items-center gap-1.5 flex-1 min-w-0 text-xs font-semibold truncate">
+ {filenameIcon}
+ {filename}
+ </span>
+ ) : (
+ <span className="flex-1" />
+ )}
+ <div className="flex items-center gap-0.5 shrink-0">
+ {onRefresh && (
+ <Tooltip content="Refresh">
+ <Button variant="text" size="xs" onClick={onRefresh}>
+ <RefreshCwIcon className="h-3.5 w-3.5" />
+ </Button>
+ </Tooltip>
+ )}
+ {actions}
+ {onDownload && (
+ <Tooltip content="Download">
+ <Button variant="text" size="xs" onClick={onDownload}>
+ <DownloadIcon className="h-3.5 w-3.5" />
+ </Button>
+ </Tooltip>
+ )}
+ </div>
+ </div>
+ );
+};
diff --git a/frontend/src/components/editor/file-tree/file-viewer.tsx b/frontend/src/components/editor/file-tree/file-viewer.tsx
index 9f1c856e11..d6ff13f455 100644
--- a/frontend/src/components/editor/file-tree/file-viewer.tsx
+++ b/frontend/src/components/editor/file-tree/file-viewer.tsx
@@ -1,19 +1,18 @@
/* Copyright 2026 Marimo. All rights reserved. */
-import { EditorView, keymap } from "@codemirror/view";
+import { keymap } from "@codemirror/view";
import { useAtomValue } from "jotai";
import {
AlertTriangleIcon,
CopyIcon,
- DownloadIcon,
ExternalLinkIcon,
- RefreshCwIcon,
SaveIcon,
} from "lucide-react";
import type React from "react";
-import { Suspense, useEffect, useRef, useState } from "react";
+import { useEffect, useRef, useState } from "react";
import { renderShortcut } from "@/components/shortcuts/renderShortcut";
import { Alert, AlertDescription } from "@/components/ui/alert";
+import { Button } from "@/components/ui/button";
import { Tooltip } from "@/components/ui/tooltip";
import { disableFileDownloadsAtom, hotkeysAtom } from "@/core/config/config";
import { useRequestClient } from "@/core/network/requests";
@@ -21,19 +20,15 @@ import type { FileInfo } from "@/core/network/types";
import { filenameAtom } from "@/core/saving/file-state";
import { isWasm } from "@/core/wasm/utils";
import { useAsyncData } from "@/hooks/useAsyncData";
-import { LazyAnyLanguageCodeMirror } from "@/plugins/impl/code/LazyAnyLanguageCodeMirror";
import { ErrorBanner } from "@/plugins/impl/common/error-banner";
-import { useTheme } from "@/theme/useTheme";
import { copyToClipboard } from "@/utils/copy";
import { downloadBlob, downloadByURL } from "@/utils/download";
import { type Base64String, base64ToDataURL } from "@/utils/json/base64";
-import { Button } from "../inputs/Inputs";
+import { FilePreviewHeader } from "./file-header";
import {
- AudioViewer,
- CsvViewer,
- ImageViewer,
- PdfViewer,
- VideoViewer,
+ FileContentRenderer,
+ isMediaMime,
+ MIME_TO_LANGUAGE,
} from "./renderers";
interface Props {
@@ -46,7 +41,6 @@ interface Props {
const unsavedContentsForFile = new Map<string, string>();
export const FileViewer: React.FC<Props> = ({ file, onOpenNotebook }) => {
- const { theme } = useTheme();
const { sendFileDetails, sendUpdateFile } = useRequestClient();
const hotkeys = useAtomValue(hotkeysAtom);
const disableFileDownloads = useAtomValue(disableFileDownloadsAtom);
@@ -105,7 +99,7 @@ export const FileViewer: React.FC<Props> = ({ file, onOpenNotebook }) => {
}
const mimeType = data.mimeType || "text/plain";
- const isEditable = mimeType in mimeToLanguage;
+ const isEditable = mimeType in MIME_TO_LANGUAGE;
const isActiveNotebook =
currentNotebookFilename &&
data.file.isMarimoFile &&
@@ -127,7 +121,7 @@ export const FileViewer: React.FC<Props> = ({ file, onOpenNotebook }) => {
}
const handleDownload = () => {
- if (isMedia(mimeType)) {
+ if (isMediaMime(mimeType)) {
const dataURL = base64ToDataURL(data.contents as Base64String, mimeType);
downloadByURL(dataURL, data.file.name);
return;
@@ -140,109 +134,57 @@ export const FileViewer: React.FC<Props> = ({ file, onOpenNotebook }) => {
};
const header = (
- <div className="text-xs text-muted-foreground p-1 flex justify-end gap-2 border-b">
- <Tooltip content="Refresh">
- <Button size="small" onClick={refetch}>
- <RefreshCwIcon />
- </Button>
- </Tooltip>
- {file.isMarimoFile && !isWasm() && (
- <Tooltip content="Open notebook">
- <Button size="small" onClick={(evt) => onOpenNotebook(evt)}>
- <ExternalLinkIcon />
- </Button>
- </Tooltip>
- )}
- {!disableFileDownloads && (
- <Tooltip content="Download">
- <Button size="small" onClick={handleDownload}>
- <DownloadIcon />
- </Button>
- </Tooltip>
- )}
- {!isMedia(mimeType) && (
+ <FilePreviewHeader
+ filename={data.file.name}
+ onRefresh={refetch}
+ onDownload={disableFileDownloads ? undefined : handleDownload}
+ actions={
<>
- <Tooltip content="Copy contents to clipboard">
- <Button
- size="small"
- onClick={async () => {
- await copyToClipboard(internalValue);
- }}
- >
- <CopyIcon />
- </Button>
- </Tooltip>
- <Tooltip content={renderShortcut("global.save")}>
- <Button
- size="small"
- color={internalValue === data.contents ? undefined : "green"}
- onClick={handleSaveFile}
- disabled={internalValue === data.contents}
- >
- <SaveIcon />
- </Button>
- </Tooltip>
+ {file.isMarimoFile && !isWasm() && (
+ <Tooltip content="Open notebook">
+ <Button
+ variant="text"
+ size="xs"
+ onClick={(evt) => onOpenNotebook(evt)}
+ >
+ <ExternalLinkIcon className="h-3.5 w-3.5" />
+ </Button>
+ </Tooltip>
+ )}
+ {!isMediaMime(mimeType) && (
+ <>
+ <Tooltip content="Copy contents to clipboard">
+ <Button
+ variant="text"
+ size="xs"
+ onClick={async () => {
+ await copyToClipboard(internalValue);
+ }}
+ >
+ <CopyIcon className="h-3.5 w-3.5" />
+ </Button>
+ </Tooltip>
+ <Tooltip content={renderShortcut("global.save")}>
+ <Button
+ variant={internalValue === data.contents ? "text" : "success"}
+ size="xs"
+ onClick={handleSaveFile}
+ disabled={internalValue === data.contents}
+ >
+ <SaveIcon className="h-3.5 w-3.5" />
+ </Button>
+ </Tooltip>
+ </>
+ )}
</>
- )}
- </div>
+ }
+ />
);
- if (mimeType.startsWith("image/")) {
- return (
- <>
- {header}
- <div className="flex-1 overflow-hidden flex flex-col">
- <ImageViewer base64={data.contents as Base64String} mime={mimeType} />
- </div>
- </>
- );
- }
-
- if (mimeType === "text/csv" && data.contents) {
- return (
- <>
- {header}
- <div className="flex-1 overflow-hidden flex flex-col">
- <CsvViewer contents={data.contents} />
- </div>
- </>
- );
- }
-
- if (mimeType.startsWith("audio/")) {
- return (
- <>
- {header}
- <div className="flex-1 overflow-hidden flex flex-col">
- <AudioViewer base64={data.contents as Base64String} mime={mimeType} />
- </div>
- </>
- );
- }
-
- if (mimeType.startsWith("video/")) {
- return (
- <>
- {header}
- <div className="flex-1 overflow-hidden flex flex-col">
- <VideoViewer base64={data.contents as Base64String} mime={mimeType} />
- </div>
- </>
- );
- }
-
- if (mimeType.startsWith("application/pdf")) {
- return (
- <>
- {header}
- <div className="flex-1 overflow-hidden flex flex-col">
- <PdfViewer base64={data.contents as Base64String} mime={mimeType} />
- </div>
- </>
- );
- }
+ const isMedia = isMediaMime(mimeType);
+ const isText = !isMedia && mimeType !== "text/csv";
- const warningBanner = isActiveNotebook && (
+ const warningBanner = isText && isActiveNotebook && (
<Alert variant="warning" className="rounded-none">
<AlertTriangleIcon className="h-4 w-4" />
<AlertDescription>
@@ -256,60 +198,39 @@ export const FileViewer: React.FC<Props> = ({ file, onOpenNotebook }) => {
<>
{header}
{warningBanner}
- <div className="flex-1 overflow-auto">
- <Suspense>
- <LazyAnyLanguageCodeMirror
- theme={theme === "dark" ? "dark" : "light"}
- language={mimeToLanguage[mimeType] || mimeToLanguage.default}
- className="border-b"
- extensions={[
- EditorView.lineWrapping,
- // Command S for save
- keymap.of([
- {
- key: hotkeys.getHotkey("global.save").key,
- stopPropagation: true,
- run: () => {
- if (internalValue !== data.contents) {
- handleSaveFile();
- return true;
- }
- return false;
- },
- },
- ]),
- ]}
- value={internalValue}
- onChange={setInternalValue}
- />
- </Suspense>
- </div>
+ <FileContentRenderer
+ mimeType={mimeType}
+ contents={
+ isMedia
+ ? undefined
+ : isText
+ ? internalValue
+ : (data.contents ?? undefined)
+ }
+ mediaSource={
+ isMedia
+ ? { base64: data.contents as Base64String, mime: mimeType }
+ : undefined
+ }
+ readOnly={!isText}
+ onChange={setInternalValue}
+ extensions={[
+ // Command S for save
+ keymap.of([
+ {
+ key: hotkeys.getHotkey("global.save").key,
+ stopPropagation: true,
+ run: () => {
+ if (internalValue !== data.contents) {
+ handleSaveFile();
+ return true;
+ }
+ return false;
+ },
+ },
+ ]),
+ ]}
+ />
</>
);
};
-
-const isMedia = (mime: string) => {
- if (!mime) {
- return false;
- }
- return (
- mime.startsWith("image/") ||
- mime.startsWith("audio/") ||
- mime.startsWith("video/") ||
- mime.startsWith("application/pdf")
- );
-};
-
-const mimeToLanguage: Record<string, string> = {
- "application/javascript": "javascript",
- "text/markdown": "markdown",
- "text/html": "html",
- "text/css": "css",
- "text/x-python": "python",
- "application/json": "json",
- "application/xml": "xml",
- "text/x-yaml": "yaml",
- "text/csv": "markdown",
- "text/plain": "markdown",
- default: "markdown",
-};
diff --git a/frontend/src/components/editor/file-tree/renderers.tsx b/frontend/src/components/editor/file-tree/renderers.tsx
index ef2099e129..1284343ffa 100644
--- a/frontend/src/components/editor/file-tree/renderers.tsx
+++ b/frontend/src/components/editor/file-tree/renderers.tsx
@@ -1,20 +1,65 @@
/* Copyright 2026 Marimo. All rights reserved. */
+import type { Extension } from "@codemirror/state";
+import { EditorView } from "@codemirror/view";
import type { PaginationState } from "@tanstack/react-table";
import type React from "react";
-import { useMemo, useState } from "react";
+import { Suspense, useMemo, useState } from "react";
import {
generateColumns,
inferFieldTypes,
} from "@/components/data-table/columns";
import { DataTable } from "@/components/data-table/data-table";
+import { LazyAnyLanguageCodeMirror } from "@/plugins/impl/code/LazyAnyLanguageCodeMirror";
import { parseCsvData } from "@/plugins/impl/vega/loader";
+import { useTheme } from "@/theme/useTheme";
import { Arrays } from "@/utils/arrays";
import { type Base64String, base64ToDataURL } from "@/utils/json/base64";
import { Objects } from "@/utils/objects";
const PAGE_SIZE = 25;
+export function isMediaMime(mime: string): boolean {
+ if (!mime) {
+ return false;
+ }
+ return (
+ mime.startsWith("image/") ||
+ mime.startsWith("audio/") ||
+ mime.startsWith("video/") ||
+ mime.startsWith("application/pdf")
+ );
+}
+
+export const MIME_TO_LANGUAGE: Record<string, string> = {
+ "application/javascript": "javascript",
+ "text/markdown": "markdown",
+ "text/html": "html",
+ "text/css": "css",
+ "text/x-python": "python",
+ "application/json": "json",
+ "application/xml": "xml",
+ "text/x-yaml": "yaml",
+ "text/csv": "markdown",
+ "text/plain": "markdown",
+ default: "markdown",
+};
+
+/**
+ * Media viewer props: provide either a direct `url` or `base64` + `mime`.
+ * When `url` is set it takes precedence.
+ */
+export type MediaSource =
+ | { url: string; base64?: undefined; mime?: string }
+ | { url?: undefined; base64: Base64String; mime: string };
+
+function resolveMediaSrc(source: MediaSource): string {
+ if (source.url != null) {
+ return source.url;
+ }
+ return base64ToDataURL(source.base64, source.mime);
+}
+
export const CsvViewer: React.FC<{ contents: string }> = ({ contents }) => {
const data = useMemo(() => parseCsvData(contents), [contents]);
const [pagination, setPagination] = useState<PaginationState>({
@@ -48,36 +93,112 @@ export const CsvViewer: React.FC<{ contents: string }> = ({ contents }) => {
/>
);
};
-export const ImageViewer: React.FC<{ base64: Base64String; mime: string }> = ({
- mime,
- base64,
-}) => {
- return <img src={base64ToDataURL(base64, mime)} alt="Preview" />;
+
+export const ImageViewer: React.FC<MediaSource> = (props) => {
+ return <img src={resolveMediaSrc(props)} alt="Preview" />;
};
-export const AudioViewer: React.FC<{ base64: Base64String; mime: string }> = ({
- mime,
- base64,
-}) => {
- return <audio controls={true} src={base64ToDataURL(base64, mime)} />;
+export const AudioViewer: React.FC<MediaSource> = (props) => {
+ return <audio controls={true} src={resolveMediaSrc(props)} />;
};
-export const VideoViewer: React.FC<{ base64: Base64String; mime: string }> = ({
- mime,
- base64,
-}) => {
- return <video controls={true} src={base64ToDataURL(base64, mime)} />;
+export const VideoViewer: React.FC<MediaSource> = (props) => {
+ return <video controls={true} src={resolveMediaSrc(props)} />;
};
-export const PdfViewer: React.FC<{ base64: Base64String; mime: string }> = ({
- mime,
- base64,
-}) => {
+export const PdfViewer: React.FC<MediaSource> = (props) => {
return (
<iframe
- src={base64ToDataURL(base64, mime)}
+ src={resolveMediaSrc(props)}
title="PDF Viewer"
className="w-full h-full"
/>
);
};
+
+/**
+ * Renders the appropriate media viewer for the given mime type.
+ * Accepts either a `url` or `base64` + `mime` via {@link MediaSource}.
+ */
+export const MediaRenderer: React.FC<MediaSource & { mimeType: string }> = ({
+ mimeType,
+ ...source
+}) => {
+ if (mimeType.startsWith("image/")) {
+ return <ImageViewer {...source} />;
+ }
+ if (mimeType.startsWith("audio/")) {
+ return <AudioViewer {...source} />;
+ }
+ if (mimeType.startsWith("video/")) {
+ return <VideoViewer {...source} />;
+ }
+ if (mimeType.startsWith("application/pdf")) {
+ return <PdfViewer {...source} />;
+ }
+ return null;
+};
+
+/**
+ * Unified content renderer that dispatches to the appropriate viewer
+ * based on MIME type. Used by both the local file viewer and the
+ * storage file viewer.
+ */
+export const FileContentRenderer: React.FC<{
+ mimeType: string;
+ /** Text content for text/CSV files. */
+ contents?: string;
+ /** Media source for image/audio/video/PDF files. */
+ mediaSource?: MediaSource;
+ readOnly?: boolean;
+ onChange?: (value: string) => void;
+ /** Additional CodeMirror extensions (e.g. save hotkey). */
+ extensions?: Extension[];
+}> = ({
+ mimeType,
+ contents,
+ mediaSource,
+ readOnly = true,
+ onChange,
+ extensions = [],
+}) => {
+ const { theme } = useTheme();
+
+ if (mimeType === "text/csv" && contents != null) {
+ return (
+ <div className="flex-1 overflow-hidden flex flex-col">
+ <CsvViewer contents={contents} />
+ </div>
+ );
+ }
+
+ if (isMediaMime(mimeType) && mediaSource) {
+ return (
+ <div className="flex-1 overflow-hidden flex flex-col">
+ <MediaRenderer {...mediaSource} mimeType={mimeType} />
+ </div>
+ );
+ }
+
+ if (contents != null) {
+ const language = MIME_TO_LANGUAGE[mimeType] || MIME_TO_LANGUAGE.default;
+ return (
+ <div className="flex-1 overflow-auto">
+ <Suspense>
+ <LazyAnyLanguageCodeMirror
+ theme={theme === "dark" ? "dark" : "light"}
+ language={language}
+ className="border-b"
+ extensions={[EditorView.lineWrapping, ...extensions]}
+ value={contents}
+ readOnly={readOnly}
+ editable={readOnly ? false : undefined}
+ onChange={readOnly ? undefined : onChange}
+ />
+ </Suspense>
+ </div>
+ );
+ }
+
+ return null;
+};
diff --git a/frontend/src/components/storage/storage-file-viewer.tsx b/frontend/src/components/storage/storage-file-viewer.tsx
new file mode 100644
index 0000000000..9f62d93e7f
--- /dev/null
+++ b/frontend/src/components/storage/storage-file-viewer.tsx
@@ -0,0 +1,225 @@
+/* Copyright 2026 Marimo. All rights reserved. */
+
+import { FileIcon, LoaderCircle, RefreshCwIcon } from "lucide-react";
+import type React from "react";
+import { useCallback } from "react";
+import { useLocale } from "react-aria";
+import { FilePreviewHeader } from "@/components/editor/file-tree/file-header";
+import {
+ FileContentRenderer,
+ isMediaMime,
+} from "@/components/editor/file-tree/renderers";
+import { toast } from "@/components/ui/use-toast";
+import { DownloadStorage } from "@/core/storage/request-registry";
+import type { StorageEntry } from "@/core/storage/types";
+import { useAsyncData } from "@/hooks/useAsyncData";
+import { downloadByURL } from "@/utils/download";
+import { formatBytes } from "@/utils/formatting";
+import { Logger } from "@/utils/Logger";
+import { CopyClipboardIcon } from "../icons/copy-icon";
+import { Button } from "../ui/button";
+import { renderFileIcon } from "./components";
+
+const MAX_MEDIA_PREVIEW_SIZE = 100 * 1024 * 1024; // 100 MB
+
+interface Props {
+ entry: StorageEntry;
+ namespace: string;
+ onBack: () => void;
+}
+
+function displayName(path: string): string {
+ const trimmed = path.endsWith("/") ? path.slice(0, -1) : path;
+ const parts = trimmed.split("/");
+ return parts[parts.length - 1] || trimmed;
+}
+
+type PreviewData =
+ | { type: "media"; url: string }
+ | { type: "text"; content: string };
+
+export const StorageFileViewer: React.FC<Props> = ({
+ entry,
+ namespace,
+ onBack,
+}) => {
+ const { locale } = useLocale();
+ const name = displayName(entry.path);
+ const mime = entry.mimeType || "text/plain";
+ const isMedia = isMediaMime(mime);
+ const tooLargeForMedia =
+ isMedia && entry.size > MAX_MEDIA_PREVIEW_SIZE && entry.size > 0;
+
+ const {
+ data: preview,
+ isPending,
+ error,
+ refetch,
+ } = useAsyncData(async (): Promise<PreviewData | null> => {
+ if (tooLargeForMedia) {
+ return null;
+ }
+
+ const result = await DownloadStorage.request({
+ namespace,
+ path: entry.path,
+ preview: !isMedia,
+ });
+ if (result.error) {
+ throw new Error(result.error);
+ }
+ if (!result.url) {
+ throw new Error("No URL returned");
+ }
+
+ if (isMedia) {
+ return { type: "media", url: result.url };
+ }
+
+ const resp = await fetch(result.url);
+ if (!resp.ok) {
+ throw new Error(`Failed to fetch preview: ${resp.statusText}`);
+ }
+ const content = await resp.text();
+ return { type: "text", content };
+ }, [namespace, entry.path, isMedia, tooLargeForMedia]);
+
+ const handleDownload = useCallback(async () => {
+ try {
+ const result = await DownloadStorage.request({
+ namespace,
+ path: entry.path,
+ });
+ if (result.error) {
+ toast({
+ title: "Download failed",
+ description: result.error,
+ variant: "danger",
+ });
+ return;
+ }
+ if (result.url) {
+ downloadByURL(result.url, result.filename ?? name);
+ }
+ } catch (error_) {
+ Logger.error("Failed to download storage entry", error_);
+ toast({
+ title: "Download failed",
+ description: String(error_),
+ variant: "danger",
+ });
+ }
+ }, [namespace, entry.path, name]);
+
+ const header = (
+ <FilePreviewHeader
+ filename={name}
+ filenameIcon={renderFileIcon(name)}
+ onBack={onBack}
+ onDownload={handleDownload}
+ />
+ );
+
+ const renderMetadata = ({
+ includeMime = false,
+ }: {
+ includeMime?: boolean;
+ }) => {
+ return (
+ <div className="grid grid-cols-[auto_1fr] gap-x-4 gap-y-1.5 p-4 text-xs">
+ <span className="text-muted-foreground font-medium">Path</span>
+ <div className="truncate flex items-center gap-1.5">
+ <span className="font-mono text-[11px]">{entry.path}</span>
+ <CopyClipboardIcon value={entry.path} className="h-3 w-3" />
+ </div>
+
+ {includeMime && (
+ <span className="text-muted-foreground font-medium">Type</span>
+ )}
+ {includeMime && <span>{mime}</span>}
+ {entry.size > 0 && (
+ <>
+ <span className="text-muted-foreground font-medium">Size</span>
+ <span>{formatBytes(entry.size, locale)}</span>
+ </>
+ )}
+ {entry.lastModified != null && (
+ <>
+ <span className="text-muted-foreground font-medium">Modified</span>
+ <span>{new Date(entry.lastModified * 1000).toLocaleString()}</span>
+ </>
+ )}
+ </div>
+ );
+ };
+
+ if (tooLargeForMedia) {
+ return (
+ <div className="flex flex-col h-full">
+ {header}
+ {renderMetadata({ includeMime: true })}
+ <div className="px-4 pb-4 text-xs text-muted-foreground italic">
+ File is too large to preview ({formatBytes(entry.size, locale)}).
+ </div>
+ </div>
+ );
+ }
+
+ if (isPending) {
+ return (
+ <div className="flex flex-col h-full">
+ {header}
+ {renderMetadata({})}
+ <div className="flex-1 flex items-center justify-center gap-2 text-xs text-muted-foreground min-h-24">
+ <LoaderCircle className="h-4 w-4 animate-spin" />
+ Loading preview...
+ </div>
+ </div>
+ );
+ }
+
+ if (error) {
+ return (
+ <div className="flex flex-col h-full">
+ {header}
+ {renderMetadata({ includeMime: true })}
+ <div className="px-4 pb-4 text-xs text-destructive">
+ Failed to load preview: {error.message}
+ </div>
+ <div className="px-4 pb-4">
+ <Button variant="secondary" size="xs" onClick={refetch}>
+ <RefreshCwIcon className="h-3 w-3 mr-1" />
+ Retry
+ </Button>
+ </div>
+ </div>
+ );
+ }
+
+ if (preview) {
+ return (
+ <div className="flex flex-col h-full">
+ {header}
+ {renderMetadata({})}
+ <FileContentRenderer
+ mimeType={mime}
+ contents={preview.type === "text" ? preview.content : undefined}
+ mediaSource={
+ preview.type === "media" ? { url: preview.url } : undefined
+ }
+ />
+ </div>
+ );
+ }
+
+ return (
+ <div className="flex flex-col h-full">
+ {header}
+ {renderMetadata({ includeMime: true })}
+ <div className="p-4 flex items-center gap-2 text-xs text-muted-foreground">
+ <FileIcon className="h-4 w-4" />
+ Preview not available for this file type.
+ </div>
+ </div>
+ );
+};
diff --git a/frontend/src/components/storage/storage-inspector.tsx b/frontend/src/components/storage/storage-inspector.tsx
index 5764b5b747..11de822425 100644
--- a/frontend/src/components/storage/storage-inspector.tsx
+++ b/frontend/src/components/storage/storage-inspector.tsx
@@ -11,6 +11,7 @@ import {
LoaderCircle,
MoreVerticalIcon,
RefreshCwIcon,
+ ViewIcon,
XIcon,
} from "lucide-react";
import React, { useCallback, useState } from "react";
@@ -47,6 +48,12 @@ import { Logger } from "@/utils/Logger";
import { ErrorState } from "../datasources/components";
import { Button } from "../ui/button";
import { ProtocolIcon, renderFileIcon } from "./components";
+import { StorageFileViewer } from "./storage-file-viewer";
+
+interface OpenFileInfo {
+ entry: StorageEntry;
+ namespace: string;
+}
// Pixels per depth level. Applied as paddingLeft on each full-width item
// so the selection highlight still spans the entire panel.
@@ -133,6 +140,7 @@ const StorageEntryChildren: React.FC<{
depth: number;
locale: string;
searchValue: string;
+ onOpenFile: (info: OpenFileInfo) => void;
}> = ({
namespace,
protocol,
@@ -141,6 +149,7 @@ const StorageEntryChildren: React.FC<{
depth,
locale,
searchValue,
+ onOpenFile,
}) => {
const { entriesByPath } = useStorage();
const {
@@ -199,6 +208,7 @@ const StorageEntryChildren: React.FC<{
depth={depth}
locale={locale}
searchValue={searchValue}
+ onOpenFile={onOpenFile}
/>
))}
</>
@@ -213,7 +223,17 @@ const StorageEntryRow: React.FC<{
depth: number;
locale: string;
searchValue: string;
-}> = ({ entry, namespace, protocol, rootPath, depth, locale, searchValue }) => {
+ onOpenFile: (info: OpenFileInfo) => void;
+}> = ({
+ entry,
+ namespace,
+ protocol,
+ rootPath,
+ depth,
+ locale,
+ searchValue,
+ onOpenFile,
+}) => {
const [isExpanded, setIsExpanded] = useState(false);
const { entriesByPath } = useStorage();
const isDir = entry.kind === "directory";
@@ -272,6 +292,8 @@ const StorageEntryRow: React.FC<{
onSelect={() => {
if (isDir) {
setIsExpanded(!effectiveExpanded);
+ } else {
+ onOpenFile({ entry, namespace });
}
}}
>
@@ -322,6 +344,14 @@ const StorageEntryRow: React.FC<{
onClick={(e) => e.stopPropagation()}
onCloseAutoFocus={(e) => e.preventDefault()}
>
+ {!isDir && (
+ <DropdownMenuItem
+ onSelect={() => onOpenFile({ entry, namespace })}
+ >
+ <ViewIcon className="h-3.5 w-3.5 mr-2" />
+ View
+ </DropdownMenuItem>
+ )}
<DropdownMenuItem
onSelect={async () => {
const url = storageUrl(protocol, rootPath, entry.path);
@@ -351,6 +381,7 @@ const StorageEntryRow: React.FC<{
depth={depth + 1}
locale={locale}
searchValue={searchValue}
+ onOpenFile={onOpenFile}
/>
)}
</>
@@ -361,7 +392,8 @@ const StorageNamespaceSection: React.FC<{
namespace: StorageNamespace;
locale: string;
searchValue: string;
-}> = ({ namespace, locale, searchValue }) => {
+ onOpenFile: (info: OpenFileInfo) => void;
+}> = ({ namespace, locale, searchValue, onOpenFile }) => {
const [isExpanded, setIsExpanded] = useState(true);
const [isSpinning, setIsSpinning] = useState(false);
const { entriesByPath } = useStorage();
@@ -479,6 +511,7 @@ const StorageNamespaceSection: React.FC<{
depth={1}
locale={locale}
searchValue={searchValue}
+ onOpenFile={onOpenFile}
/>
))}
</>
@@ -491,6 +524,7 @@ export const StorageInspector: React.FC = () => {
const { namespaces } = useStorage();
const { locale } = useLocale();
const [searchValue, setSearchValue] = useState("");
+ const [openFile, setOpenFile] = useState<OpenFileInfo | null>(null);
const hasSearch = !!searchValue.trim();
if (namespaces.length === 0) {
@@ -517,45 +551,60 @@ export const StorageInspector: React.FC = () => {
}
return (
- <Command
- className="border-b bg-background rounded-none h-full pb-10 overflow-auto outline-hidden scrollbar-thin"
- shouldFilter={false}
- >
- <div className="flex items-center w-full border-b">
- <CommandInput
- placeholder="Search entries..."
- className="h-6 m-1"
- value={searchValue}
- onValueChange={setSearchValue}
- rootClassName="flex-1 border-b-0"
+ <div className="h-full flex flex-col">
+ {openFile && (
+ <StorageFileViewer
+ entry={openFile.entry}
+ namespace={openFile.namespace}
+ onBack={() => setOpenFile(null)}
/>
- {hasSearch && (
- <Button
- variant="text"
- size="xs"
- className="float-right border-none px-2 m-0 h-full"
- onClick={() => setSearchValue("")}
- >
- <XIcon className="h-4 w-4" />
- </Button>
+ )}
+ <Command
+ className={cn(
+ "border-b bg-background rounded-none h-full pb-10 overflow-auto outline-hidden scrollbar-thin",
+ // We want to keep the command open but hidden when a file is opened to preserve state (folders opened etc.)
+ // TODO: Preserve scroll position
+ openFile && "hidden",
)}
- <Tooltip
- content="Filters loaded entries only. Expand directories to include their contents in the search."
- delayDuration={200}
- >
- <HelpCircleIcon className="h-3.5 w-3.5 mr-2 shrink-0 cursor-help text-muted-foreground hover:text-foreground" />
- </Tooltip>
- </div>
- <CommandList className="flex flex-col">
- {namespaces.map((ns) => (
- <StorageNamespaceSection
- key={ns.name ?? ns.displayName}
- namespace={ns}
- locale={locale}
- searchValue={searchValue}
+ shouldFilter={false}
+ >
+ <div className="flex items-center w-full border-b">
+ <CommandInput
+ placeholder="Search entries..."
+ className="h-6 m-1"
+ value={searchValue}
+ onValueChange={setSearchValue}
+ rootClassName="flex-1 border-b-0"
/>
- ))}
- </CommandList>
- </Command>
+ {hasSearch && (
+ <Button
+ variant="text"
+ size="xs"
+ className="float-right border-none px-2 m-0 h-full"
+ onClick={() => setSearchValue("")}
+ >
+ <XIcon className="h-4 w-4" />
+ </Button>
+ )}
+ <Tooltip
+ content="Filters loaded entries only. Expand directories to include their contents in the search."
+ delayDuration={200}
+ >
+ <HelpCircleIcon className="h-3.5 w-3.5 mr-2 shrink-0 cursor-help text-muted-foreground hover:text-foreground" />
+ </Tooltip>
+ </div>
+ <CommandList className="flex flex-col">
+ {namespaces.map((ns) => (
+ <StorageNamespaceSection
+ key={ns.name ?? ns.displayName}
+ namespace={ns}
+ locale={locale}
+ searchValue={searchValue}
+ onOpenFile={setOpenFile}
+ />
+ ))}
+ </CommandList>
+ </Command>
+ </div>
);
};
diff --git a/marimo/_data/_external_storage/models.py b/marimo/_data/_external_storage/models.py
index 058aec532f..7c3b018409 100644
--- a/marimo/_data/_external_storage/models.py
+++ b/marimo/_data/_external_storage/models.py
@@ -26,6 +26,7 @@ class StorageEntry(msgspec.Struct, rename="camel"):
size: The size of the storage entry.
last_modified: The last modified time of the storage entry.
metadata: The metadata of the storage entry.
+ mime_type: The MIME type of the storage entry, or None for directories.
"""
path: str
@@ -33,6 +34,7 @@ class StorageEntry(msgspec.Struct, rename="camel"):
size: int
last_modified: float | None
metadata: dict[str, Any] = msgspec.field(default_factory=dict)
+ mime_type: str | None = None
class StorageNamespace(msgspec.Struct, rename="camel"):
@@ -99,6 +101,12 @@ class StorageBackend(abc.ABC, Generic[Backend]):
async def download(self, path: str) -> bytes:
"""Download the file at the given path."""
+ @abc.abstractmethod
+ async def read_range(
+ self, path: str, *, offset: int = 0, length: int | None = None
+ ) -> bytes:
+ """Read a byte range from the file. If length is None, read the entire file."""
+
@abc.abstractmethod
async def sign_download_url(
self, path: str, expiration: int = SIGNED_URL_EXPIRATION
diff --git a/marimo/_data/_external_storage/storage.py b/marimo/_data/_external_storage/storage.py
index 56dec4d98d..52b5757847 100644
--- a/marimo/_data/_external_storage/storage.py
+++ b/marimo/_data/_external_storage/storage.py
@@ -2,6 +2,7 @@
from __future__ import annotations
import asyncio
+import mimetypes
from datetime import timedelta
from typing import TYPE_CHECKING, Any, Literal, cast
@@ -86,6 +87,7 @@ class Obstore(StorageBackend["ObjectStore"]):
last_modified=last_modified.timestamp() if last_modified else None,
kind="object",
metadata=entry_meta,
+ mime_type=mimetypes.guess_type(path or "")[0],
)
async def download(self, path: str) -> bytes:
@@ -93,6 +95,20 @@ class Obstore(StorageBackend["ObjectStore"]):
bytes_data = await result.bytes_async()
return bytes(bytes_data)
+ async def read_range(
+ self, path: str, *, offset: int = 0, length: int | None = None
+ ) -> bytes:
+ if length is None:
+ data = await self.download(path)
+ return data[offset:]
+ from obstore import get_range_async
+
+ return bytes(
+ await get_range_async(
+ self.store, path, start=offset, length=length
+ )
+ )
+
async def sign_download_url(
self, path: str, expiration: int = SIGNED_URL_EXPIRATION
) -> str | None:
@@ -252,12 +268,16 @@ class FsspecFilesystem(StorageBackend["AbstractFileSystem"]):
else:
resolved_kind = self._identify_kind(entry_type)
+ resolved_path = name or ""
return StorageEntry(
- path=name or "",
+ path=resolved_path,
size=size or 0,
last_modified=file.get("mtime"),
kind=resolved_kind,
metadata=entry_meta,
+ mime_type=mimetypes.guess_type(resolved_path)[0]
+ if resolved_kind != "directory"
+ else None,
)
async def download(self, path: str) -> bytes:
@@ -270,6 +290,17 @@ class FsspecFilesystem(StorageBackend["AbstractFileSystem"]):
return file.encode("utf-8")
return file
+ async def read_range(
+ self, path: str, *, offset: int = 0, length: int | None = None
+ ) -> bytes:
+ end = offset + length if length is not None else None
+ data = await asyncio.to_thread(
+ self.store.cat_file, path, start=offset, end=end
+ )
+ if isinstance(data, str):
+ return data.encode("utf-8")
+ return bytes(data)
+
async def sign_download_url(
self, path: str, expiration: int = SIGNED_URL_EXPIRATION
) -> str | None:
diff --git a/marimo/_runtime/commands.py b/marimo/_runtime/commands.py
index c48907c0dc..a5d3916ac8 100644
--- a/marimo/_runtime/commands.py
+++ b/marimo/_runtime/commands.py
@@ -692,18 +692,21 @@ class StorageListEntriesCommand(Command):
class StorageDownloadCommand(Command):
"""Download a storage entry.
- Downloads file bytes from storage and creates a virtual file
+ Obtains a pre-signed URL or downloads the file locally and returns a virtual file URL
so the frontend can fetch the contents.
Attributes:
request_id: Unique identifier for this request.
namespace: Variable name identifying the storage backend.
path: Full path of the entry to download.
+ preview: If true, a local preview of the file is returned.
+ This is useful if you need to bypass CORS.
"""
request_id: RequestId
namespace: str
path: str
+ preview: bool = False
class ListSecretKeysCommand(Command):
diff --git a/marimo/_runtime/runtime.py b/marimo/_runtime/runtime.py
index 54c00e5025..5809883c8f 100644
--- a/marimo/_runtime/runtime.py
+++ b/marimo/_runtime/runtime.py
@@ -179,6 +179,7 @@ from marimo._runtime.state import State
from marimo._runtime.utils.set_ui_element_request_manager import (
SetUIElementRequestManager,
)
+from marimo._runtime.virtual_file.virtual_file import VirtualFile
from marimo._runtime.win32_interrupt_handler import Win32InterruptHandler
from marimo._secrets.load_dotenv import (
load_dotenv_with_fallback,
@@ -212,7 +213,6 @@ if TYPE_CHECKING:
from types import ModuleType
from marimo._plugins.ui._core.ui_element import UIElement
- from marimo._runtime.virtual_file.virtual_file import VirtualFile
LOGGER = _loggers.marimo_logger()
@@ -2756,9 +2756,14 @@ class ExternalStorageCallbacks:
),
)
+ _PREVIEW_MAX_BYTES = 1_000_000 # 1 MB
+
@kernel_tracer.start_as_current_span("storage_download")
async def download(self, request: StorageDownloadCommand) -> None:
- """Download a storage entry, preferring a signed URL."""
+ """
+ Download a storage entry, preferring a signed URL.
+ If preview is true, downloads the first 1MB of the file and returns a same-origin virtual file URL.
+ """
backend, error = self._get_storage_backend(request.namespace)
if error is not None or backend is None:
broadcast_notification(
@@ -2774,34 +2779,10 @@ class ExternalStorageCallbacks:
filename = request.path.rsplit("/", 1)[-1] or "download"
try:
- signed_url = await backend.sign_download_url(request.path)
- if signed_url is not None:
- broadcast_notification(
- StorageDownloadReadyNotification(
- request_id=request.request_id,
- url=signed_url,
- filename=filename,
- ),
- )
- return
-
- # Signing not supported; fall back to virtual file with TTL
- from marimo._runtime.virtual_file.virtual_file import VirtualFile
-
- result = await backend.download_file(request.path)
- vfile = VirtualFile.create_and_register(
- result.file_bytes,
- result.ext,
- )
- self._schedule_vfile_cleanup(vfile)
-
- broadcast_notification(
- StorageDownloadReadyNotification(
- request_id=request.request_id,
- url=vfile.url,
- filename=result.filename,
- ),
- )
+ if request.preview:
+ await self._download_preview(backend, request, filename)
+ else:
+ await self._download_full(backend, request, filename)
except Exception as e:
LOGGER.exception(
"Failed to download %s from %s",
@@ -2817,6 +2798,58 @@ class ExternalStorageCallbacks:
),
)
+ async def _download_full(
+ self,
+ backend: StorageBackend[Any],
+ request: StorageDownloadCommand,
+ filename: str,
+ ) -> None:
+ signed_url = await backend.sign_download_url(request.path)
+ if signed_url is not None:
+ broadcast_notification(
+ StorageDownloadReadyNotification(
+ request_id=request.request_id,
+ url=signed_url,
+ filename=filename,
+ ),
+ )
+ return
+
+ # Signing not supported; fall back to virtual file with TTL
+ result = await backend.download_file(request.path)
+ vfile = VirtualFile.create_and_register(result.file_bytes, result.ext)
+ self._schedule_vfile_cleanup(vfile)
+
+ broadcast_notification(
+ StorageDownloadReadyNotification(
+ request_id=request.request_id,
+ url=vfile.url,
+ filename=result.filename,
+ ),
+ )
+
+ async def _download_preview(
+ self,
+ backend: StorageBackend[Any],
+ request: StorageDownloadCommand,
+ filename: str,
+ ) -> None:
+ """Read partial content and serve via a virtual file with TTL. This is useful to bypass CORS."""
+ data = await backend.read_range(
+ request.path, offset=0, length=self._PREVIEW_MAX_BYTES
+ )
+ _, ext = os.path.splitext(filename)
+ vfile = VirtualFile.create_and_register(data, ext.lstrip(".") or "txt")
+ self._schedule_vfile_cleanup(vfile)
+
+ broadcast_notification(
+ StorageDownloadReadyNotification(
+ request_id=request.request_id,
+ url=vfile.url,
+ filename=filename,
+ ),
+ )
+
class SqlCallbacks:
def __init__(self, kernel: Kernel):
diff --git a/marimo/_server/models/models.py b/marimo/_server/models/models.py
index f0d7b5d107..a4060ee48f 100644
--- a/marimo/_server/models/models.py
+++ b/marimo/_server/models/models.py
@@ -153,6 +153,7 @@ class StorageDownloadRequest(StorageDownloadCommand, tag=False):
request_id=self.request_id,
namespace=self.namespace,
path=self.path,
+ preview=self.preview,
)
diff --git a/packages/openapi/api.yaml b/packages/openapi/api.yaml
index 89a5b83ef2..9ee4f31304 100644
--- a/packages/openapi/api.yaml
+++ b/packages/openapi/api.yaml
@@ -4257,16 +4257,21 @@ components:
title: StopKernelCommand
type: object
StorageDownloadCommand:
- description: "Download a storage entry.\n\n Downloads file bytes from storage\
- \ and creates a virtual file\n so the frontend can fetch the contents.\n\
- \n Attributes:\n request_id: Unique identifier for this request.\n\
- \ namespace: Variable name identifying the storage backend.\n \
- \ path: Full path of the entry to download."
+ description: "Download a storage entry.\n\n Obtains a pre-signed URL or downloads\
+ \ the file locally and returns a virtual file URL\n so the frontend can\
+ \ fetch the contents.\n\n Attributes:\n request_id: Unique identifier\
+ \ for this request.\n namespace: Variable name identifying the storage\
+ \ backend.\n path: Full path of the entry to download.\n preview:\
+ \ If true, a local preview of the file is returned.\n This is useful\
+ \ if you need to bypass CORS."
properties:
namespace:
type: string
path:
type: string
+ preview:
+ default: false
+ type: boolean
requestId:
type: string
type:
@@ -4318,6 +4323,9 @@ components:
type: string
path:
type: string
+ preview:
+ default: false
+ type: boolean
requestId:
type: string
required:
@@ -4372,7 +4380,8 @@ components:
\ systems\n\n Attributes:\n path: The path of the storage entry.\n\
\ kind: The kind of the storage entry.\n size: The size of the\
\ storage entry.\n last_modified: The last modified time of the storage\
- \ entry.\n metadata: The metadata of the storage entry."
+ \ entry.\n metadata: The metadata of the storage entry.\n mime_type:\
+ \ The MIME type of the storage entry, or None for directories."
properties:
kind:
enum:
@@ -4386,6 +4395,11 @@ components:
metadata:
default: {}
type: object
+ mimeType:
+ anyOf:
+ - type: string
+ - type: 'null'
+ default: null
path:
type: string
size:
@@ -6519,3 +6533,4 @@ paths:
summary: Submit login form
tags:
- auth
+
diff --git a/packages/openapi/src/api.ts b/packages/openapi/src/api.ts
index 242b858547..31b7bd303c 100644
--- a/packages/openapi/src/api.ts
+++ b/packages/openapi/src/api.ts
@@ -5707,17 +5707,21 @@ export interface components {
* StorageDownloadCommand
* @description Download a storage entry.
*
- * Downloads file bytes from storage and creates a virtual file
+ * Obtains a pre-signed URL or downloads the file locally and returns a virtual file URL
* so the frontend can fetch the contents.
*
* Attributes:
* request_id: Unique identifier for this request.
* namespace: Variable name identifying the storage backend.
* path: Full path of the entry to download.
+ * preview: If true, a local preview of the file is returned.
+ * This is useful if you need to bypass CORS.
*/
StorageDownloadCommand: {
namespace: string;
path: string;
+ /** @default false */
+ preview?: boolean;
requestId: string;
/** @enum {unknown} */
type: "storage-download";
@@ -5750,6 +5754,8 @@ export interface components {
StorageDownloadRequest: {
namespace: string;
path: string;
+ /** @default false */
+ preview?: boolean;
requestId: string;
};
/**
@@ -5787,6 +5793,7 @@ export interface components {
* size: The size of the storage entry.
* last_modified: The last modified time of the storage entry.
* metadata: The metadata of the storage entry.
+ * mime_type: The MIME type of the storage entry, or None for directories.
*/
StorageEntry: {
/** @enum {unknown} */
@@ -5794,6 +5801,8 @@ export interface components {
lastModified: number | null;
/** @default {} */
metadata?: Record<string, any>;
+ /** @default null */
+ mimeType?: string | null;
path: string;
size: number;
};
diff --git a/tests/_data/_external_storage/test_storage_models.py b/tests/_data/_external_storage/test_storage_models.py
index 8660eb80d2..e3f79726d8 100644
--- a/tests/_data/_external_storage/test_storage_models.py
+++ b/tests/_data/_external_storage/test_storage_models.py
@@ -65,6 +65,7 @@ class TestObstore:
size=0,
last_modified=None,
metadata={},
+ mime_type=None,
),
StorageEntry(
path="file1.txt",
@@ -72,6 +73,7 @@ class TestObstore:
size=100,
last_modified=now.timestamp(),
metadata={"e_tag": "abc"},
+ mime_type="text/plain",
),
StorageEntry(
path="dir/file2.txt",
@@ -79,6 +81,7 @@ class TestObstore:
size=200,
last_modified=now.timestamp(),
metadata={"version": "v1"},
+ mime_type="text/plain",
),
]
)
@@ -110,10 +113,11 @@ class TestObstore:
assert entry == snapshot(
StorageEntry(
path="",
+ kind="object",
size=0,
last_modified=None,
- kind="object",
metadata={},
+ mime_type=None,
)
)
@@ -134,10 +138,11 @@ class TestObstore:
assert entry == snapshot(
StorageEntry(
path="test.csv",
+ kind="object",
size=500,
last_modified=now.timestamp(),
- kind="object",
metadata={"e_tag": "etag123", "version": "v2"},
+ mime_type="text/csv",
)
)
@@ -160,10 +165,11 @@ class TestObstore:
assert result == snapshot(
StorageEntry(
path="test.txt",
+ kind="object",
size=42,
last_modified=now.timestamp(),
- kind="object",
metadata={"e_tag": "e1"},
+ mime_type="text/plain",
)
)
mock_store.head_async.assert_called_once_with("test.txt")
@@ -278,6 +284,72 @@ class TestObstore:
ext="bin",
)
+ async def test_read_range_full_file_delegates_to_download(self) -> None:
+ mock_store = MagicMock()
+ mock_bytes_result = MagicMock()
+ mock_bytes_result.bytes_async = MagicMock(
+ return_value=_async_return(b"full content")
+ )
+ mock_store.get_async = MagicMock(
+ return_value=_async_return(mock_bytes_result)
+ )
+
+ backend = self._make_backend(mock_store)
+ result = await backend.read_range("file.txt")
+ assert result == b"full content"
+ mock_store.get_async.assert_called_once_with("file.txt")
+
+ async def test_read_range_offset_without_length_slices_download(
+ self,
+ ) -> None:
+ mock_store = MagicMock()
+ mock_bytes_result = MagicMock()
+ mock_bytes_result.bytes_async = MagicMock(
+ return_value=_async_return(b"hello world")
+ )
+ mock_store.get_async = MagicMock(
+ return_value=_async_return(mock_bytes_result)
+ )
+
+ backend = self._make_backend(mock_store)
+ result = await backend.read_range("file.txt", offset=6)
+ assert result == b"world"
+ mock_store.get_async.assert_called_once_with("file.txt")
+
+ async def test_read_range_with_offset_and_length(self) -> None:
+ mock_store = MagicMock()
+ backend = self._make_backend(mock_store)
+
+ with patch(
+ "obstore.get_range_async",
+ new_callable=AsyncMock,
+ return_value=b"partial",
+ ) as mock_get_range:
+ result = await backend.read_range(
+ "file.txt", offset=10, length=100
+ )
+
+ assert result == b"partial"
+ mock_get_range.assert_called_once_with(
+ mock_store, "file.txt", start=10, length=100
+ )
+
+ async def test_read_range_with_length_only(self) -> None:
+ mock_store = MagicMock()
+ backend = self._make_backend(mock_store)
+
+ with patch(
+ "obstore.get_range_async",
+ new_callable=AsyncMock,
+ return_value=b"first bytes",
+ ) as mock_get_range:
+ result = await backend.read_range("file.txt", length=50)
+
+ assert result == b"first bytes"
+ mock_get_range.assert_called_once_with(
+ mock_store, "file.txt", start=0, length=50
+ )
+
def test_protocol_memory(self) -> None:
from obstore.store import MemoryStore
@@ -441,17 +513,19 @@ class TestFsspecFilesystem:
[
StorageEntry(
path="file1.txt",
+ kind="file",
size=100,
last_modified=1234567890.0,
- kind="file",
metadata={},
+ mime_type="text/plain",
),
StorageEntry(
path="subdir",
+ kind="directory",
size=0,
last_modified=1234567891.0,
- kind="directory",
metadata={},
+ mime_type=None,
),
]
)
@@ -485,10 +559,11 @@ class TestFsspecFilesystem:
[
StorageEntry(
path="file0.txt",
+ kind="file",
size=0,
last_modified=None,
- kind="file",
metadata={},
+ mime_type="text/plain",
),
StorageEntry(
path="file1.txt",
@@ -496,6 +571,7 @@ class TestFsspecFilesystem:
size=10,
last_modified=None,
metadata={},
+ mime_type="text/plain",
),
StorageEntry(
path="file2.txt",
@@ -503,6 +579,7 @@ class TestFsspecFilesystem:
size=20,
last_modified=None,
metadata={},
+ mime_type="text/plain",
),
]
)
@@ -530,17 +607,19 @@ class TestFsspecFilesystem:
[
StorageEntry(
path="good.txt",
+ kind="file",
size=10,
last_modified=None,
- kind="file",
metadata={},
+ mime_type="text/plain",
),
StorageEntry(
path="also_good.txt",
+ kind="file",
size=20,
last_modified=None,
- kind="file",
metadata={},
+ mime_type="text/plain",
),
]
)
@@ -579,9 +658,9 @@ class TestFsspecFilesystem:
assert entry == snapshot(
StorageEntry(
path="data.csv",
+ kind="file",
size=1024,
last_modified=1700000000.0,
- kind="file",
metadata={
"e_tag": "abc123",
"is_link": False,
@@ -589,6 +668,7 @@ class TestFsspecFilesystem:
"n_link": 1,
"created": 1699000000.0,
},
+ mime_type="text/csv",
)
)
@@ -602,10 +682,11 @@ class TestFsspecFilesystem:
assert entry == snapshot(
StorageEntry(
path="",
+ kind="file",
size=0,
last_modified=None,
- kind="file",
metadata={},
+ mime_type=None,
)
)
@@ -619,10 +700,11 @@ class TestFsspecFilesystem:
assert entry == snapshot(
StorageEntry(
path="my_dir/",
+ kind="directory",
size=0,
last_modified=None,
- kind="directory",
metadata={},
+ mime_type=None,
)
)
@@ -640,10 +722,11 @@ class TestFsspecFilesystem:
assert result == snapshot(
StorageEntry(
path="test.txt",
+ kind="file",
size=42,
last_modified=1700000000.0,
- kind="file",
metadata={},
+ mime_type="text/plain",
)
)
@@ -691,6 +774,47 @@ class TestFsspecFilesystem:
ext="csv",
)
+ async def test_read_range_returns_bytes(self) -> None:
+ mock_store = MagicMock()
+ mock_store.cat_file.return_value = b"partial content"
+
+ backend = self._make_backend(mock_store)
+ result = await backend.read_range("path/file.txt", offset=0, length=15)
+ assert result == b"partial content"
+ mock_store.cat_file.assert_called_once_with(
+ "path/file.txt", start=0, end=15
+ )
+
+ async def test_read_range_encodes_string_to_bytes(self) -> None:
+ mock_store = MagicMock()
+ mock_store.cat_file.return_value = "text content"
+
+ backend = self._make_backend(mock_store)
+ result = await backend.read_range("path/file.txt", offset=0, length=50)
+ assert result == b"text content"
+
+ async def test_read_range_with_offset(self) -> None:
+ mock_store = MagicMock()
+ mock_store.cat_file.return_value = b"middle"
+
+ backend = self._make_backend(mock_store)
+ result = await backend.read_range("path/file.txt", offset=10, length=6)
+ assert result == b"middle"
+ mock_store.cat_file.assert_called_once_with(
+ "path/file.txt", start=10, end=16
+ )
+
+ async def test_read_range_full_file(self) -> None:
+ mock_store = MagicMock()
+ mock_store.cat_file.return_value = b"entire file"
+
+ backend = self._make_backend(mock_store)
+ result = await backend.read_range("path/file.txt")
+ assert result == b"entire file"
+ mock_store.cat_file.assert_called_once_with(
+ "path/file.txt", start=0, end=None
+ )
+
def test_protocol_tuple(self) -> None:
mock_store = MagicMock()
mock_store.protocol = ("gcs", "gs")
@@ -808,6 +932,7 @@ class TestFsspecFilesystemIntegration:
size=11,
last_modified=None,
metadata={"created": IsPositiveFloat()},
+ mime_type="text/plain",
),
StorageEntry(
path="/test/data.csv",
@@ -815,6 +940,7 @@ class TestFsspecFilesystemIntegration:
size=11,
last_modified=None,
metadata={"created": IsPositiveFloat()},
+ mime_type="text/csv",
),
]
)
@@ -837,6 +963,7 @@ class TestFsspecFilesystemIntegration:
size=12,
last_modified=None,
metadata={"created": IsDatetime()},
+ mime_type="text/plain",
)
)
@@ -852,6 +979,46 @@ class TestFsspecFilesystemIntegration:
result = await backend.sign_download_url("/test/file.txt")
assert result is None
+ async def test_read_range_full_file(self) -> None:
+ from fsspec.implementations.memory import MemoryFileSystem
+
+ fs = MemoryFileSystem()
+ fs.pipe("/test/data.txt", b"hello world")
+
+ backend = FsspecFilesystem(fs, VariableName("mem_fs"))
+ result = await backend.read_range("/test/data.txt")
+ assert result == b"hello world"
+
+ async def test_read_range_partial(self) -> None:
+ from fsspec.implementations.memory import MemoryFileSystem
+
+ fs = MemoryFileSystem()
+ fs.pipe("/test/data.txt", b"hello world")
+
+ backend = FsspecFilesystem(fs, VariableName("mem_fs"))
+ result = await backend.read_range("/test/data.txt", offset=0, length=5)
+ assert result == b"hello"
+
+ async def test_read_range_with_offset(self) -> None:
+ from fsspec.implementations.memory import MemoryFileSystem
+
+ fs = MemoryFileSystem()
+ fs.pipe("/test/data.txt", b"hello world")
+
+ backend = FsspecFilesystem(fs, VariableName("mem_fs"))
+ result = await backend.read_range("/test/data.txt", offset=6, length=5)
+ assert result == b"world"
+
+ async def test_read_range_offset_without_length(self) -> None:
+ from fsspec.implementations.memory import MemoryFileSystem
+
+ fs = MemoryFileSystem()
+ fs.pipe("/test/data.txt", b"hello world")
+
+ backend = FsspecFilesystem(fs, VariableName("mem_fs"))
+ result = await backend.read_range("/test/data.txt", offset=6)
+ assert result == b"world"
+
def test_protocol_memory_filesystem(self) -> None:
from fsspec.implementations.memory import MemoryFileSystem
@@ -883,6 +1050,7 @@ class TestObstoreIntegration:
size=5,
last_modified=IsPositiveFloat(), # pyright: ignore[reportArgumentType]
metadata={"e_tag": "0"},
+ mime_type="text/plain",
),
StorageEntry(
path="test/file2.txt",
@@ -890,6 +1058,7 @@ class TestObstoreIntegration:
size=6,
last_modified=IsPositiveFloat(), # pyright: ignore[reportArgumentType]
metadata={"e_tag": "1"},
+ mime_type="text/plain",
),
]
)
@@ -919,6 +1088,7 @@ class TestObstoreIntegration:
size=12,
last_modified=IsPositiveFloat(), # pyright: ignore[reportArgumentType]
metadata={"e_tag": "0"},
+ mime_type="text/plain",
)
)
@@ -934,6 +1104,46 @@ class TestObstoreIntegration:
result = await backend.sign_download_url("data.txt")
assert result is None
+ async def test_read_range_full_file(self) -> None:
+ from obstore.store import MemoryStore
+
+ store = MemoryStore()
+ await store.put_async("file.txt", b"hello world")
+
+ backend = Obstore(store, VariableName("mem_store"))
+ result = await backend.read_range("file.txt")
+ assert result == b"hello world"
+
+ async def test_read_range_partial(self) -> None:
+ from obstore.store import MemoryStore
+
+ store = MemoryStore()
+ await store.put_async("file.txt", b"hello world")
+
+ backend = Obstore(store, VariableName("mem_store"))
+ result = await backend.read_range("file.txt", offset=0, length=5)
+ assert result == b"hello"
+
+ async def test_read_range_with_offset(self) -> None:
+ from obstore.store import MemoryStore
+
+ store = MemoryStore()
+ await store.put_async("file.txt", b"hello world")
+
+ backend = Obstore(store, VariableName("mem_store"))
+ result = await backend.read_range("file.txt", offset=6, length=5)
+ assert result == b"world"
+
+ async def test_read_range_offset_without_length(self) -> None:
+ from obstore.store import MemoryStore
+
+ store = MemoryStore()
+ await store.put_async("file.txt", b"hello world")
+
+ backend = Obstore(store, VariableName("mem_store"))
+ result = await backend.read_range("file.txt", offset=6)
+ assert result == b"world"
+
class TestNormalizeProtocol:
@pytest.mark.parametrize(
diff --git a/tests/_runtime/test_runtime_external_storage.py b/tests/_runtime/test_runtime_external_storage.py
index ad8e6349bd..7268072cfe 100644
--- a/tests/_runtime/test_runtime_external_storage.py
+++ b/tests/_runtime/test_runtime_external_storage.py
@@ -215,6 +215,7 @@ class TestExternalStorageCallbacks:
size=5,
last_modified=IsPositiveFloat(), # pyright: ignore[reportArgumentType]
metadata={"e_tag": IsStr()},
+ mime_type="text/csv",
),
StorageEntry(
path="data/file2.txt",
@@ -222,6 +223,7 @@ class TestExternalStorageCallbacks:
size=5,
last_modified=IsPositiveFloat(), # pyright: ignore[reportArgumentType]
metadata={"e_tag": IsStr()},
+ mime_type="text/plain",
),
],
namespace=STORAGE_VAR,
@@ -275,6 +277,7 @@ class TestExternalStorageCallbacks:
size=1,
last_modified=IsPositiveFloat(), # pyright: ignore[reportArgumentType]
metadata={"e_tag": IsStr()},
+ mime_type="text/plain",
),
StorageEntry(
path="b.txt",
@@ -282,6 +285,7 @@ class TestExternalStorageCallbacks:
size=1,
last_modified=IsPositiveFloat(), # pyright: ignore[reportArgumentType]
metadata={"e_tag": IsStr()},
+ mime_type="text/plain",
),
],
namespace=STORAGE_VAR,
@@ -682,3 +686,230 @@ class TestExternalStorageCallbacks:
error="Failed to download: access denied",
)
]
+
+ async def test_download_preview(self, mocked_kernel: MockedKernel) -> None:
+ k = mocked_kernel.k
+ stream = mocked_kernel.stream
+
+ await k.run(
+ [
+ ExecuteCellCommand(
+ cell_id=CellId_t("0"),
+ code=(
+ "from obstore.store import MemoryStore\n"
+ f"{STORAGE_VAR} = MemoryStore()"
+ ),
+ ),
+ ]
+ )
+
+ store = k.globals[VariableName(STORAGE_VAR)]
+ await store.put_async("docs/readme.txt", b"hello world")
+
+ request = StorageDownloadCommand(
+ request_id=RequestId("req-30"),
+ namespace=STORAGE_VAR,
+ path="docs/readme.txt",
+ preview=True,
+ )
+ await k.handle_message(request)
+
+ results = [
+ op
+ for op in stream.operations
+ if isinstance(op, StorageDownloadReadyNotification)
+ ]
+ assert results == [
+ StorageDownloadReadyNotification(
+ request_id=RequestId("req-30"),
+ url=IsStr(), # pyright: ignore[reportArgumentType]
+ filename="readme.txt",
+ error=None,
+ )
+ ]
+ notif = results[0]
+ assert notif.url is not None
+ # Preview returns a virtual file URL, not a signed URL
+ assert "@file/" in notif.url
+
+ async def test_download_preview_skips_signed_url(
+ self, mocked_kernel: MockedKernel
+ ) -> None:
+ """Preview mode should use read_range, never sign_download_url."""
+ k = mocked_kernel.k
+ stream = mocked_kernel.stream
+
+ await k.run(
+ [
+ ExecuteCellCommand(
+ cell_id=CellId_t("0"),
+ code=(
+ "from obstore.store import MemoryStore\n"
+ f"{STORAGE_VAR} = MemoryStore()"
+ ),
+ ),
+ ]
+ )
+
+ store = k.globals[VariableName(STORAGE_VAR)]
+ await store.put_async("file.py", b"print('hello')")
+
+ backend = Obstore(store, VariableName(STORAGE_VAR))
+ backend.sign_download_url = AsyncMock( # type: ignore[method-assign]
+ side_effect=AssertionError("should not be called"),
+ )
+
+ with pytest.MonkeyPatch.context() as mp:
+ mp.setattr(
+ k.external_storage_callbacks,
+ "_get_storage_backend",
+ lambda _: (backend, None),
+ )
+ request = StorageDownloadCommand(
+ request_id=RequestId("req-31"),
+ namespace=STORAGE_VAR,
+ path="file.py",
+ preview=True,
+ )
+ await k.handle_message(request)
+
+ results = [
+ op
+ for op in stream.operations
+ if isinstance(op, StorageDownloadReadyNotification)
+ ]
+ assert results == [
+ StorageDownloadReadyNotification(
+ request_id=RequestId("req-31"),
+ url=IsStr(), # pyright: ignore[reportArgumentType]
+ filename="file.py",
+ error=None,
+ )
+ ]
+
+ async def test_download_preview_schedules_cleanup(
+ self, mocked_kernel: MockedKernel
+ ) -> None:
+ import asyncio
+
+ k = mocked_kernel.k
+
+ await k.run(
+ [
+ ExecuteCellCommand(
+ cell_id=CellId_t("0"),
+ code=(
+ "from obstore.store import MemoryStore\n"
+ f"{STORAGE_VAR} = MemoryStore()"
+ ),
+ ),
+ ]
+ )
+
+ store = k.globals[VariableName(STORAGE_VAR)]
+ await store.put_async("tmp/preview.txt", b"data")
+
+ request = StorageDownloadCommand(
+ request_id=RequestId("req-32"),
+ namespace=STORAGE_VAR,
+ path="tmp/preview.txt",
+ preview=True,
+ )
+
+ loop = asyncio.get_running_loop()
+ scheduled: list[tuple[float, object]] = []
+ original_call_later = loop.call_later
+
+ def spy_call_later(
+ delay: float, callback: object, *args: object
+ ) -> asyncio.TimerHandle:
+ scheduled.append((delay, callback))
+ return original_call_later(delay, callback, *args) # pyright: ignore[reportArgumentType]
+
+ with pytest.MonkeyPatch.context() as mp:
+ mp.setattr(loop, "call_later", spy_call_later)
+ await k.handle_message(request)
+
+ assert len(scheduled) == 1
+ delay, _callback = scheduled[0]
+ assert delay == 60
+
+ async def test_download_preview_backend_exception(
+ self, mocked_kernel: MockedKernel
+ ) -> None:
+ k = mocked_kernel.k
+ stream = mocked_kernel.stream
+
+ await k.run(
+ [
+ ExecuteCellCommand(
+ cell_id=CellId_t("0"),
+ code=(
+ "from obstore.store import MemoryStore\n"
+ f"{STORAGE_VAR} = MemoryStore()"
+ ),
+ ),
+ ]
+ )
+
+ store = k.globals[VariableName(STORAGE_VAR)]
+ broken_backend = Obstore(store, VariableName(STORAGE_VAR))
+ broken_backend.read_range = AsyncMock( # type: ignore[method-assign]
+ side_effect=OSError("read failed"),
+ )
+
+ with pytest.MonkeyPatch.context() as mp:
+ mp.setattr(
+ k.external_storage_callbacks,
+ "_get_storage_backend",
+ lambda _: (broken_backend, None),
+ )
+ request = StorageDownloadCommand(
+ request_id=RequestId("req-33"),
+ namespace=STORAGE_VAR,
+ path="broken/file.txt",
+ preview=True,
+ )
+ await k.handle_message(request)
+
+ results = [
+ op
+ for op in stream.operations
+ if isinstance(op, StorageDownloadReadyNotification)
+ ]
+ assert results == [
+ StorageDownloadReadyNotification(
+ request_id=RequestId("req-33"),
+ url=None,
+ filename=None,
+ error="Failed to download: read failed",
+ )
+ ]
+
+ async def test_download_preview_variable_not_found(
+ self, mocked_kernel: MockedKernel
+ ) -> None:
+ k = mocked_kernel.k
+ stream = mocked_kernel.stream
+
+ request = StorageDownloadCommand(
+ request_id=RequestId("req-34"),
+ namespace="nonexistent_var",
+ path="file.txt",
+ preview=True,
+ )
+ await k.handle_message(request)
+
+ results = [
+ op
+ for op in stream.operations
+ if isinstance(op, StorageDownloadReadyNotification)
+ ]
+ assert results == [
+ StorageDownloadReadyNotification(
+ request_id=RequestId("req-34"),
+ url=None,
+ filename=None,
+ error="Variable 'nonexistent_var' not found",
+ )
+ ]