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.

improvement: configurable max-size for mo.ui.file() upload (#4733)

Myles Scolnicke8739b63d361
diff --git a/frontend/src/plugins/impl/FileUploadPlugin.tsx b/frontend/src/plugins/impl/FileUploadPlugin.tsx
index e0a7d1a751..3f0a5a5df3 100644
--- a/frontend/src/plugins/impl/FileUploadPlugin.tsx
+++ b/frontend/src/plugins/impl/FileUploadPlugin.tsx
@@ -20,12 +20,14 @@ type FileUploadType = "button" | "area";
* @param filetypes - file types to accept (same as HTML input's accept attr)
* @param multiple - whether to allow the user to upload multiple files
* @param label - a label for the file upload area
+ * @param max_size - the maximum size of the file to upload (in bytes)
*/
interface Data {
filetypes: string[];
multiple: boolean;
kind: FileUploadType;
label: string | null;
+ max_size: number;
}
type T = Array<[string, string]>;
@@ -38,6 +40,7 @@ export class FileUploadPlugin implements IPlugin<T, Data> {
multiple: z.boolean(),
kind: z.enum(["button", "area"]),
label: z.string().nullable(),
+ max_size: z.number(),
});
render(props: IPluginProps<T, Data>): JSX.Element {
@@ -49,6 +52,7 @@ export class FileUploadPlugin implements IPlugin<T, Data> {
kind={props.data.kind}
value={props.value}
setValue={props.setValue}
+ max_size={props.data.max_size}
/>
);
}
@@ -110,28 +114,16 @@ function groupFileTypesByMIMEType(extensions: string[]) {
return filesByMIMEType;
}
-// We may want to increase this based on user feedback
-//
-// But rather than forever increasing this, we should consider
-// adding a non-browser file-chooser which allows users to select files from
-// their local filesystem, and we only return the path.
-//
-// By using the browser's file chooser, it is more secure as we don
-// not get access to the uploaded file's path but
-// we are forced to upload the file to browser memory before
-// sending it to the server.
-const MAX_SIZE = 100_000_000; // 100 MB
-
/* TODO(akshayka): Allow uploading files one-by-one and removing uploaded files
* when multiple is `True`*/
export const FileUpload = (props: FileUploadProps): JSX.Element => {
const acceptGroups = groupFileTypesByMIMEType(props.filetypes);
- const { setValue, kind, multiple, value } = props;
+ const { setValue, kind, multiple, value, max_size } = props;
const { getRootProps, getInputProps, isFocused, isDragAccept, isDragReject } =
useDropzone({
accept: acceptGroups,
multiple: multiple,
- maxSize: MAX_SIZE,
+ maxSize: max_size,
onError: (error) => {
Logger.error(error);
toast({
diff --git a/frontend/src/stories/file-upload.stories.tsx b/frontend/src/stories/file-upload.stories.tsx
index efab5f5a5b..7547f2054b 100644
--- a/frontend/src/stories/file-upload.stories.tsx
+++ b/frontend/src/stories/file-upload.stories.tsx
@@ -15,6 +15,7 @@ export const AcceptAny = {
label={null}
value={[]}
setValue={() => null}
+ max_size={100_000_000}
/>
),
@@ -30,6 +31,7 @@ export const AcceptLongText = {
label={"Drop here, ".repeat(100)}
value={[]}
setValue={() => null}
+ max_size={100_000_000}
/>
),
@@ -45,6 +47,7 @@ export const AcceptTxtOnly = {
label={null}
value={[]}
setValue={() => null}
+ max_size={100_000_000}
/>
),
@@ -60,6 +63,7 @@ export const AcceptTxtOnlyButton = {
label={null}
value={[]}
setValue={() => null}
+ max_size={100_000_000}
/>
),
@@ -75,6 +79,7 @@ export const SingleFileArea = {
label={null}
value={[]}
setValue={() => null}
+ max_size={100_000_000}
/>
),
@@ -90,6 +95,7 @@ export const SingleFileButton = {
label={null}
value={[]}
setValue={() => null}
+ max_size={100_000_000}
/>
),
diff --git a/marimo/_plugins/ui/_impl/input.py b/marimo/_plugins/ui/_impl/input.py
index 4ce18bbcf2..e232ecfe70 100644
--- a/marimo/_plugins/ui/_impl/input.py
+++ b/marimo/_plugins/ui/_impl/input.py
@@ -1324,6 +1324,8 @@ class file(UIElement[list[tuple[str, str]], Sequence[FileUploadResults]]):
on_change (Callable[[Sequence[FileUploadResults]], None], optional):
Optional callback to run when this element's value changes.
Defaults to None.
+ max_size (int, optional): The maximum size of the file to upload
+ (in bytes). Defaults to 100MB.
"""
_name: Final[str] = "marimo-file"
@@ -1334,6 +1336,7 @@ class file(UIElement[list[tuple[str, str]], Sequence[FileUploadResults]]):
multiple: bool = False,
kind: Literal["button", "area"] = "button",
*,
+ max_size: int = 100_000_000, # 100MB default
label: str = "",
on_change: Optional[
Callable[[Sequence[FileUploadResults]], None]
@@ -1351,6 +1354,9 @@ class file(UIElement[list[tuple[str, str]], Sequence[FileUploadResults]]):
f"Invalid types: {', '.join(invalid_types)}"
)
+ if max_size <= 0:
+ raise ValueError("max_size must be greater than 0")
+
super().__init__(
component_name=file._name,
initial_value=[],
@@ -1359,6 +1365,7 @@ class file(UIElement[list[tuple[str, str]], Sequence[FileUploadResults]]):
"filetypes": filetypes if filetypes is not None else [],
"multiple": multiple,
"kind": kind,
+ "max_size": max_size,
},
on_change=on_change,
)
diff --git a/tests/_plugins/ui/_impl/test_input.py b/tests/_plugins/ui/_impl/test_input.py
index 493e830f87..58abd7e8f1 100644
--- a/tests/_plugins/ui/_impl/test_input.py
+++ b/tests/_plugins/ui/_impl/test_input.py
@@ -608,6 +608,23 @@ def test_file_validation() -> None:
assert "or contain a forward slash" in str(e.value)
assert "doc, pdf" in str(e.value)
+ # Test max_size validation
+ with pytest.raises(ValueError) as e:
+ ui.file(max_size=0)
+ assert "max_size must be greater than 0" in str(e.value)
+
+ with pytest.raises(ValueError) as e:
+ ui.file(max_size=-1)
+ assert "max_size must be greater than 0" in str(e.value)
+
+ # Test default max_size
+ f = ui.file()
+ assert f._component_args["max_size"] == 100_000_000 # 100MB
+
+ # Test custom max_size
+ f = ui.file(max_size=50_000_000) # 50MB
+ assert f._component_args["max_size"] == 50_000_000
+
@pytest.mark.skipif(not HAS_NUMPY, reason="numpy not installed")
def test_numpy_steps() -> None: