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: generate Google Drive connection code for embedded environments (#9884)

Shahmir Varqha3e52b5d46f03
diff --git a/frontend/src/components/editor/connections/storage/__tests__/__snapshots__/as-code.test.ts.snap b/frontend/src/components/editor/connections/storage/__tests__/__snapshots__/as-code.test.ts.snap
index 127ad01812..59121833fd 100644
--- a/frontend/src/components/editor/connections/storage/__tests__/__snapshots__/as-code.test.ts.snap
+++ b/frontend/src/components/editor/connections/storage/__tests__/__snapshots__/as-code.test.ts.snap
@@ -82,10 +82,17 @@ exports[`generateStorageCode > GCS > without service account key (default creden
store = GCSStore("my-bucket")"
`;
-exports[`generateStorageCode > Google Drive > with browser auth (no credentials) 1`] = `
+exports[`generateStorageCode > Google Drive > with default auth (no credentials) 1`] = `
"from gdrive_fsspec import GoogleDriveFileSystem
-fs = GoogleDriveFileSystem(token="browser", use_listings_cache=False)"
+fs = GoogleDriveFileSystem(use_listings_cache=False)"
+`;
+
+exports[`generateStorageCode > Google Drive > with embedded auth (no credentials) 1`] = `
+"from gdrive_fsspec import GoogleDriveFileSystem
+
+fs = GoogleDriveFileSystem(use_listings_cache=False, auth_kwargs={"use_local_webserver": False})
+print("Google Drive connected! Important: Run this cell again to clear the console")"
`;
exports[`generateStorageCode > Google Drive > with service account credentials 1`] = `
diff --git a/frontend/src/components/editor/connections/storage/__tests__/as-code.test.ts b/frontend/src/components/editor/connections/storage/__tests__/as-code.test.ts
index f61a6f266a..44194ad4a9 100644
--- a/frontend/src/components/editor/connections/storage/__tests__/as-code.test.ts
+++ b/frontend/src/components/editor/connections/storage/__tests__/as-code.test.ts
@@ -43,7 +43,9 @@ describe("generateStorageCode", () => {
describe("S3", () => {
it("basic connection with all fields", () => {
- expect(generateStorageCode(baseS3, "obstore")).toMatchSnapshot();
+ expect(
+ generateStorageCode(baseS3, { library: "obstore" }),
+ ).toMatchSnapshot();
});
it("minimal connection (bucket only)", () => {
@@ -51,7 +53,9 @@ describe("generateStorageCode", () => {
type: "s3",
bucket: "my-bucket",
};
- expect(generateStorageCode(conn, "obstore")).toMatchSnapshot();
+ expect(
+ generateStorageCode(conn, { library: "obstore" }),
+ ).toMatchSnapshot();
});
it("with custom endpoint", () => {
@@ -59,7 +63,9 @@ describe("generateStorageCode", () => {
...baseS3,
endpoint_url: "https://minio.example.com:9000",
};
- expect(generateStorageCode(conn, "obstore")).toMatchSnapshot();
+ expect(
+ generateStorageCode(conn, { library: "obstore" }),
+ ).toMatchSnapshot();
});
it("with secrets", () => {
@@ -70,13 +76,17 @@ describe("generateStorageCode", () => {
access_key_id: prefixSecret("AWS_ACCESS_KEY_ID"),
secret_access_key: prefixSecret("AWS_SECRET_ACCESS_KEY"),
};
- expect(generateStorageCode(conn, "obstore")).toMatchSnapshot();
+ expect(
+ generateStorageCode(conn, { library: "obstore" }),
+ ).toMatchSnapshot();
});
});
describe("GCS", () => {
it("with service account key", () => {
- expect(generateStorageCode(baseGCS, "obstore")).toMatchSnapshot();
+ expect(
+ generateStorageCode(baseGCS, { library: "obstore" }),
+ ).toMatchSnapshot();
});
it("without service account key (default credentials)", () => {
@@ -84,13 +94,17 @@ describe("generateStorageCode", () => {
type: "gcs",
bucket: "my-bucket",
};
- expect(generateStorageCode(conn, "obstore")).toMatchSnapshot();
+ expect(
+ generateStorageCode(conn, { library: "obstore" }),
+ ).toMatchSnapshot();
});
});
describe("Azure", () => {
it("basic connection with account key", () => {
- expect(generateStorageCode(baseAzure, "obstore")).toMatchSnapshot();
+ expect(
+ generateStorageCode(baseAzure, { library: "obstore" }),
+ ).toMatchSnapshot();
});
it("without account key", () => {
@@ -99,7 +113,9 @@ describe("generateStorageCode", () => {
container: "my-container",
account_name: "storageaccount",
};
- expect(generateStorageCode(conn, "obstore")).toMatchSnapshot();
+ expect(
+ generateStorageCode(conn, { library: "obstore" }),
+ ).toMatchSnapshot();
});
it("with secrets", () => {
@@ -109,13 +125,17 @@ describe("generateStorageCode", () => {
account_name: prefixSecret("AZURE_ACCOUNT"),
account_key: prefixSecret("AZURE_KEY"),
};
- expect(generateStorageCode(conn, "obstore")).toMatchSnapshot();
+ expect(
+ generateStorageCode(conn, { library: "obstore" }),
+ ).toMatchSnapshot();
});
});
describe("CoreWeave", () => {
it("basic connection with all fields", () => {
- expect(generateStorageCode(baseCoreWeave, "obstore")).toMatchSnapshot();
+ expect(
+ generateStorageCode(baseCoreWeave, { library: "obstore" }),
+ ).toMatchSnapshot();
});
it("minimal connection (bucket and region only)", () => {
@@ -124,7 +144,9 @@ describe("generateStorageCode", () => {
bucket: "operator-bucket",
region: "US-EAST-04A",
};
- expect(generateStorageCode(conn, "obstore")).toMatchSnapshot();
+ expect(
+ generateStorageCode(conn, { library: "obstore" }),
+ ).toMatchSnapshot();
});
it("with secrets", () => {
@@ -135,39 +157,52 @@ describe("generateStorageCode", () => {
access_key_id: prefixSecret("COREWEAVE_OBJECT_STORAGE_KEY"),
secret_access_key: prefixSecret("COREWEAVE_OBJECT_STORAGE_SECRET"),
};
- expect(generateStorageCode(conn, "obstore")).toMatchSnapshot();
+ expect(
+ generateStorageCode(conn, { library: "obstore" }),
+ ).toMatchSnapshot();
});
});
describe("Google Drive", () => {
it("with service account credentials", () => {
- expect(generateStorageCode(baseGDrive, "fsspec")).toMatchSnapshot();
+ expect(
+ generateStorageCode(baseGDrive, { library: "fsspec" }),
+ ).toMatchSnapshot();
});
- it("with browser auth (no credentials)", () => {
+ it("with default auth (no credentials)", () => {
const conn: StorageConnection = {
type: "gdrive",
};
- expect(generateStorageCode(conn, "fsspec")).toMatchSnapshot();
+ expect(
+ generateStorageCode(conn, { library: "fsspec" }),
+ ).toMatchSnapshot();
+ });
+
+ it("with embedded auth (no credentials)", () => {
+ const conn: StorageConnection = {
+ type: "gdrive",
+ };
+ expect(
+ generateStorageCode(conn, { library: "fsspec", isEmbedded: true }),
+ ).toMatchSnapshot();
});
});
describe("invalid cases", () => {
it("throws for empty S3 bucket", () => {
expect(() =>
- generateStorageCode(
- { type: "s3", bucket: "" } as StorageConnection,
- "obstore",
- ),
+ generateStorageCode({ type: "s3", bucket: "" } as StorageConnection, {
+ library: "obstore",
+ }),
).toThrow();
});
it("throws for empty GCS bucket", () => {
expect(() =>
- generateStorageCode(
- { type: "gcs", bucket: "" } as StorageConnection,
- "obstore",
- ),
+ generateStorageCode({ type: "gcs", bucket: "" } as StorageConnection, {
+ library: "obstore",
+ }),
).toThrow();
});
@@ -179,7 +214,7 @@ describe("generateStorageCode", () => {
container: "",
account_name: "acct",
} as StorageConnection,
- "obstore",
+ { library: "obstore" },
),
).toThrow();
});
@@ -192,7 +227,7 @@ describe("generateStorageCode", () => {
container: "my-container",
account_name: "",
} as StorageConnection,
- "obstore",
+ { library: "obstore" },
),
).toThrow();
});
@@ -205,7 +240,7 @@ describe("generateStorageCode", () => {
bucket: "",
region: "US-EAST-04A",
} as StorageConnection,
- "obstore",
+ { library: "obstore" },
),
).toThrow();
});
@@ -218,7 +253,7 @@ describe("generateStorageCode", () => {
bucket: "operator-bucket",
region: "",
} as StorageConnection,
- "obstore",
+ { library: "obstore" },
),
).toThrow();
});
diff --git a/frontend/src/components/editor/connections/storage/add-storage-form.tsx b/frontend/src/components/editor/connections/storage/add-storage-form.tsx
index ec1cc1ba13..d1b7b750e9 100644
--- a/frontend/src/components/editor/connections/storage/add-storage-form.tsx
+++ b/frontend/src/components/editor/connections/storage/add-storage-form.tsx
@@ -5,6 +5,7 @@ import type { FieldValues } from "react-hook-form";
import type { z } from "zod";
import { ProtocolIcon } from "@/components/storage/components";
import type { KnownStorageProtocol } from "@/core/storage/types";
+import { useIframeCapabilities } from "@/hooks/useIframeCapabilities";
import { ConnectionForm, SelectorButton, SelectorGrid } from "../components";
import {
generateStorageCode,
@@ -108,6 +109,7 @@ export const AddStorageForm: React.FC<{
onSubmit: () => void;
header?: React.ReactNode;
}> = ({ onSubmit, header }) => {
+ const { isEmbedded } = useIframeCapabilities();
const [selectedSchema, setSelectedSchema] = useState<z.ZodType<
StorageConnection,
FieldValues
@@ -134,7 +136,9 @@ export const AddStorageForm: React.FC<{
preferredLibrary={libs?.preferred ?? "obstore"}
displayNames={StorageLibraryDisplayNames}
libraryLabel="Preferred storage library"
- generateCode={(values, library) => generateStorageCode(values, library)}
+ generateCode={(values, library) =>
+ generateStorageCode(values, { library, isEmbedded })
+ }
onSubmit={onSubmit}
onBack={() => setSelectedSchema(null)}
/>
diff --git a/frontend/src/components/editor/connections/storage/as-code.ts b/frontend/src/components/editor/connections/storage/as-code.ts
index 014c7d8d9d..c5fa6b58ba 100644
--- a/frontend/src/components/editor/connections/storage/as-code.ts
+++ b/frontend/src/components/editor/connections/storage/as-code.ts
@@ -7,6 +7,11 @@ import { type StorageConnection, StorageConnectionSchema } from "./schemas";
export type StorageLibrary = "obstore" | "fsspec";
+export interface StorageCodeOptions {
+ library: StorageLibrary;
+ isEmbedded?: boolean;
+}
+
export const StorageLibraryDisplayNames: Record<StorageLibrary, string> = {
obstore: "obstore",
fsspec: "fsspec",
@@ -161,8 +166,9 @@ function generateCoreWeaveCode(
function generateGDriveCode(
connection: Extract<StorageConnection, { type: "gdrive" }>,
- secrets: SecretContainer,
+ options: { secrets: SecretContainer; isEmbedded?: boolean },
): { imports: Set<string>; code: string } {
+ const { secrets, isEmbedded = false } = options;
const imports = new Set(["from gdrive_fsspec import GoogleDriveFileSystem"]);
if (connection.credentials_json) {
@@ -178,15 +184,20 @@ function generateGDriveCode(
return { imports, code };
}
- const code = dedent(`
- fs = GoogleDriveFileSystem(token="browser", use_listings_cache=False)
- `);
+ const code = isEmbedded
+ ? dedent(`
+ fs = GoogleDriveFileSystem(use_listings_cache=False, auth_kwargs={"use_local_webserver": False})
+ print("Google Drive connected! Important: Run this cell again to clear the console")
+ `)
+ : dedent(`
+ fs = GoogleDriveFileSystem(use_listings_cache=False)
+ `);
return { imports, code };
}
export function generateStorageCode(
connection: StorageConnection,
- _library: StorageLibrary,
+ options: StorageCodeOptions,
): string {
StorageConnectionSchema.parse(connection);
@@ -207,7 +218,10 @@ export function generateStorageCode(
result = generateCoreWeaveCode(connection, secrets);
break;
case "gdrive":
- result = generateGDriveCode(connection, secrets);
+ result = generateGDriveCode(connection, {
+ secrets,
+ isEmbedded: options.isEmbedded,
+ });
break;
default:
assertNever(connection);