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: support uploading folders in the sidebar (keep heirarchy) (#5349)

Myles Scolnickd7df0ece0edb
diff --git a/frontend/src/components/editor/file-tree/file-viewer.tsx b/frontend/src/components/editor/file-tree/file-viewer.tsx
index e92f060cd3..7a680c54d3 100644
--- a/frontend/src/components/editor/file-tree/file-viewer.tsx
+++ b/frontend/src/components/editor/file-tree/file-viewer.tsx
@@ -24,7 +24,13 @@ 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 { AudioViewer, CsvViewer, ImageViewer, VideoViewer } from "./renderers";
+import {
+ AudioViewer,
+ CsvViewer,
+ ImageViewer,
+ PdfViewer,
+ VideoViewer,
+} from "./renderers";
interface Props {
file: FileInfo;
@@ -203,6 +209,17 @@ export const FileViewer: React.FC<Props> = ({ file, onOpenNotebook }) => {
);
}
+ 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>
+ </>
+ );
+ }
+
return (
<>
{header}
@@ -245,7 +262,8 @@ const isMedia = (mime: string) => {
return (
mime.startsWith("image/") ||
mime.startsWith("audio/") ||
- mime.startsWith("video/")
+ mime.startsWith("video/") ||
+ mime.startsWith("application/pdf")
);
};
diff --git a/frontend/src/components/editor/file-tree/renderers.tsx b/frontend/src/components/editor/file-tree/renderers.tsx
index 06d267d874..dffa9c7499 100644
--- a/frontend/src/components/editor/file-tree/renderers.tsx
+++ b/frontend/src/components/editor/file-tree/renderers.tsx
@@ -70,3 +70,16 @@ export const VideoViewer: React.FC<{ base64: Base64String; mime: string }> = ({
// eslint-disable-next-line jsx-a11y/media-has-caption
return <video controls={true} src={base64ToDataURL(base64, mime)} />;
};
+
+export const PdfViewer: React.FC<{ base64: Base64String; mime: string }> = ({
+ mime,
+ base64,
+}) => {
+ return (
+ <iframe
+ src={base64ToDataURL(base64, mime)}
+ title="PDF Viewer"
+ className="w-full h-full"
+ />
+ );
+};
diff --git a/frontend/src/components/editor/file-tree/upload.tsx b/frontend/src/components/editor/file-tree/upload.tsx
index 8f931b4b0f..8dc6f39765 100644
--- a/frontend/src/components/editor/file-tree/upload.tsx
+++ b/frontend/src/components/editor/file-tree/upload.tsx
@@ -5,7 +5,7 @@ import { toast } from "@/components/ui/use-toast";
import { sendCreateFileOrFolder } from "@/core/network/requests";
import { serializeBlob } from "@/utils/blob";
import { Logger } from "@/utils/Logger";
-import type { FilePath } from "@/utils/paths";
+import { type FilePath, PathBuilder } from "@/utils/paths";
import { refreshRoot } from "./state";
const MAX_SIZE = 1024 * 1024 * 100; // 100MB
@@ -40,6 +40,15 @@ export function useFileExplorerUpload(options: DropzoneOptions = {}) {
},
onDrop: async (acceptedFiles) => {
for (const file of acceptedFiles) {
+ // We strip the leading slash since File.path can return
+ // `/path/to/file`.
+ const filePath = stripLeadingSlash(getPath(file));
+ let directoryPath = "" as FilePath;
+ if (filePath) {
+ directoryPath =
+ PathBuilder.guessDeliminator(filePath).dirname(filePath);
+ }
+
// File contents are sent base64-encoded to support arbitrary
// bytes data
//
@@ -47,7 +56,7 @@ export function useFileExplorerUpload(options: DropzoneOptions = {}) {
// data:*/*;base64,
const base64 = (await serializeBlob(file)).split(",")[1];
await sendCreateFileOrFolder({
- path: "" as FilePath, // add to root
+ path: directoryPath,
type: "file",
name: file.name,
contents: base64,
@@ -58,3 +67,34 @@ export function useFileExplorerUpload(options: DropzoneOptions = {}) {
...options,
});
}
+
+/**
+ * Get the path of a file.
+ *
+ * Types only have `webkitRelativePath`, but File objects in the browser
+ * can have `path` and `relativePath`.
+ */
+function getPath(file: File): FilePath | undefined {
+ if (file.webkitRelativePath) {
+ return file.webkitRelativePath as FilePath;
+ }
+ if ("path" in file && typeof file.path === "string") {
+ return file.path as FilePath;
+ }
+ if ("relativePath" in file && typeof file.relativePath === "string") {
+ return file.relativePath as FilePath;
+ }
+ return undefined;
+}
+
+/**
+ * Strip leading slashes from a path.
+ *
+ * TODO: this may not support windows paths.
+ */
+function stripLeadingSlash(path: FilePath | undefined): FilePath | undefined {
+ if (!path) {
+ return undefined;
+ }
+ return path.replace(/^\/+/, "") as FilePath;
+}