-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat(table): locale-aware CSV and TSV exports #10661
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
kirangadhave
wants to merge
10
commits into
main
Choose a base branch
from
kg/mo-7455-locale-table-export
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
7a22005
feat(frontend): resolve locale for table export requests
kirangadhave 7277673
feat(table): add locale-aware delimited export dialect
kirangadhave 6f15d6e
feat(table): format delimited exports through Narwhals
kirangadhave 5dfee48
feat(table): plumb locale through download_as handlers
kirangadhave e58029b
feat(frontend): send locale with CSV and TSV table exports
kirangadhave 9ba153b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 8f250b4
fix(table): preserve locale export compatibility
kirangadhave 8f505e5
fix(table): harden delimited export paths
kirangadhave 18ad80c
fix(table): disambiguate export actions
kirangadhave ef964c5
perf(table): make localized Polars exports native-speed (#10674)
Light2Dark File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
frontend/src/components/data-table/__tests__/export-actions.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| /* Copyright 2026 Marimo. All rights reserved. */ | ||
|
|
||
| import { fireEvent, render, screen, waitFor } from "@testing-library/react"; | ||
| import type React from "react"; | ||
| import { beforeEach, describe, expect, it, vi } from "vitest"; | ||
| import { TooltipProvider } from "@/components/ui/tooltip"; | ||
| import { toast } from "@/components/ui/use-toast"; | ||
| import { downloadByURL } from "@/utils/download"; | ||
| import { | ||
| buildDownloadAsRequest, | ||
| ExportMenu, | ||
| sourceFormatForCopy, | ||
| } from "../export-actions"; | ||
|
|
||
| vi.mock("@/components/ui/use-toast", () => ({ | ||
| toast: vi.fn(() => ({ dismiss: vi.fn(), update: vi.fn() })), | ||
| })); | ||
|
|
||
| vi.mock("@/components/ui/tooltip", () => ({ | ||
| Tooltip: ({ children }: { children: React.ReactNode }) => children, | ||
| TooltipProvider: ({ children }: { children: React.ReactNode }) => children, | ||
| })); | ||
|
|
||
| vi.mock("@/utils/download", async (importOriginal) => { | ||
| const actual = await importOriginal<typeof import("@/utils/download")>(); | ||
| return { ...actual, downloadByURL: vi.fn() }; | ||
| }); | ||
|
|
||
| const PT_BR_LOCALE = { tag: "pt-BR", decimal_separator: "," }; | ||
|
|
||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| describe("ExportMenu", () => { | ||
| it("stops a download and shows a standalone export error", async () => { | ||
| const downloadAs = vi.fn().mockResolvedValue({ | ||
| url: "", | ||
| filename: "", | ||
| error: "The export locale is invalid.", | ||
| }); | ||
| render( | ||
| <TooltipProvider> | ||
| <ExportMenu downloadAs={downloadAs} /> | ||
| </TooltipProvider>, | ||
| ); | ||
|
|
||
| fireEvent.keyDown(screen.getByTestId("export-button"), { | ||
| key: "ArrowDown", | ||
| }); | ||
| fireEvent.click((await screen.findAllByText("CSV"))[0]); | ||
|
|
||
| await waitFor(() => { | ||
| expect(toast).toHaveBeenCalledWith({ | ||
| title: "Export failed", | ||
| description: "The export locale is invalid.", | ||
| variant: "danger", | ||
| }); | ||
| }); | ||
| expect(downloadByURL).not.toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
|
|
||
| describe("buildDownloadAsRequest", () => { | ||
| it("includes locale for CSV and TSV", () => { | ||
| expect(buildDownloadAsRequest("csv", "pt-BR")).toEqual({ | ||
| format: "csv", | ||
| locale: PT_BR_LOCALE, | ||
| }); | ||
| expect(buildDownloadAsRequest("tsv", "pt-BR")).toEqual({ | ||
| format: "tsv", | ||
| locale: PT_BR_LOCALE, | ||
| }); | ||
| }); | ||
|
|
||
| it("omits locale for JSON and Parquet", () => { | ||
| expect(buildDownloadAsRequest("json", "pt-BR")).toEqual({ format: "json" }); | ||
| expect(buildDownloadAsRequest("parquet", "pt-BR")).toEqual({ | ||
| format: "parquet", | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe("file and clipboard request parity", () => { | ||
| it("sends the same locale for CSV download and clipboard copy", () => { | ||
| const download = buildDownloadAsRequest("csv", "pt-BR"); | ||
| const clipboard = buildDownloadAsRequest( | ||
| sourceFormatForCopy("csv"), | ||
| "pt-BR", | ||
| ); | ||
| expect(download).toEqual({ format: "csv", locale: PT_BR_LOCALE }); | ||
| expect(clipboard).toEqual(download); | ||
| }); | ||
|
|
||
| it("sends the same locale for TSV download and clipboard copy", () => { | ||
| const download = buildDownloadAsRequest("tsv", "pt-BR"); | ||
| const clipboard = buildDownloadAsRequest( | ||
| sourceFormatForCopy("tsv"), | ||
| "pt-BR", | ||
| ); | ||
| expect(download).toEqual({ format: "tsv", locale: PT_BR_LOCALE }); | ||
| expect(clipboard).toEqual(download); | ||
| }); | ||
|
|
||
| it("omits locale for JSON, Parquet, and Markdown source requests", () => { | ||
| expect(buildDownloadAsRequest("json", "pt-BR")).toEqual({ format: "json" }); | ||
| expect(buildDownloadAsRequest("parquet", "pt-BR")).toEqual({ | ||
| format: "parquet", | ||
| }); | ||
| expect( | ||
| buildDownloadAsRequest(sourceFormatForCopy("json"), "pt-BR"), | ||
| ).toEqual({ format: "json" }); | ||
| expect( | ||
| buildDownloadAsRequest(sourceFormatForCopy("markdown"), "pt-BR"), | ||
| ).toEqual({ format: "json" }); | ||
| }); | ||
| }); | ||
46 changes: 46 additions & 0 deletions
46
frontend/src/components/data-table/__tests__/export-locale.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| /* Copyright 2026 Marimo. All rights reserved. */ | ||
|
|
||
| import { describe, expect, it } from "vitest"; | ||
| import { resolveExportLocale } from "../export-locale"; | ||
| import { DownloadAsSchema } from "../schemas"; | ||
|
|
||
| describe("resolveExportLocale", () => { | ||
| it("resolves a decimal point for en-US", () => { | ||
| expect(resolveExportLocale("en-US")).toEqual({ | ||
| tag: "en-US", | ||
| decimal_separator: ".", | ||
| }); | ||
| }); | ||
|
|
||
| it("resolves a decimal comma for pt-BR", () => { | ||
| expect(resolveExportLocale("pt-BR").decimal_separator).toBe(","); | ||
| }); | ||
|
|
||
| it("preserves a non-comma Unicode decimal separator", () => { | ||
| expect(resolveExportLocale("ar-EG").decimal_separator).toBe("٫"); | ||
| }); | ||
|
|
||
| it("rejects an invalid locale", () => { | ||
| expect(() => resolveExportLocale("not_a_locale")).toThrow(); | ||
| }); | ||
|
|
||
| it("rejects an empty locale tag", () => { | ||
| expect(() => resolveExportLocale("")).toThrow(); | ||
| }); | ||
| }); | ||
|
|
||
| describe("DownloadAsSchema", () => { | ||
| it("accepts a request with only format", () => { | ||
| expect(DownloadAsSchema.input.parse({ format: "csv" })).toEqual({ | ||
| format: "csv", | ||
| }); | ||
| }); | ||
|
|
||
| it("accepts an optional locale value", () => { | ||
| const locale = { tag: "pt-BR", decimal_separator: "," }; | ||
| expect(DownloadAsSchema.input.parse({ format: "csv", locale })).toEqual({ | ||
| format: "csv", | ||
| locale, | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| /* Copyright 2026 Marimo. All rights reserved. */ | ||
|
|
||
| export interface ResolvedExportLocale { | ||
| tag: string; | ||
| decimal_separator: string; | ||
| } | ||
|
|
||
| const FORBIDDEN_DECIMAL_SEPARATORS = new Set(["\r", "\n", "\0"]); | ||
|
|
||
| export function resolveExportLocale(locale: string): ResolvedExportLocale { | ||
| if (locale === "") { | ||
| throw new Error("Locale tag must be a non-empty string."); | ||
| } | ||
|
|
||
| const decimalSeparator = new Intl.NumberFormat(locale) | ||
| .formatToParts(1.1) | ||
| .find((part) => part.type === "decimal")?.value; | ||
|
|
||
| if ( | ||
| decimalSeparator === undefined || | ||
| [...decimalSeparator].length !== 1 || | ||
| FORBIDDEN_DECIMAL_SEPARATORS.has(decimalSeparator) | ||
| ) { | ||
| throw new Error( | ||
| `Locale "${locale}" did not resolve a valid decimal separator.`, | ||
| ); | ||
| } | ||
|
|
||
| return { | ||
| tag: locale, | ||
| decimal_separator: decimalSeparator, | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.