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
fix: export filenames are unreadable in molab (#10478)
Shahmir Varqhafc3c3491fa78
diff --git a/frontend/src/components/editor/actions/export-dialog/export-command.ts b/frontend/src/components/editor/actions/export-dialog/export-command.tsindex 94ca0def80..eab6fba4dc 100644--- a/frontend/src/components/editor/actions/export-dialog/export-command.ts+++ b/frontend/src/components/editor/actions/export-dialog/export-command.ts@@ -1,18 +1,13 @@/* Copyright 2026 Marimo. All rights reserved. */+import {+ MARKDOWN_EXTENSIONS,+ MARKDOWN_SUFFIXES,+} from "@/core/network/markdown-export";import { Filenames } from "@/utils/filenames";import { shellQuote } from "@/utils/shell";import type { ExportFormat, ExportOptions, MarkdownFlavor } from "./state";-const MARKDOWN_EXTENSIONS: Record<MarkdownFlavor, string> = {- pymdown: "md",- qmd: "qmd",- mystmd: "myst.md",- mdx: "mdx",-};--const MARKDOWN_SUFFIXES = [".myst.md", ".markdown", ".qmd", ".mdx", ".md"];-function inferMarkdownFlavor(filename: string): MarkdownFlavor {if (filename.endsWith(".myst.md")) {return "mystmd";diff --git a/frontend/src/core/network/__tests__/api.test.ts b/frontend/src/core/network/__tests__/api.test.tsindex f80a1e6bc9..011e4d8268 100644--- a/frontend/src/core/network/__tests__/api.test.ts+++ b/frontend/src/core/network/__tests__/api.test.ts@@ -105,7 +105,10 @@ describe("API", () => {});await expect(- API.handleExportResponse({ data: "markdown", response }),+ API.handleExportResponse(+ { data: "markdown", response },+ { defaultFilename: "download.md" },+ ),).resolves.toEqual({contents: "markdown",filename: "résumé.qmd",@@ -113,27 +116,139 @@ describe("API", () => {});});- it("rejects export responses without a filename", async () => {+ it("parses quoted Content-Disposition filenames", async () => {+ const response = new Response("html", {+ headers: {+ "Content-Disposition": 'inline; filename="notebook.html"',+ "Content-Type": "text/html; charset=utf-8",+ },+ });+await expect(- API.handleExportResponse({- data: "markdown",- response: new Response(null, {- headers: { "Content-Type": "text/plain" },- }),- }),- ).rejects.toThrow("Export response is missing a filename");+ API.handleExportResponse(+ { data: "html", response },+ { defaultFilename: "download.html" },+ ),+ ).resolves.toEqual({+ contents: "html",+ filename: "notebook.html",+ mediaType: "text/html; charset=utf-8",+ });+ });++ it("parses unquoted Content-Disposition filenames", async () => {+ const response = new Response("pdf", {+ headers: {+ "Content-Disposition": "attachment; filename=notebook.pdf",+ "Content-Type": "application/pdf",+ },+ });++ await expect(+ API.handleExportResponse(+ { data: "pdf", response },+ { defaultFilename: "download.pdf" },+ ),+ ).resolves.toEqual({+ contents: "pdf",+ filename: "notebook.pdf",+ mediaType: "application/pdf",+ });+ });++ it("prefers RFC 5987 filenames over quoted fallbacks", async () => {+ const response = new Response("markdown", {+ headers: {+ "Content-Disposition":+ "attachment; filename=\"wrong.md\"; filename*=UTF-8''notebook.md",+ "Content-Type": "text/plain; charset=utf-8",+ },+ });++ await expect(+ API.handleExportResponse(+ { data: "markdown", response },+ { defaultFilename: "download.md" },+ ),+ ).resolves.toEqual({+ contents: "markdown",+ filename: "notebook.md",+ mediaType: "text/plain; charset=utf-8",+ });+ });++ it("falls back to a default filename when headers are missing", async () => {+ const response = new Response("markdown", {+ headers: {+ "Content-Type": "text/plain; charset=utf-8",+ },+ });++ await expect(+ API.handleExportResponse(+ { data: "markdown", response },+ { defaultFilename: "download.md" },+ ),+ ).resolves.toEqual({+ contents: "markdown",+ filename: "download.md",+ mediaType: "text/plain; charset=utf-8",+ });+ });++ it("falls back to a default filename when Content-Disposition is unparsable", async () => {+ const response = new Response("markdown", {+ headers: {+ "Content-Disposition": "attachment",+ "Content-Type": "text/plain; charset=utf-8",+ },+ });++ await expect(+ API.handleExportResponse(+ { data: "markdown", response },+ { defaultFilename: "download.md" },+ ),+ ).resolves.toEqual({+ contents: "markdown",+ filename: "download.md",+ mediaType: "text/plain; charset=utf-8",+ });+ });++ it("falls back to a default filename when RFC 5987 encoding is malformed", async () => {+ const response = new Response("markdown", {+ headers: {+ "Content-Disposition": "attachment; filename*=UTF-8''%E0%A4%A",+ "Content-Type": "text/plain; charset=utf-8",+ },+ });++ await expect(+ API.handleExportResponse(+ { data: "markdown", response },+ { defaultFilename: "download.md" },+ ),+ ).resolves.toEqual({+ contents: "markdown",+ filename: "download.md",+ mediaType: "text/plain; charset=utf-8",+ });});it("rejects export responses without a media type", async () => {await expect(- API.handleExportResponse({- data: "markdown",- response: new Response(null, {- headers: {- "Content-Disposition": "attachment; filename*=UTF-8''notebook.md",- },- }),- }),+ API.handleExportResponse(+ {+ data: "markdown",+ response: new Response(null, {+ headers: {+ "Content-Disposition": "attachment; filename*=UTF-8''notebook.md",+ },+ }),+ },+ { defaultFilename: "download.md" },+ ),).rejects.toThrow("Export response is missing a media type");});});diff --git a/frontend/src/core/network/__tests__/export-filename.test.ts b/frontend/src/core/network/__tests__/export-filename.test.tsnew file mode 100644index 0000000000..a92d77b20b--- /dev/null+++ b/frontend/src/core/network/__tests__/export-filename.test.ts@@ -0,0 +1,43 @@+/* Copyright 2026 Marimo. All rights reserved. */++import { beforeEach, describe, expect, it } from "vitest";+import { filenameAtom } from "@/core/saving/file-state";+import { store } from "@/core/state/jotai";+import {+ getDefaultExportFilename,+ getDefaultMarkdownExportFilename,+} from "../export-filename";++describe("default export filenames", () => {+ beforeEach(() => {+ store.set(filenameAtom, "folder/my_notebook.py");+ });++ it("derives filenames from the notebook name", () => {+ expect(getDefaultExportFilename("html")).toBe("my_notebook.html");+ expect(getDefaultExportFilename("pdf")).toBe("my_notebook.pdf");+ });++ it("handles Windows-style notebook paths", () => {+ store.set(filenameAtom, "C:\\Users\\me\\my_notebook.py");+ expect(getDefaultExportFilename("html")).toBe("my_notebook.html");+ });++ it("derives markdown filenames from the selected flavor", () => {+ expect(getDefaultMarkdownExportFilename("qmd")).toBe("my_notebook.qmd");+ expect(getDefaultMarkdownExportFilename("mystmd")).toBe(+ "my_notebook.myst.md",+ );+ });++ it("derives script export filenames from the notebook name", () => {+ expect(getDefaultExportFilename("script.py")).toBe("my_notebook.script.py");+ });++ it("falls back to download.* when the notebook is unnamed", () => {+ store.set(filenameAtom, null);+ expect(getDefaultExportFilename("html")).toBe("download.html");+ expect(getDefaultMarkdownExportFilename("qmd")).toBe("download.qmd");+ expect(getDefaultExportFilename("script.py")).toBe("download.script.py");+ });+});diff --git a/frontend/src/core/network/api.ts b/frontend/src/core/network/api.tsindex 5b48cae5b0..2ae6fc0054 100644--- a/frontend/src/core/network/api.ts+++ b/frontend/src/core/network/api.ts@@ -16,13 +16,45 @@ function getBaseUriWithoutQueryParams(): string {return url.toString();}-function getExportFilename(response: Response): string {- const contentDisposition = response.headers.get("Content-Disposition");- const match = contentDisposition?.match(/filename\*=UTF-8''([^;]+)/i);- if (!match) {- throw new Error("Export response is missing a filename");+function parseContentDispositionFilename(+ contentDisposition: string | null,+): string | undefined {+ if (!contentDisposition) {+ return undefined;}- return decodeURIComponent(match[1]);++ const rfc5987Match = contentDisposition.match(/filename\*=UTF-8''([^;]+)/i);+ if (rfc5987Match) {+ try {+ return decodeURIComponent(rfc5987Match[1]);+ } catch {+ return undefined;+ }+ }++ const quotedMatch = contentDisposition.match(/filename="([^"]+)"/i);+ if (quotedMatch) {+ return quotedMatch[1];+ }++ const unquotedMatch = contentDisposition.match(/filename=([^;]+)/i);+ if (unquotedMatch) {+ return unquotedMatch[1].trim();+ }++ return undefined;+}++function getExportFilename(response: Response): string | undefined {+ const filename = parseContentDispositionFilename(+ response.headers.get("Content-Disposition"),+ );+ if (!filename) {+ Logger.warn(+ "Export response is missing a readable Content-Disposition filename",+ );+ }+ return filename;}/**@@ -129,11 +161,14 @@ export const API = {}return Promise.resolve(response.data as T);},- handleExportResponse: async <T extends BlobPart>(response: {- data?: T | undefined;- error?: Record<string, unknown>;- response: Response;- }): Promise<ExportedFile<T>> => {+ handleExportResponse: async <T extends BlobPart>(+ response: {+ data?: T | undefined;+ error?: Record<string, unknown>;+ response: Response;+ },+ opts: { defaultFilename: string },+ ): Promise<ExportedFile<T>> => {const contents = await API.handleResponse<T>(response);const mediaType = response.response.headers.get("Content-Type");if (!mediaType) {@@ -142,7 +177,7 @@ export const API = {return {contents,- filename: getExportFilename(response.response),+ filename: getExportFilename(response.response) ?? opts.defaultFilename,mediaType,};},diff --git a/frontend/src/core/network/export-filename.ts b/frontend/src/core/network/export-filename.tsnew file mode 100644index 0000000000..bed2e9ece7--- /dev/null+++ b/frontend/src/core/network/export-filename.ts@@ -0,0 +1,37 @@+/* Copyright 2026 Marimo. All rights reserved. */++import { filenameAtom } from "@/core/saving/file-state";+import { store } from "@/core/state/jotai";+import { Filenames } from "@/utils/filenames";+import { Paths } from "@/utils/paths";+import {+ DEFAULT_MARKDOWN_FLAVOR,+ MARKDOWN_EXTENSIONS,+} from "./markdown-export";+import type { ExportAsMarkdownRequest } from "./types";++const DEFAULT_EXPORT_FILENAME = "download";++function getNotebookStem(): string | undefined {+ const filename = store.get(filenameAtom);+ if (!filename) {+ return undefined;+ }+ const basename = Paths.basename(filename);+ return Filenames.withoutExtension(basename);+}++export function getDefaultExportFilename(extension: string): string {+ const stem = getNotebookStem();+ if (stem) {+ return `${stem}.${extension}`;+ }+ return `${DEFAULT_EXPORT_FILENAME}.${extension}`;+}++export function getDefaultMarkdownExportFilename(+ flavor: ExportAsMarkdownRequest["flavor"],+): string {+ const extension = MARKDOWN_EXTENSIONS[flavor ?? DEFAULT_MARKDOWN_FLAVOR];+ return getDefaultExportFilename(extension);+}diff --git a/frontend/src/core/network/markdown-export.ts b/frontend/src/core/network/markdown-export.tsnew file mode 100644index 0000000000..0458aa3cfc--- /dev/null+++ b/frontend/src/core/network/markdown-export.ts@@ -0,0 +1,19 @@+/* Copyright 2026 Marimo. All rights reserved. */++import type { MarkdownExportFlavor } from "./types";++export const MARKDOWN_EXTENSIONS = {+ pymdown: "md",+ qmd: "qmd",+ mystmd: "myst.md",+ mdx: "mdx",+} satisfies Record<MarkdownExportFlavor, string>;+export const MARKDOWN_SUFFIXES = [+ ".myst.md",+ ".markdown",+ ".qmd",+ ".mdx",+ ".md",+];++export const DEFAULT_MARKDOWN_FLAVOR: MarkdownExportFlavor = "pymdown";diff --git a/frontend/src/core/network/requests-network.ts b/frontend/src/core/network/requests-network.tsindex 9307ca58bd..e47018b367 100644--- a/frontend/src/core/network/requests-network.ts+++ b/frontend/src/core/network/requests-network.ts@@ -3,6 +3,10 @@import { once } from "@/utils/once";import { getRuntimeManager } from "../runtime/config";import { API, createClientWithRuntimeManager } from "./api";+import {+ getDefaultExportFilename,+ getDefaultMarkdownExportFilename,+} from "./export-filename";import {waitForConnectionOpen,waitForConnectionOpenIfNotebook,@@ -426,7 +430,11 @@ export function createNetworkRequests(): EditRequests & RunRequests {parseAs: "text",params: getParams(),})- .then(handleExportResponse);+ .then((response) =>+ handleExportResponse(response, {+ defaultFilename: getDefaultExportFilename("html"),+ }),+ );},exportAsMarkdown: async (request) => {return getClient()@@ -435,7 +443,11 @@ export function createNetworkRequests(): EditRequests & RunRequests {parseAs: "text",params: getParams(),})- .then(handleExportResponse);+ .then((response) =>+ handleExportResponse(response, {+ defaultFilename: getDefaultMarkdownExportFilename(request.flavor),+ }),+ );},exportAsScript: async (request) => {return getClient()@@ -444,7 +456,11 @@ export function createNetworkRequests(): EditRequests & RunRequests {parseAs: "text",params: getParams(),})- .then(handleExportResponse);+ .then((response) =>+ handleExportResponse(response, {+ defaultFilename: getDefaultExportFilename("script.py"),+ }),+ );},exportAsIPYNB: async (request) => {return getClient()@@ -453,7 +469,11 @@ export function createNetworkRequests(): EditRequests & RunRequests {parseAs: "text",params: getParams(),})- .then(handleExportResponse);+ .then((response) =>+ handleExportResponse(response, {+ defaultFilename: getDefaultExportFilename("ipynb"),+ }),+ );},exportAsPDF: async (request) => {return getClient()@@ -462,7 +482,11 @@ export function createNetworkRequests(): EditRequests & RunRequests {parseAs: "blob",params: getParams(),})- .then(handleExportResponse);+ .then((response) =>+ handleExportResponse(response, {+ defaultFilename: getDefaultExportFilename("pdf"),+ }),+ );},autoExportAsHTML: async (request) => {return getClient()diff --git a/frontend/src/core/network/types.ts b/frontend/src/core/network/types.tsindex f6a88a6be8..b74c4ffcf0 100644--- a/frontend/src/core/network/types.ts+++ b/frontend/src/core/network/types.ts@@ -22,6 +22,9 @@ export type AutoExportAsMarkdownRequest =schemas["AutoExportAsMarkdownRequest"];export type ExportAsHTMLRequest = schemas["ExportAsHTMLRequest"];export type ExportAsMarkdownRequest = schemas["ExportAsMarkdownRequest"];+export type MarkdownExportFlavor = NonNullable<+ ExportAsMarkdownRequest["flavor"]+>;export type ExportAsIPYNBRequest = schemas["ExportAsIPYNBRequest"];export type ExportAsScriptRequest = schemas["ExportAsScriptRequest"];export type ExportAsPDFRequest = schemas["ExportAsPDFRequest"];