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: --base-url support for marimo edit/new (#1651)

Myles Scolnick5e1cc8dbf31d
diff --git a/frontend/src/components/editor/ai/add-cell-with-ai.tsx b/frontend/src/components/editor/ai/add-cell-with-ai.tsx
index f848ac3bda..2c78943c52 100644
--- a/frontend/src/components/editor/ai/add-cell-with-ai.tsx
+++ b/frontend/src/components/editor/ai/add-cell-with-ai.tsx
@@ -10,6 +10,7 @@ import { prettyError } from "@/utils/errors";
import { useCompletion } from "ai/react";
import ReactCodeMirror, { EditorView } from "@uiw/react-codemirror";
import { customPythonLanguageSupport } from "@/core/codemirror/language/python";
+import { asURL } from "@/utils/url";
const extensions = [customPythonLanguageSupport(), EditorView.lineWrapping];
@@ -30,7 +31,7 @@ export const AddCellWithAI: React.FC<{
handleInputChange,
handleSubmit,
} = useCompletion({
- api: "/api/ai/completion",
+ api: asURL(`api/ai/completion`).toString(),
headers: API.headers(),
streamMode: "text",
body: {
diff --git a/frontend/src/components/editor/cell/code/ai-completion-editor.tsx b/frontend/src/components/editor/cell/code/ai-completion-editor.tsx
index 484f6791c5..0e0487b5b3 100644
--- a/frontend/src/components/editor/cell/code/ai-completion-editor.tsx
+++ b/frontend/src/components/editor/cell/code/ai-completion-editor.tsx
@@ -19,6 +19,7 @@ import { includeOtherCellsAtom } from "@/core/ai/state";
import { Checkbox } from "@/components/ui/checkbox";
import { getCodes } from "@/core/codemirror/copilot/getCodes";
import { useTheme } from "@/theme/useTheme";
+import { asURL } from "@/utils/url";
const Original = CodeMirrorMerge.Original;
const Modified = CodeMirrorMerge.Modified;
@@ -58,7 +59,7 @@ export const AiCompletionEditor: React.FC<Props> = ({
handleInputChange,
handleSubmit,
} = useCompletion({
- api: "/api/ai/completion",
+ api: asURL("api/ai/completion").toString(),
headers: API.headers(),
streamMode: "text",
body: {
diff --git a/frontend/src/components/pages/home-page.tsx b/frontend/src/components/pages/home-page.tsx
index fcd7eb3c9b..983f53fe43 100644
--- a/frontend/src/components/pages/home-page.tsx
+++ b/frontend/src/components/pages/home-page.tsx
@@ -29,6 +29,7 @@ import { ConfigButton } from "../app-config/app-config-button";
import { Checkbox } from "@/components/ui/checkbox";
import { Label } from "@/components/ui/label";
import { MarkdownIcon } from "@/components/editor/cell/code/icons";
+import { asURL } from "@/utils/url";
function tabTarget(path: string) {
// Consistent tab target so we open in the same tab when clicking on the same notebook
@@ -97,7 +98,7 @@ const HomePage: React.FC = () => {
/>
</div>
<div className="flex flex-col gap-8 max-w-5xl container pt-10 pb-20 z-10">
- <img src="/logo.png" alt="Marimo Logo" className="w-64 mb-4" />
+ <img src="logo.png" alt="marimo logo" className="w-64 mb-4" />
<CreateNewNotebook />
<NotebookList
header="Running notebooks"
@@ -177,8 +178,8 @@ const NotebookList: React.FC<{
// We want to keep the sessionId in this case
const isNewNotebook = isSessionId(file.path);
const href = isNewNotebook
- ? `/?file=${file.initializationId}&session_id=${file.path}`
- : `/?file=${file.path}`;
+ ? asURL(`?file=${file.initializationId}&session_id=${file.path}`)
+ : asURL(`?file=${file.path}`);
const isMarkdown = file.path.endsWith(".md");
@@ -186,7 +187,7 @@ const NotebookList: React.FC<{
<a
className="py-2 px-4 hover:bg-[var(--blue-2)] hover:text-primary transition-all duration-300 cursor-pointer group relative"
key={file.path}
- href={href}
+ href={href.toString()}
target={tabTarget(file.initializationId || file.path)}
>
<div className="flex flex-col justify-between">
@@ -283,7 +284,7 @@ const CreateNewNotebook: React.FC = () => {
text-primary hover:bg-[var(--blue-2)] shadow-smAccent border
transition-all duration-300 cursor-pointer
"
- href={`/?file=${initializationId}`}
+ href={asURL(`?file=${initializationId}`).toString()}
target={tabTarget(initializationId)}
>
<h2 className="text-lg font-semibold">Create a new notebook</h2>
diff --git a/frontend/src/utils/__tests__/url.test.ts b/frontend/src/utils/__tests__/url.test.ts
index cad36ac945..001d188a2c 100644
--- a/frontend/src/utils/__tests__/url.test.ts
+++ b/frontend/src/utils/__tests__/url.test.ts
@@ -35,6 +35,20 @@ describe("asURL function", () => {
const result = asURL(path);
expect(result.toString()).toBe("https://example.com/path/to/resource");
});
+
+ test("relative with query params", () => {
+ const path = "path/to/resource?query=param";
+ const result = asURL(path);
+ expect(result.toString()).toBe(
+ "https://example.com/path/to/resource?query=param",
+ );
+ });
+
+ test("just query params", () => {
+ const path = "?query=param";
+ const result = asURL(path);
+ expect(result.toString()).toBe("https://example.com/?query=param");
+ });
});
describe("when document.baseURI is set", () => {
@@ -47,16 +61,14 @@ describe("asURL function", () => {
});
test('should handle relative path starting with "./"', () => {
- const path = "./path/to/resource";
- const result = asURL(path);
+ const result = asURL("./path/to/resource");
expect(result.toString()).toBe(
"https://example.com/base/path/to/resource",
);
});
test('should handle absolute path starting with "/"', () => {
- const path = "/path/to/resource";
- const result = asURL(path);
+ const result = asURL("/path/to/resource");
expect(result.toString()).toBe("https://example.com/path/to/resource");
});
@@ -67,11 +79,22 @@ describe("asURL function", () => {
});
test('should handle relative path without "./"', () => {
- const path = "path/to/resource";
- const result = asURL(path);
+ const result = asURL("path/to/resource");
expect(result.toString()).toBe(
"https://example.com/base/path/to/resource",
);
});
+
+ test("relative with query params", () => {
+ const result = asURL("path/to/resource?query=param");
+ expect(result.toString()).toBe(
+ "https://example.com/base/path/to/resource?query=param",
+ );
+ });
+
+ test("just query params", () => {
+ const result = asURL("?query=param");
+ expect(result.toString()).toBe("https://example.com/base/?query=param");
+ });
});
});
diff --git a/marimo/_cli/cli.py b/marimo/_cli/cli.py
index ec151e0c4f..6afb01d907 100644
--- a/marimo/_cli/cli.py
+++ b/marimo/_cli/cli.py
@@ -236,6 +236,14 @@ edit_help_msg = "\n".join(
type=str,
help=token_password_message,
)
+ "--base-url",
+ default="",
+ show_default=True,
+ type=str,
+ help="Base URL for the server. Should start with a /.",
+ callback=validators.base_url,
+)
@click.argument("name", required=False)
@click.argument("args", nargs=-1, type=click.UNPROCESSED)
def edit(
@@ -245,6 +253,7 @@ def edit(
headless: bool,
token: bool,
token_password: Optional[str],
+ base_url: str,
name: Optional[str],
args: tuple[str],
) -> None:
@@ -287,6 +296,7 @@ def edit(
watch=False,
cli_args=parse_args(args),
auth_token=_resolve_token(token, token_password),
+ base_url=base_url,
)
@@ -334,6 +344,14 @@ def edit(
type=str,
help=token_password_message,
)
+ "--base-url",
+ default="",
+ show_default=True,
+ type=str,
+ help="Base URL for the server. Should start with a /.",
+ callback=validators.base_url,
+)
def new(
port: Optional[int],
host: str,
@@ -341,6 +359,7 @@ def new(
headless: bool,
token: bool,
token_password: Optional[str],
+ base_url: str,
) -> None:
start(
file_router=AppFileRouter.new_file(),
@@ -355,6 +374,7 @@ def new(
watch=False,
cli_args={},
auth_token=_resolve_token(token, token_password),
+ base_url=base_url,
)