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.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
improvement: mo.ui.file upload confirmation, removal (#1077)
Akshay Agrawal39aa9c23644a
diff --git a/frontend/src/plugins/impl/FileUploadPlugin.tsx b/frontend/src/plugins/impl/FileUploadPlugin.tsxindex 9570e53640..be86380fe5 100644--- a/frontend/src/plugins/impl/FileUploadPlugin.tsx+++ b/frontend/src/plugins/impl/FileUploadPlugin.tsx@@ -9,6 +9,7 @@ import { filesToBase64 } from "../../utils/fileToBase64";import { buttonVariants } from "../../components/ui/button";import { renderHTML } from "../core/RenderHTML";import { toast } from "@/components/ui/use-toast";+import { Tooltip, TooltipProvider } from "@/components/ui/tooltip";type FileUploadType = "button" | "area";@@ -170,6 +171,15 @@ export const FileUpload = (props: FileUploadProps): JSX.Element => {},});+ const uploadedFiles = (+ <ul>+ {value.map(([fileName, _]) => (+ <li key={fileName}>{fileName}</li>+ ))}+ </ul>+ );+ const uploaded = value.length > 0;+if (kind === "button") {// TODO(akshayka): React to a change in `value` due to an update from another// instance of this element. Browsers do not allow scripts to set the `value`@@ -180,80 +190,115 @@ export const FileUpload = (props: FileUploadProps): JSX.Element => {// link button to the hidden input elementconst label = props.label ?? "Upload";return (- <>- <button- data-testid="marimo-plugin-file-upload-button"- {...getRootProps({})}- className={buttonVariants({- variant: "secondary",- size: "xs",- })}- >- {renderHTML({ html: label })}- <Upload size={14} className="ml-2" />- </button>- <input {...getInputProps({})} type="file" />- </>+ <TooltipProvider>+ <div className="flex flex-row items-center justify-start flex-grow gap-2">+ <button+ data-testid="marimo-plugin-file-upload-button"+ {...getRootProps({})}+ className={buttonVariants({+ variant: "secondary",+ size: "xs",+ })}+ >+ {renderHTML({ html: label })}+ <Upload size={14} className="ml-2" />+ </button>+ <input {...getInputProps({})} type="file" />+ {uploaded ? (+ <>+ <Tooltip content={uploadedFiles}>+ <span className="text-xs text-muted-foreground">+ Uploaded{" "}+ <span className="underline cursor-pointer">+ {value.length} {value.length > 1 ? "files" : "file"}.+ </span>+ </span>+ </Tooltip>++ <span className="text-xs text-destructive hover:underline hover:cursor-pointer">+ <button+ className={cn("text-destructive", "hover:underline")}+ onClick={() => setValue([])}+ >+ Click to clear files.+ </button>+ </span>+ </>+ ) : null}+ </div>+ </TooltipProvider>);}- const uploadedFiles = value.map(([fileName, _]) => (- <li key={fileName}>{fileName}</li>- ));-- const uploaded = uploadedFiles.length > 0;const label =props.label ?? "Drag and drop files here, or click to open file browser";return (<section>- <div- className={cn(- "mt-3 mb-2 w-full flex flex-col items-center justify-center ",- "px-6 py-6 sm:px-8 sm:py-8 md:py-10 md:px-16",- "border rounded-sm",- "text-sm text-muted-foreground",- "hover:cursor-pointer",- "active:shadow-xsSolid",- "focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring focus-visible:border-accent",- !isFocused && "border-input/60 border-dashed",- isFocused && "border-solid",- )}- {...getRootProps()}- >- <input {...getInputProps()} />- <div className="flex flex-col items-center justify-center flex-grow gap-3">- {uploaded ? (- <span>To re-upload: {renderHTML({ html: label })}</span>- ) : (- <span className="mt-0">{renderHTML({ html: label })}</span>+ <div className="flex flex-col items-start justify-start flex-grow gap-3">+ <div+ className={cn(+ "hover:text-primary",+ "mt-3 mb-2 w-full flex flex-col items-center justify-center ",+ "px-6 py-6 sm:px-8 sm:py-8 md:py-10 md:px-16",+ "border rounded-sm",+ "text-sm text-muted-foreground",+ "hover:cursor-pointer",+ "active:shadow-xsSolid",+ "focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring focus-visible:border-accent",+ !isFocused && "border-input/60 border-dashed",+ isFocused && "border-solid",)}- <div className="flex flex-row items-center justify-center flex-grow gap-3 hover:text-primary">- <Upload- strokeWidth={1.4}- className={cn(- isDragAccept && "text-primary",- isDragReject && "text-destructive",- )}- />- <MousePointerSquareDashedIcon- strokeWidth={1.4}- className={cn(- isDragAccept && "text-primary",- isDragReject && "text-destructive",- )}- />+ {...getRootProps()}+ >+ <input {...getInputProps()} />+ <div className="flex flex-col items-center justify-center flex-grow gap-3">+ {uploaded ? (+ <span>To re-upload: {renderHTML({ html: label })}</span>+ ) : (+ <span className="mt-0">{renderHTML({ html: label })}</span>+ )}+ <div className="flex flex-row items-center justify-center flex-grow gap-3">+ <Upload+ strokeWidth={1.4}+ className={cn(+ isDragAccept && "text-primary",+ isDragReject && "text-destructive",+ )}+ />+ <MousePointerSquareDashedIcon+ strokeWidth={1.4}+ className={cn(+ isDragAccept && "text-primary",+ isDragReject && "text-destructive",+ )}+ />+ </div></div></div>- </div>- <aside>{uploaded ? (- <span className="markdown">- <strong>Uploaded files</strong>- <ul style={{ margin: 0 }}>{uploadedFiles}</ul>- </span>+ <div className="flex flex-row gap-1">+ <div className="text-xs text-muted-foreground">+ <TooltipProvider>+ Uploaded{" "}+ <Tooltip content={uploadedFiles}>+ <span className="underline cursor-pointer">+ {value.length} {value.length > 1 ? "files" : "file"}.+ </span>+ </Tooltip>+ </TooltipProvider>+ </div>+ <span className="text-xs text-destructive hover:underline hover:cursor-pointer">+ <button+ className={cn("text-destructive", "hover:underline")}+ onClick={() => setValue([])}+ >+ Click to clear files.+ </button>+ </span>+ </div>) : null}- </aside>+ </div></section>);};diff --git a/marimo/_plugins/ui/_impl/input.py b/marimo/_plugins/ui/_impl/input.pyindex b3087656c0..08e8be706f 100644--- a/marimo/_plugins/ui/_impl/input.py+++ b/marimo/_plugins/ui/_impl/input.py@@ -775,6 +775,10 @@ class FileUploadResults:name: strcontents: bytes+ def __repr__(self) -> str:+ # Truncate contents because it can be very long+ return f"FileUploadResults(name='{self.name}', contents=...)"+@mddocclass file(UIElement[List[Tuple[str, str]], Sequence[FileUploadResults]]):diff --git a/marimo/_smoke_tests/bugs/1064.py b/marimo/_smoke_tests/bugs/1064.pyindex 4323aaf884..69e894bb17 100644--- a/marimo/_smoke_tests/bugs/1064.py+++ b/marimo/_smoke_tests/bugs/1064.py@@ -1,3 +1,4 @@+# Copyright 2024 Marimo. All rights reserved.import marimo__generated_with = "0.3.8"