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: use url hash instead of query param (#1001)

Myles Scolnicka6bc85511580
diff --git a/frontend/src/core/pyodide/__tests__/router.test.ts b/frontend/src/core/pyodide/__tests__/router.test.ts
index 3f6c9d9413..d4eb0784ef 100644
--- a/frontend/src/core/pyodide/__tests__/router.test.ts
+++ b/frontend/src/core/pyodide/__tests__/router.test.ts
@@ -29,7 +29,22 @@ describe("URLPyodideRouter", () => {
test("getCode returns correct code from URL", () => {
window.location.href = 'https://marimo.app?code=print("Hello, World!")';
- expect(PyodideRouter.getCode()).toBe('print("Hello, World!")');
+ expect(PyodideRouter.getCodeFromSearchParam()).toBe(
+ 'print("Hello, World!")',
+ );
+ });
+
+ test("getCode from hash returns correct code", () => {
+ window.location.href = "https://marimo.app";
+ window.location.hash = "#code/print('Hello, World!')";
+ expect(PyodideRouter.getCodeFromHash()).toBe("print('Hello, World!')");
+ });
+
+ test("getCode from hash with query params returns correct code", () => {
+ window.location.href = "https://marimo.app?filename=test.py";
+ window.location.hash = "#code/print('Hello, World!')";
+ expect(PyodideRouter.getCodeFromHash()).toBe("print('Hello, World!')");
+ expect(PyodideRouter.getFilename()).toBe("test.py");
});
test("setFilename correctly modifies URL", () => {
diff --git a/frontend/src/core/pyodide/__tests__/share.test.ts b/frontend/src/core/pyodide/__tests__/share.test.ts
new file mode 100644
index 0000000000..f041e2007c
--- /dev/null
+++ b/frontend/src/core/pyodide/__tests__/share.test.ts
@@ -0,0 +1,35 @@
+/* Copyright 2024 Marimo. All rights reserved. */
+
+import { expect, describe, it } from "vitest";
+import { createShareableLink } from "../share";
+import { compressToEncodedURIComponent } from "lz-string";
+
+describe("createShareableLink", () => {
+ it("should return a URL with the base URL when no code is provided", () => {
+ const result = createShareableLink({ code: null });
+ expect(result).toBe("https://marimo.app/");
+ });
+
+ it("should return a URL with the provided base URL when no code is provided", () => {
+ const result = createShareableLink({
+ code: null,
+ baseUrl: "https://test.com",
+ });
+ expect(result).toBe("https://test.com/");
+ });
+
+ it("should return a URL with the compressed code in the hash when code is provided", () => {
+ const code = 'console.log("Hello, World!")';
+ const compressed = compressToEncodedURIComponent(code);
+ const result = createShareableLink({ code });
+ expect(result).toBe(`https://marimo.app/#code/${compressed}`);
+ });
+
+ it("should return a URL with the compressed code in the hash and the provided base URL when code is provided", () => {
+ const code = 'console.log("Hello, World!")';
+ const baseUrl = "https://test.com/";
+ const compressed = compressToEncodedURIComponent(code);
+ const result = createShareableLink({ code, baseUrl });
+ expect(result).toBe(`${baseUrl}#code/${compressed}`);
+ });
+});
diff --git a/frontend/src/core/pyodide/bridge.ts b/frontend/src/core/pyodide/bridge.ts
index ac4bc53b27..f5101a2ee6 100644
--- a/frontend/src/core/pyodide/bridge.ts
+++ b/frontend/src/core/pyodide/bridge.ts
@@ -31,7 +31,6 @@ import { isPyodide } from "./utils";
import { Deferred } from "@/utils/Deferred";
import { createShareableLink } from "./share";
import { PyodideRouter } from "./router";
-import { Paths } from "@/utils/paths";
import { getMarimoVersion } from "../dom/marimo-tag";
import { getWorkerRPC } from "./rpc";
import { API } from "../network/api";
@@ -237,12 +236,9 @@ export class PyodideBridge implements RunRequests, EditRequests {
};
openFile = async (request: { path: string }): Promise<null> => {
- // Open the file in a new tab by file path
- const filename = Paths.basename(request.path);
const url = createShareableLink({
code: null,
baseUrl: window.location.origin,
- filename,
});
window.open(url, "_blank");
return null;
diff --git a/frontend/src/core/pyodide/router.ts b/frontend/src/core/pyodide/router.ts
index 5643eb6997..7a8677189c 100644
--- a/frontend/src/core/pyodide/router.ts
+++ b/frontend/src/core/pyodide/router.ts
@@ -7,10 +7,19 @@ class URLPyodideRouter {
this.setSearchParam("filename", filename);
}
- getCode(): string | null {
+ getCodeFromSearchParam(): string | null {
return this.getSearchParam("code");
}
+ getCodeFromHash(): string | null {
+ const hash = window.location.hash;
+ const prefix = "#code/";
+ if (!hash.startsWith(prefix)) {
+ return null;
+ }
+ return hash.slice(prefix.length);
+ }
+
private setSearchParam(key: string, value: string) {
const url = new URL(window.location.href);
url.searchParams.set(key, value);
diff --git a/frontend/src/core/pyodide/share.ts b/frontend/src/core/pyodide/share.ts
index fa04b73a79..f2d477409b 100644
--- a/frontend/src/core/pyodide/share.ts
+++ b/frontend/src/core/pyodide/share.ts
@@ -4,16 +4,12 @@ import { compressToEncodedURIComponent } from "lz-string";
export function createShareableLink(opts: {
code: string | null;
baseUrl?: string;
- filename?: string | null;
}): string {
- const { code, baseUrl = "https://marimo.app", filename } = opts;
+ const { code, baseUrl = "https://marimo.app" } = opts;
const url = new URL(baseUrl);
if (code) {
const compressed = compressToEncodedURIComponent(code);
- url.searchParams.set("code", compressed);
- }
- if (filename) {
- url.searchParams.set("filename", filename);
+ url.hash = `#code/${compressed}`;
}
return url.href;
}
diff --git a/frontend/src/core/pyodide/store.ts b/frontend/src/core/pyodide/store.ts
index 4a8c7fd909..cdc3856fdd 100644
--- a/frontend/src/core/pyodide/store.ts
+++ b/frontend/src/core/pyodide/store.ts
@@ -23,7 +23,8 @@ const urlFileStore: FileStore = {
// Do nothing
},
readFile() {
- const code = PyodideRouter.getCode();
+ const code =
+ PyodideRouter.getCodeFromHash() || PyodideRouter.getCodeFromSearchParam();
if (!code) {
return null;
}