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: Add file upload error handling and maximum file size (100mb)

Myles Scolnick813730e253c6
diff --git a/frontend/src/plugins/impl/FileUploadPlugin.tsx b/frontend/src/plugins/impl/FileUploadPlugin.tsx
index dee8a1d714..63412b2d5f 100644
--- a/frontend/src/plugins/impl/FileUploadPlugin.tsx
+++ b/frontend/src/plugins/impl/FileUploadPlugin.tsx
@@ -8,6 +8,7 @@ import { IPlugin, IPluginProps, Setter } from "../types";
import { filesToBase64 } from "../../utils/fileToBase64";
import { buttonVariants } from "../../components/ui/button";
import { renderHTML } from "../core/RenderHTML";
+import { toast } from "@/components/ui/use-toast";
type FileUploadType = "button" | "area";
@@ -107,6 +108,18 @@ 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 => {
@@ -116,11 +129,45 @@ export const FileUpload = (props: FileUploadProps): JSX.Element => {
useDropzone({
accept: acceptGroups,
multiple: multiple,
- onDrop: (acceptedFiles) => {
- filesToBase64(acceptedFiles).then((value) => {
- setValue(value);
+ maxSize: MAX_SIZE,
+ onError: (error) => {
+ console.error(error);
+ toast({
+ title: "File upload failed",
+ description: error.message,
+ variant: "danger",
+ });
+ },
+ onDropRejected: (rejectedFiles) => {
+ toast({
+ title: "File upload failed",
+ description: (
+ <div className="flex flex-col gap-1">
+ {rejectedFiles.map((file) => (
+ <div key={file.file.name}>
+ {file.file.name} (
+ {file.errors.map((e) => e.message).join(", ")})
+ </div>
+ ))}
+ </div>
+ ),
+ variant: "danger",
});
},
+ onDrop: (acceptedFiles) => {
+ filesToBase64(acceptedFiles)
+ .then((value) => {
+ setValue(value);
+ })
+ .catch((error) => {
+ console.error(error);
+ toast({
+ title: "File upload failed",
+ description: "Failed to convert file to base64.",
+ variant: "danger",
+ });
+ });
+ },
});
if (kind === "button") {
@@ -131,7 +178,7 @@ export const FileUpload = (props: FileUploadProps): JSX.Element => {
// - hide the input element with a hidden attribute
// - create a button and some text that reflects what has been uploaded;
// link button to the hidden input element
- const label = props.label ?? "<p>Upload</p>";
+ const label = props.label ?? "Upload";
return (
<>
<button
@@ -142,7 +189,7 @@ export const FileUpload = (props: FileUploadProps): JSX.Element => {
})}
>
{renderHTML({ html: label })}
- <Upload size={16} className="ml-2" />
+ <Upload size={14} className="ml-2" />
</button>
<input {...getInputProps({})} type="file" />
</>
diff --git a/marimo/_plugins/ui/_impl/input.py b/marimo/_plugins/ui/_impl/input.py
index 51a898a819..f5fe2317d3 100644
--- a/marimo/_plugins/ui/_impl/input.py
+++ b/marimo/_plugins/ui/_impl/input.py
@@ -772,6 +772,8 @@ class file(UIElement[List[Tuple[str, str]], Sequence[FileUploadResults]]):
Use the `kind` argument to switch between a button and a drop-and-drop
area.
+ The maximum file size is 100MB.
+
**Examples.**
Uploading a single file: