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.

feat: export geometry columns as GeoParquet (#11063)

Kiran Gadhave54a3b92be6e8
diff --git a/frontend/plugins.openapi.yaml b/frontend/plugins.openapi.yaml
index 19a461abce..8d8edb453f 100644
--- a/frontend/plugins.openapi.yaml
+++ b/frontend/plugins.openapi.yaml
@@ -779,6 +779,64 @@ paths:
responses:
"204":
description: Accepted.
+ /plugins/marimo-dataframe/functions/get_export_metadata/input:
+ summary: marimo-dataframe get_export_metadata input
+ get:
+ operationId: read_marimo_dataframe_get_export_metadata_input
+ summary: Read marimo-dataframe get_export_metadata input
+ tags:
+ - rpc-input
+ responses:
+ "200":
+ description: marimo-dataframe get_export_metadata input accepted by a plugin
+ consumer.
+ content:
+ application/json:
+ schema:
+ $ref: "#/components/schemas/marimo-dataframe.get_export_metadata.input"
+ put:
+ operationId: write_marimo_dataframe_get_export_metadata_input
+ summary: Write marimo-dataframe get_export_metadata input
+ tags:
+ - rpc-input
+ requestBody:
+ required: true
+ content:
+ application/json:
+ schema:
+ $ref: "#/components/schemas/marimo-dataframe.get_export_metadata.input"
+ responses:
+ "204":
+ description: Accepted.
+ /plugins/marimo-dataframe/functions/get_export_metadata/output:
+ summary: marimo-dataframe get_export_metadata output
+ get:
+ operationId: read_marimo_dataframe_get_export_metadata_output
+ summary: Read marimo-dataframe get_export_metadata output
+ tags:
+ - rpc-output
+ responses:
+ "200":
+ description: marimo-dataframe get_export_metadata output accepted by a plugin
+ consumer.
+ content:
+ application/json:
+ schema:
+ $ref: "#/components/schemas/marimo-dataframe.get_export_metadata.output"
+ put:
+ operationId: write_marimo_dataframe_get_export_metadata_output
+ summary: Write marimo-dataframe get_export_metadata output
+ tags:
+ - rpc-output
+ requestBody:
+ required: true
+ content:
+ application/json:
+ schema:
+ $ref: "#/components/schemas/marimo-dataframe.get_export_metadata.output"
+ responses:
+ "204":
+ description: Accepted.
/plugins/marimo-dataframe/functions/get_size_bytes/input:
summary: marimo-dataframe get_size_bytes input
get:
@@ -2325,6 +2383,63 @@ paths:
responses:
"204":
description: Accepted.
+ /plugins/marimo-table/functions/get_export_metadata/input:
+ summary: marimo-table get_export_metadata input
+ get:
+ operationId: read_marimo_table_get_export_metadata_input
+ summary: Read marimo-table get_export_metadata input
+ tags:
+ - rpc-input
+ responses:
+ "200":
+ description: marimo-table get_export_metadata input accepted by a plugin consumer.
+ content:
+ application/json:
+ schema:
+ $ref: "#/components/schemas/marimo-table.get_export_metadata.input"
+ put:
+ operationId: write_marimo_table_get_export_metadata_input
+ summary: Write marimo-table get_export_metadata input
+ tags:
+ - rpc-input
+ requestBody:
+ required: true
+ content:
+ application/json:
+ schema:
+ $ref: "#/components/schemas/marimo-table.get_export_metadata.input"
+ responses:
+ "204":
+ description: Accepted.
+ /plugins/marimo-table/functions/get_export_metadata/output:
+ summary: marimo-table get_export_metadata output
+ get:
+ operationId: read_marimo_table_get_export_metadata_output
+ summary: Read marimo-table get_export_metadata output
+ tags:
+ - rpc-output
+ responses:
+ "200":
+ description: marimo-table get_export_metadata output accepted by a plugin
+ consumer.
+ content:
+ application/json:
+ schema:
+ $ref: "#/components/schemas/marimo-table.get_export_metadata.output"
+ put:
+ operationId: write_marimo_table_get_export_metadata_output
+ summary: Write marimo-table get_export_metadata output
+ tags:
+ - rpc-output
+ requestBody:
+ required: true
+ content:
+ application/json:
+ schema:
+ $ref: "#/components/schemas/marimo-table.get_export_metadata.output"
+ responses:
+ "204":
+ description: Accepted.
/plugins/marimo-table/functions/get_row_ids/input:
summary: marimo-table get_row_ids input
get:
@@ -3150,6 +3265,10 @@ components:
- json
- parquet
- tsv
+ geometry_column:
+ type:
+ - string
+ - "null"
options:
type: object
properties:
@@ -3178,6 +3297,22 @@ components:
items:
type: string
- type: "null"
+ code:
+ anyOf:
+ - type: string
+ enum:
+ - geometry_required
+ - invalid_geometry
+ - invalid_metadata
+ - unsupported_representation
+ - unsupported_version
+ - missing_packages
+ - conversion_failed
+ - type: "null"
+ column:
+ type:
+ - string
+ - "null"
required:
- url
- filename
@@ -3306,6 +3441,73 @@ components:
- row_headers
- field_types
- column_types_per_step
+ marimo-dataframe.get_export_metadata.input:
+ type: object
+ properties: {}
+ marimo-dataframe.get_export_metadata.output:
+ type: object
+ properties:
+ geometry_columns:
+ type: array
+ items:
+ type: object
+ properties:
+ name:
+ type: string
+ encoding:
+ type: string
+ enum:
+ - objects
+ - wkb
+ - wkt
+ - other
+ crs:
+ anyOf:
+ - anyOf:
+ - type: string
+ - type: object
+ propertyNames:
+ type: string
+ additionalProperties: {}
+ - type: "null"
+ required:
+ - name
+ - encoding
+ - crs
+ primary_geometry_column:
+ type:
+ - string
+ - "null"
+ default_geometry_column:
+ type:
+ - string
+ - "null"
+ formats:
+ type: object
+ propertyNames:
+ type: string
+ additionalProperties:
+ type: object
+ properties:
+ available:
+ type: boolean
+ reason:
+ type:
+ - string
+ - "null"
+ missing_packages:
+ type: array
+ items:
+ type: string
+ required:
+ - available
+ - reason
+ - missing_packages
+ required:
+ - geometry_columns
+ - primary_geometry_column
+ - default_geometry_column
+ - formats
marimo-dataframe.get_size_bytes.input:
type: object
properties: {}
@@ -4641,6 +4843,10 @@ components:
- json
- parquet
- tsv
+ geometry_column:
+ type:
+ - string
+ - "null"
options:
type: object
properties:
@@ -4669,6 +4875,22 @@ components:
items:
type: string
- type: "null"
+ code:
+ anyOf:
+ - type: string
+ enum:
+ - geometry_required
+ - invalid_geometry
+ - invalid_metadata
+ - unsupported_representation
+ - unsupported_version
+ - missing_packages
+ - conversion_failed
+ - type: "null"
+ column:
+ type:
+ - string
+ - "null"
required:
- url
- filename
@@ -4853,6 +5075,73 @@ components:
required:
- data_url
- format
+ marimo-table.get_export_metadata.input:
+ type: object
+ properties: {}
+ marimo-table.get_export_metadata.output:
+ type: object
+ properties:
+ geometry_columns:
+ type: array
+ items:
+ type: object
+ properties:
+ name:
+ type: string
+ encoding:
+ type: string
+ enum:
+ - objects
+ - wkb
+ - wkt
+ - other
+ crs:
+ anyOf:
+ - anyOf:
+ - type: string
+ - type: object
+ propertyNames:
+ type: string
+ additionalProperties: {}
+ - type: "null"
+ required:
+ - name
+ - encoding
+ - crs
+ primary_geometry_column:
+ type:
+ - string
+ - "null"
+ default_geometry_column:
+ type:
+ - string
+ - "null"
+ formats:
+ type: object
+ propertyNames:
+ type: string
+ additionalProperties:
+ type: object
+ properties:
+ available:
+ type: boolean
+ reason:
+ type:
+ - string
+ - "null"
+ missing_packages:
+ type: array
+ items:
+ type: string
+ required:
+ - available
+ - reason
+ - missing_packages
+ required:
+ - geometry_columns
+ - primary_geometry_column
+ - default_geometry_column
+ - formats
marimo-table.get_row_ids.input:
type: object
properties: {}
diff --git a/frontend/src/components/data-table/TableTopBar.tsx b/frontend/src/components/data-table/TableTopBar.tsx
index 3aef5c03fc..1c3ce40c1b 100644
--- a/frontend/src/components/data-table/TableTopBar.tsx
+++ b/frontend/src/components/data-table/TableTopBar.tsx
@@ -54,6 +54,8 @@ export const TableTopBar = <TData,>({
togglePanel,
isAnyPanelOpen,
downloadAs,
+ getExportMetadata,
+ metadataSource,
sizeBytes,
sizeBytesIsLoading,
}: TableTopBarProps<TData>) => {
@@ -132,6 +134,8 @@ export const TableTopBar = <TData,>({
{downloadAs && (
<ExportActions
downloadAs={downloadAs}
+ getExportMetadata={getExportMetadata}
+ metadataSource={metadataSource}
sizeBytes={sizeBytes}
sizeBytesIsLoading={sizeBytesIsLoading}
/>
diff --git a/frontend/src/components/data-table/__tests__/export-actions.test.tsx b/frontend/src/components/data-table/__tests__/export-actions.test.tsx
index b2c8a15046..695dc2de36 100644
--- a/frontend/src/components/data-table/__tests__/export-actions.test.tsx
+++ b/frontend/src/components/data-table/__tests__/export-actions.test.tsx
@@ -18,6 +18,7 @@ import { downloadByURL } from "@/utils/download";
import { jsonToMarkdown } from "@/utils/json/json-parser";
import { downloadSizeLimitAtom } from "../download-policy/atoms";
import { ExportActions, type ExportActionProps } from "../export-actions";
+import type { ExportMetadata } from "../schemas";
const mocks = vi.hoisted(() => ({
handleInstallPackages: vi.fn(),
@@ -58,13 +59,19 @@ function renderExportActions(
store.set(downloadSizeLimitAtom, sizeLimit);
store.set(viewStateAtom, { mode, cellAnchor: null });
- return render(
+ const view = (nextProps: Partial<ExportActionProps>) => (
<Provider store={store}>
<TooltipProvider delayDuration={0}>
- <ExportActions downloadAs={downloadAs} {...props} />
+ <ExportActions downloadAs={downloadAs} {...nextProps} />
</TooltipProvider>
- </Provider>,
+ </Provider>
);
+ const result = render(view(props));
+ return {
+ ...result,
+ rerenderExportActions: (nextProps: Partial<ExportActionProps>) =>
+ result.rerender(view(nextProps)),
+ };
}
async function openDialog() {
@@ -72,6 +79,26 @@ async function openDialog() {
return screen.findByRole("dialog", { name: "Export table" });
}
+function selectGeometry(name: string) {
+ const select = screen.getByRole("combobox", { name: "Primary geometry" });
+ const option = within(select).getByRole("option", {
+ name,
+ }) as HTMLOptionElement;
+ fireEvent.change(select, { target: { value: option.value } });
+}
+
+const geometryMetadata: ExportMetadata = {
+ geometry_columns: [
+ { name: "location", encoding: "objects", crs: "EPSG:4326" },
+ { name: "boundary", encoding: "objects", crs: null },
+ ],
+ primary_geometry_column: "location",
+ default_geometry_column: "location",
+ formats: {
+ parquet: { available: true, reason: null, missing_packages: [] },
+ },
+};
+
describe("ExportActions dialog", () => {
beforeEach(() => {
vi.clearAllMocks();
@@ -85,6 +112,423 @@ describe("ExportActions dialog", () => {
vi.unstubAllGlobals();
});
+ it("selects a primary geometry for GeoParquet without changing other formats", async () => {
+ const getExportMetadata = vi.fn().mockResolvedValue(geometryMetadata);
+ renderExportActions({ getExportMetadata });
+ await openDialog();
+ await waitFor(() => {
+ expect(
+ screen.getByRole("button", { name: "GeoParquet options" }),
+ ).toBeEnabled();
+ });
+ expect(screen.getByTestId("export-summary-parquet")).toHaveTextContent(
+ "location",
+ );
+ fireEvent.click(screen.getByRole("button", { name: "GeoParquet options" }));
+ selectGeometry("boundary");
+ fireEvent.click(
+ screen.getByRole("button", { name: "Download GeoParquet" }),
+ );
+ await waitFor(() => {
+ expect(downloadByURL).toHaveBeenCalled();
+ });
+ expect(downloadAs).toHaveBeenCalledWith({
+ format: "parquet",
+ geometry_column: "boundary",
+ });
+ expect(getExportMetadata).toHaveBeenCalledWith({});
+ expect(screen.getByText("GeoParquet")).toBeInTheDocument();
+ expect(
+ screen.getByText("Parquet with geometry and CRS metadata"),
+ ).toBeInTheDocument();
+ expect(screen.getByTestId("export-summary-parquet")).toHaveTextContent(
+ "boundary",
+ );
+
+ fireEvent.click(screen.getByRole("button", { name: "Download CSV" }));
+ await waitFor(() => {
+ expect(downloadAs).toHaveBeenCalledWith({ format: "csv" });
+ });
+ });
+
+ it("requires a choice when several geometries have no primary", async () => {
+ renderExportActions({
+ getExportMetadata: vi.fn().mockResolvedValue({
+ ...geometryMetadata,
+ primary_geometry_column: null,
+ default_geometry_column: null,
+ }),
+ });
+ await openDialog();
+ await screen.findByRole("button", { name: "GeoParquet options" });
+ expect(
+ screen.getByRole("button", { name: "Download GeoParquet" }),
+ ).toBeDisabled();
+ expect(
+ screen.getByText(
+ "Choose a primary geometry column to export GeoParquet.",
+ ),
+ ).toBeInTheDocument();
+ fireEvent.click(screen.getByRole("button", { name: "GeoParquet options" }));
+ selectGeometry("boundary");
+ expect(
+ screen.getByRole("button", { name: "Download GeoParquet" }),
+ ).toBeEnabled();
+ });
+
+ it("shows ineligible geometry sources and keeps ordinary exports available", async () => {
+ renderExportActions({
+ getExportMetadata: vi.fn().mockResolvedValue({
+ ...geometryMetadata,
+ formats: {
+ parquet: {
+ available: false,
+ reason: "GeoParquet export from Arrow tables is not supported yet.",
+ missing_packages: [],
+ },
+ },
+ }),
+ });
+ await openDialog();
+ expect(
+ await screen.findByText(
+ "GeoParquet export from Arrow tables is not supported yet.",
+ ),
+ ).toBeInTheDocument();
+ expect(
+ screen.getByRole("button", { name: "Download GeoParquet" }),
+ ).toBeDisabled();
+ fireEvent.click(screen.getByRole("button", { name: "Download JSON" }));
+ await waitFor(() => {
+ expect(downloadAs).toHaveBeenCalledWith({ format: "json" });
+ });
+ });
+
+ it("keeps the geometry choice through a package install retry", async () => {
+ const getExportMetadata = vi.fn().mockResolvedValue(geometryMetadata);
+ downloadAs
+ .mockResolvedValueOnce({
+ url: "",
+ filename: "",
+ error: "GeoParquet export requires pyarrow.",
+ code: "missing_packages",
+ missing_packages: ["pyarrow"],
+ })
+ .mockResolvedValueOnce({
+ url: "https://example.test/export",
+ filename: "table",
+ });
+ renderExportActions({ getExportMetadata });
+ await openDialog();
+ await screen.findByRole("button", { name: "GeoParquet options" });
+ fireEvent.click(screen.getByRole("button", { name: "GeoParquet options" }));
+ selectGeometry("boundary");
+ fireEvent.click(
+ screen.getByRole("button", { name: "Download GeoParquet" }),
+ );
+ expect(
+ await screen.findByText("GeoParquet export requires pyarrow."),
+ ).toBeInTheDocument();
+ expect(screen.queryByRole("alert")).not.toBeInTheDocument();
+ expect(
+ screen.getByRole("button", { name: "Download GeoParquet" }),
+ ).toBeDisabled();
+ expect(downloadByURL).not.toHaveBeenCalled();
+ fireEvent.click(screen.getByRole("button", { name: "Install pyarrow" }));
+ expect(mocks.handleInstallPackages).toHaveBeenCalledWith(
+ ["pyarrow"],
+ expect.any(Function),
+ );
+ await act(async () => {
+ mocks.handleInstallPackages.mock.calls[0][1]();
+ });
+ await waitFor(() => {
+ expect(downloadByURL).toHaveBeenCalled();
+ });
+ expect(downloadAs).toHaveBeenLastCalledWith({
+ format: "parquet",
+ geometry_column: "boundary",
+ });
+ });
+
+ it("refreshes eligibility after package installation", async () => {
+ const getExportMetadata = vi.fn().mockResolvedValue({
+ ...geometryMetadata,
+ formats: {
+ parquet: {
+ available: false,
+ reason: "GeoParquet export requires pyarrow.",
+ missing_packages: ["pyarrow"],
+ },
+ },
+ });
+ renderExportActions({ getExportMetadata });
+ await openDialog();
+ await screen.findByRole("button", { name: "GeoParquet options" });
+ expect(
+ screen.getByRole("button", { name: "Download GeoParquet" }),
+ ).toBeDisabled();
+ expect(
+ screen.getAllByText("GeoParquet export requires pyarrow."),
+ ).toHaveLength(1);
+ expect(screen.queryByRole("alert")).not.toBeInTheDocument();
+ expect(downloadAs).not.toHaveBeenCalled();
+ fireEvent.click(
+ await screen.findByRole("button", { name: "Install pyarrow" }),
+ );
+ getExportMetadata.mockResolvedValue(geometryMetadata);
+ await act(async () => {
+ mocks.handleInstallPackages.mock.calls[0][1]();
+ });
+ await waitFor(() => {
+ expect(downloadByURL).toHaveBeenCalled();
+ });
+ expect(getExportMetadata).toHaveBeenCalledTimes(2);
+ expect(
+ screen.queryByText("GeoParquet export requires pyarrow."),
+ ).not.toBeInTheDocument();
+ });
+
+ it("keeps the install prompt when the package is still missing", async () => {
+ const getExportMetadata = vi.fn().mockResolvedValue({
+ ...geometryMetadata,
+ formats: {
+ parquet: {
+ available: false,
+ reason: "GeoParquet export requires pyarrow.",
+ missing_packages: ["pyarrow"],
+ },
+ },
+ });
+ renderExportActions({ getExportMetadata });
+ await openDialog();
+ fireEvent.click(
+ await screen.findByRole("button", { name: "Install pyarrow" }),
+ );
+ await act(async () => {
+ mocks.handleInstallPackages.mock.calls[0][1]();
+ });
+ await waitFor(() => {
+ expect(getExportMetadata).toHaveBeenCalledTimes(2);
+ });
+ expect(
+ screen.getByRole("button", { name: "Install pyarrow" }),
+ ).toBeInTheDocument();
+ expect(
+ screen.getByRole("button", { name: "Download GeoParquet" }),
+ ).toBeDisabled();
+ expect(downloadAs).not.toHaveBeenCalled();
+ });
+
+ it("can select a geometry column with an empty name", async () => {
+ renderExportActions({
+ getExportMetadata: vi.fn().mockResolvedValue({
+ ...geometryMetadata,
+ geometry_columns: [
+ geometryMetadata.geometry_columns[0],
+ { name: "", encoding: "objects", crs: null },
+ ],
+ }),
+ });
+ await openDialog();
+ fireEvent.click(
+ await screen.findByRole("button", { name: "GeoParquet options" }),
+ );
+ selectGeometry("(unnamed geometry)");
+ fireEvent.click(
+ screen.getByRole("button", { name: "Download GeoParquet" }),
+ );
+ await waitFor(() => {
+ expect(downloadAs).toHaveBeenCalledWith({
+ format: "parquet",
+ geometry_column: "",
+ });
+ });
+ });
+
+ it("refreshes geometry metadata when the source changes", async () => {
+ const first = vi.fn().mockResolvedValue(geometryMetadata);
+ const second = vi.fn().mockResolvedValue({
+ ...geometryMetadata,
+ geometry_columns: [
+ { name: "region", encoding: "objects", crs: "EPSG:3857" },
+ ],
+ primary_geometry_column: "region",
+ default_geometry_column: "region",
+ });
+ const { rerenderExportActions } = renderExportActions({
+ getExportMetadata: first,
+ metadataSource: "first",
+ });
+ await openDialog();
+ await screen.findByRole("button", { name: "GeoParquet options" });
+ fireEvent.click(screen.getByRole("button", { name: "GeoParquet options" }));
+ selectGeometry("boundary");
+
+ rerenderExportActions({
+ getExportMetadata: second,
+ metadataSource: "second",
+ });
+ await waitFor(() => {
+ expect(screen.getByTestId("export-summary-parquet")).toHaveTextContent(
+ "region",
+ );
+ });
+ expect(
+ screen.getByRole("combobox", { name: "Primary geometry" }),
+ ).toHaveDisplayValue("region");
+ fireEvent.click(
+ screen.getByRole("button", { name: "Download GeoParquet" }),
+ );
+ await waitFor(() => {
+ expect(downloadAs).toHaveBeenCalledWith({
+ format: "parquet",
+ geometry_column: "region",
+ });
+ });
+ expect(first).toHaveBeenCalledTimes(1);
+ expect(second).toHaveBeenCalledTimes(1);
+ });
+
+ it("keeps a still-valid geometry override when metadata refreshes", async () => {
+ const getExportMetadata = vi.fn().mockResolvedValue(geometryMetadata);
+ const { rerenderExportActions } = renderExportActions({
+ getExportMetadata,
+ metadataSource: "first",
+ });
+ await openDialog();
+ fireEvent.click(
+ await screen.findByRole("button", { name: "GeoParquet options" }),
+ );
+ selectGeometry("boundary");
+ rerenderExportActions({ getExportMetadata, metadataSource: "second" });
+ await waitFor(() => {
+ expect(getExportMetadata).toHaveBeenCalledTimes(2);
+ });
+ expect(
+ screen.getByRole("combobox", { name: "Primary geometry" }),
+ ).toHaveDisplayValue("boundary");
+ });
+
+ it("does not publish an artifact for a structured failure without an error message", async () => {
+ downloadAs.mockResolvedValueOnce({
+ url: "",
+ filename: "",
+ code: "invalid_geometry",
+ column: "location",
+ });
+ renderExportActions({
+ getExportMetadata: vi.fn().mockResolvedValue(geometryMetadata),
+ });
+ await openDialog();
+ await screen.findByRole("button", { name: "GeoParquet options" });
+ fireEvent.click(
+ screen.getByRole("button", { name: "Download GeoParquet" }),
+ );
+ expect(
+ await screen.findByText("The export did not produce a file."),
+ ).toBeInTheDocument();
+ expect(downloadByURL).not.toHaveBeenCalled();
+ });
+
+ it("retries a failed metadata lookup without blocking ordinary formats", async () => {
+ const getExportMetadata = vi
+ .fn()
+ .mockRejectedValueOnce(new Error("Connection lost"))
+ .mockResolvedValueOnce(geometryMetadata);
+ renderExportActions({ getExportMetadata });
+ await openDialog();
+ expect(
+ await screen.findByText(
+ "Could not load geometry options: Connection lost",
+ ),
+ ).toBeInTheDocument();
+ expect(
+ screen.getByRole("button", { name: "Download Parquet" }),
+ ).toBeDisabled();
+ fireEvent.click(screen.getByRole("button", { name: "Download CSV" }));
+ await waitFor(() => {
+ expect(downloadAs).toHaveBeenCalledWith({ format: "csv" });
+ });
+ fireEvent.click(screen.getByRole("button", { name: "Retry" }));
+ expect(
+ await screen.findByRole("button", { name: "GeoParquet options" }),
+ ).toBeInTheDocument();
+ expect(
+ screen.getByRole("button", { name: "Download GeoParquet" }),
+ ).toBeEnabled();
+ expect(getExportMetadata).toHaveBeenCalledTimes(2);
+ });
+
+ it("does not cancel a pending CSV download when geometry metadata is retried", async () => {
+ let resolveDownload: (value: {
+ url: string;
+ filename: string;
+ }) => void = () => undefined;
+ downloadAs.mockImplementationOnce(
+ () =>
+ new Promise((resolve) => {
+ resolveDownload = resolve;
+ }),
+ );
+ const getExportMetadata = vi
+ .fn()
+ .mockRejectedValueOnce(new Error("Connection lost"))
+ .mockResolvedValueOnce(geometryMetadata);
+ renderExportActions({ getExportMetadata });
+ await openDialog();
+ await screen.findByText("Could not load geometry options: Connection lost");
+
+ fireEvent.click(screen.getByRole("button", { name: "Download CSV" }));
+ fireEvent.click(screen.getByRole("button", { name: "Retry" }));
+ await screen.findByRole("button", { name: "GeoParquet options" });
+ await act(async () => {
+ resolveDownload({
+ url: "https://example.test/export",
+ filename: "table",
+ });
+ });
+
+ expect(downloadByURL).toHaveBeenCalledWith(
+ expect.stringContaining("https://example.test/export"),
+ "table.csv",
+ );
+ });
+
+ it("cancels a pending download when the table source changes", async () => {
+ let resolveDownload: (value: {
+ url: string;
+ filename: string;
+ }) => void = () => undefined;
+ downloadAs.mockImplementationOnce(
+ () =>
+ new Promise((resolve) => {
+ resolveDownload = resolve;
+ }),
+ );
+ const getExportMetadata = vi.fn().mockResolvedValue(geometryMetadata);
+ const { rerenderExportActions } = renderExportActions({
+ getExportMetadata,
+ metadataSource: "first",
+ });
+ await openDialog();
+ await screen.findByRole("button", { name: "GeoParquet options" });
+
+ fireEvent.click(screen.getByRole("button", { name: "Download CSV" }));
+ rerenderExportActions({ getExportMetadata, metadataSource: "second" });
+ await waitFor(() => {
+ expect(getExportMetadata).toHaveBeenCalledTimes(2);
+ });
+ await act(async () => {
+ resolveDownload({
+ url: "https://example.test/export",
+ filename: "table",
+ });
+ });
+
+ expect(downloadByURL).not.toHaveBeenCalled();
+ });
+
it("shows formats in the approved order with an options toggle where supported", async () => {
renderExportActions();
const dialog = await openDialog();
@@ -98,6 +542,11 @@ describe("ExportActions dialog", () => {
"export-row-parquet",
"export-row-markdown",
]);
+ for (const format of ["csv", "tsv", "json", "parquet", "markdown"]) {
+ expect(
+ screen.getByTestId(`export-format-icon-${format}`),
+ ).toBeInTheDocument();
+ }
for (const label of ["CSV", "TSV", "JSON"]) {
expect(
screen.getByRole("button", { name: `${label} options` }),
@@ -484,11 +933,14 @@ describe("ExportActions dialog", () => {
fireEvent.click(screen.getByRole("button", { name: "Download Parquet" }));
- const alert = await screen.findByRole("alert");
- expect(alert).toHaveTextContent("Parquet export requires pyarrow.");
- fireEvent.click(
- within(alert).getByRole("button", { name: "Install pyarrow" }),
- );
+ expect(
+ await screen.findByText("Parquet export requires pyarrow."),
+ ).toBeInTheDocument();
+ expect(screen.queryByRole("alert")).not.toBeInTheDocument();
+ expect(
+ screen.getByRole("button", { name: "Download Parquet" }),
+ ).toBeDisabled();
+ fireEvent.click(screen.getByRole("button", { name: "Install pyarrow" }));
expect(mocks.handleInstallPackages).toHaveBeenCalledWith(
["pyarrow"],
expect.any(Function),
@@ -520,12 +972,16 @@ describe("ExportActions dialog", () => {
fireEvent.click(screen.getByRole("button", { name: "Download Parquet" }));
- const alert = await screen.findByRole("alert");
- expect(alert).toHaveTextContent(
- "Parquet export isn't available in this notebook",
- );
- expect(alert).not.toHaveTextContent("pyarrow");
- expect(within(alert).queryByRole("button")).not.toBeInTheDocument();
+ expect(
+ await screen.findByText(
+ "Parquet export isn't available in this notebook",
+ ),
+ ).toBeInTheDocument();
+ expect(screen.queryByText("pyarrow")).not.toBeInTheDocument();
+ expect(
+ screen.queryByRole("button", { name: /Install/ }),
+ ).not.toBeInTheDocument();
+ expect(screen.queryByRole("alert")).not.toBeInTheDocument();
});
it("clears stale failures before a new action and after close", async () => {
diff --git a/frontend/src/components/data-table/data-table.tsx b/frontend/src/components/data-table/data-table.tsx
index be6345c862..c516c897fd 100644
--- a/frontend/src/components/data-table/data-table.tsx
+++ b/frontend/src/components/data-table/data-table.tsx
@@ -155,6 +155,7 @@ const DataTableInternal = <TData,>({
paginationState,
setPaginationState,
downloadAs,
+ getExportMetadata,
manualPagination = false,
pagination = false,
onRowSelectionChange,
@@ -376,6 +377,8 @@ const DataTableInternal = <TData,>({
togglePanel={togglePanel}
isAnyPanelOpen={isAnyPanelOpen}
downloadAs={downloadAs}
+ getExportMetadata={getExportMetadata}
+ metadataSource={data}
sizeBytes={sizeBytes}
sizeBytesIsLoading={sizeBytesIsLoading}
/>
diff --git a/frontend/src/components/data-table/export-actions.tsx b/frontend/src/components/data-table/export-actions.tsx
index 89cdf961d9..5047d30711 100644
--- a/frontend/src/components/data-table/export-actions.tsx
+++ b/frontend/src/components/data-table/export-actions.tsx
@@ -3,10 +3,14 @@
import { useAtomValue } from "jotai";
import {
AlertCircleIcon,
+ BracesIcon,
+ BrickWallIcon,
ChevronRightIcon,
ClipboardCopyIcon,
DownloadIcon,
+ FileTextIcon,
InfoIcon,
+ TableIcon,
} from "lucide-react";
import React from "react";
import { downloadSizeLimitAtom } from "./download-policy/atoms";
@@ -14,6 +18,8 @@ import type {
DownloadAsArgs,
DownloadAsOptions,
DownloadFormat,
+ ExportMetadata,
+ GetExportMetadata,
} from "./schemas";
import { logNever } from "@/utils/assertNever";
import { cn } from "@/utils/cn";
@@ -47,6 +53,7 @@ const EXPORT_OPTIONS = [
label: "CSV",
format: "csv",
description: "Comma-separated values",
+ icon: TableIcon,
canDownload: true,
canCopy: true,
},
@@ -54,6 +61,7 @@ const EXPORT_OPTIONS = [
label: "TSV",
format: "tsv",
description: "Best for Excel and Google Sheets",
+ icon: TableIcon,
canDownload: true,
canCopy: true,
},
@@ -61,6 +69,7 @@ const EXPORT_OPTIONS = [
label: "JSON",
format: "json",
description: "Raw JSON data",
+ icon: BracesIcon,
canDownload: true,
canCopy: true,
},
@@ -68,6 +77,7 @@ const EXPORT_OPTIONS = [
label: "Parquet",
format: "parquet",
description: "Columnar binary format",
+ icon: BrickWallIcon,
canDownload: true,
canCopy: false,
},
@@ -75,6 +85,7 @@ const EXPORT_OPTIONS = [
label: "Markdown",
format: "markdown",
description: "Preserves hyperlinks and formatting",
+ icon: FileTextIcon,
canDownload: false,
canCopy: true,
},
@@ -95,10 +106,8 @@ type ExportFailure =
}
| {
kind: "missing-packages";
- title: string;
description?: string | null;
packages: string[];
- featureName: string;
action: ExportAction;
};
@@ -140,7 +149,7 @@ const DEFAULT_SETTINGS: ExportSettings = {
type ConfigurableFormat = keyof ExportSettings;
-const isConfigurable = (format: ExportFormat): format is ConfigurableFormat =>
+const isConfigurable = (format: ExportFormat): format is keyof ExportSettings =>
format in DEFAULT_SETTINGS;
/**
@@ -222,6 +231,8 @@ function settingsSummary(
export interface ExportActionProps {
downloadAs: DownloadAsArgs;
+ getExportMetadata?: GetExportMetadata;
+ metadataSource?: unknown;
// JSON-serialized size of the currently-rendered data. Used together with
// downloadSizeLimitAtom to disable the Export button when a host (e.g.,
// marimo-lsp inside VS Code) declares a download size cap. Null/undefined
@@ -230,8 +241,11 @@ export interface ExportActionProps {
sizeBytesIsLoading?: boolean;
}
-const labelForFormat = (format: ExportFormat): string =>
- EXPORT_OPTIONS.find((option) => option.format === format)?.label ?? format;
+const labelForFormat = (format: ExportFormat, hasGeometry = false): string =>
+ format === "parquet" && hasGeometry
+ ? "GeoParquet"
+ : (EXPORT_OPTIONS.find((option) => option.format === format)?.label ??
+ format);
const failureTitleForAction = (action: ExportAction): string =>
action.destination === "download"
@@ -242,15 +256,62 @@ const failureDescription = (error: unknown): string =>
typeof error === "string" ? error : prettyError(error);
export const ExportActions: React.FC<ExportActionProps> = (props) => {
+ const { getExportMetadata, metadataSource } = props;
const [exportDialogOpen, setExportDialogOpen] = React.useState(false);
const [failure, setFailure] = React.useState<ExportFailure | null>(null);
const [settings, setSettings] =
React.useState<ExportSettings>(DEFAULT_SETTINGS);
- const [expandedFormat, setExpandedFormat] =
- React.useState<ConfigurableFormat | null>(null);
+ const [metadata, setMetadata] = React.useState<ExportMetadata | null>(null);
+ const [metadataLoading, setMetadataLoading] = React.useState(false);
+ const [metadataError, setMetadataError] = React.useState<string | null>(null);
+ const [metadataRefresh, setMetadataRefresh] = React.useState(0);
+ const [geometryColumn, setGeometryColumn] = React.useState<string | null>(
+ null,
+ );
+ const hasGeometry = !!metadata?.geometry_columns.length;
+ const [expandedFormat, setExpandedFormat] = React.useState<
+ ConfigurableFormat | "parquet" | null
+ >(null);
const triggerRef = React.useRef<HTMLButtonElement>(null);
const dialogRef = React.useRef<HTMLDivElement>(null);
const latestActionId = React.useRef(0);
+ React.useEffect(() => {
+ latestActionId.current += 1;
+ }, [metadataSource]);
+ React.useEffect(() => {
+ if (!exportDialogOpen || !getExportMetadata) {
+ return;
+ }
+ let active = true;
+ setMetadata(null);
+ setMetadataLoading(true);
+ setMetadataError(null);
+ void getExportMetadata({}).then(
+ (result) => {
+ if (!active) {
+ return;
+ }
+ setMetadata(result);
+ setGeometryColumn((current) =>
+ current !== null &&
+ result.geometry_columns.some((column) => column.name === current)
+ ? current
+ : result.default_geometry_column,
+ );
+ setMetadataLoading(false);
+ },
+ (error: unknown) => {
+ if (!active) {
+ return;
+ }
+ setMetadataError(failureDescription(error));
+ setMetadataLoading(false);
+ },
+ );
+ return () => {
+ active = false;
+ };
+ }, [exportDialogOpen, getExportMetadata, metadataSource, metadataRefresh]);
const policy = useAtomValue(downloadSizeLimitAtom);
const overLimit = !!(
policy &&
@@ -288,6 +349,9 @@ export const ExportActions: React.FC<ExportActionProps> = (props) => {
setFailure(null);
setExpandedFormat(null);
setSettings(DEFAULT_SETTINGS);
+ setMetadata(null);
+ setMetadataError(null);
+ setGeometryColumn(null);
}
};
@@ -313,27 +377,33 @@ export const ExportActions: React.FC<ExportActionProps> = (props) => {
filename: string;
} | null> => {
const options = requestOptions(settings, format, action.destination);
- const response = await props.downloadAs(
- options ? { format, options } : { format },
- );
+ const request = {
+ format,
+ ...(options ? { options } : {}),
+ ...(format === "parquet" && geometryColumn !== null
+ ? { geometry_column: geometryColumn }
+ : {}),
+ };
+ const response = await props.downloadAs(request);
+ if (actionId !== latestActionId.current) {
+ return null;
+ }
if (response.missing_packages && response.missing_packages.length > 0) {
setActionFailure(actionId, {
kind: "missing-packages",
- title: "Export failed",
packages: response.missing_packages,
- featureName: `${labelForFormat(action.format)} export`,
description: response.error,
action,
});
return null;
}
- if (response.error) {
+ if (response.error || response.code || !response.url) {
setActionFailure(actionId, {
kind: "error",
title: failureTitleForAction(action),
- description: response.error,
+ description: response.error ?? "The export did not produce a file.",
action,
});
return null;
@@ -348,7 +418,7 @@ export const ExportActions: React.FC<ExportActionProps> = (props) => {
const handleDownload = async (format: DownloadFormat) => {
const action: ExportAction = { destination: "download", format };
const actionId = beginAction();
- const label = labelForFormat(format);
+ const label = labelForFormat(format, hasGeometry);
try {
const ok = await withLoadingToast(
`Preparing ${label} export...`,
@@ -357,6 +427,9 @@ export const ExportActions: React.FC<ExportActionProps> = (props) => {
if (!result) {
return false;
}
+ if (actionId !== latestActionId.current) {
+ return false;
+ }
const rawName = (result.filename ?? "").trim();
const baseName = Filenames.withoutExtension(rawName) || "download";
const downloadName = `${baseName}.${format}`;
@@ -396,7 +469,7 @@ export const ExportActions: React.FC<ExportActionProps> = (props) => {
actionId: number,
) => {
await withLoadingToast(
- `Preparing ${labelForFormat(format)} for clipboard...`,
+ `Preparing ${labelForFormat(format, hasGeometry)} for clipboard...`,
async () => {
const sourceFormat = COPY_SOURCE_FORMAT[format];
const result = await resolveDownloadUrl(sourceFormat, action, actionId);
@@ -457,8 +530,55 @@ export const ExportActions: React.FC<ExportActionProps> = (props) => {
}
};
+ const retryAfterInstall = async (action: ExportAction) => {
+ if (action.format === "parquet" && getExportMetadata) {
+ const actionId = latestActionId.current;
+ let result: ExportMetadata;
+ try {
+ result = await getExportMetadata({});
+ } catch (error) {
+ if (actionId === latestActionId.current) {
+ setMetadataError(failureDescription(error));
+ }
+ return;
+ }
+ if (actionId !== latestActionId.current) {
+ return;
+ }
+ setMetadata(result);
+ setMetadataError(null);
+ if (result.formats.parquet?.available === false) {
+ setFailure(null);
+ return;
+ }
+ if (
+ geometryColumn !== null &&
+ !result.geometry_columns.some(
+ (column) => column.name === geometryColumn,
+ )
+ ) {
+ setGeometryColumn(result.default_geometry_column);
+ setActionFailure(actionId, {
+ kind: "error",
+ title: failureTitleForAction(action),
+ description:
+ "The selected geometry is no longer available. Choose another geometry to export.",
+ action,
+ });
+ return;
+ }
+ if (result.geometry_columns.length > 0 && geometryColumn === null) {
+ return;
+ }
+ }
+ retryAction(action);
+ };
+
const toggleExpanded = (format: ExportFormat) => {
- if (!isConfigurable(format)) {
+ if (
+ !isConfigurable(format) &&
+ !(format === "parquet" && metadata?.geometry_columns.length)
+ ) {
return;
}
setExpandedFormat((current) => (current === format ? null : format));
@@ -505,29 +625,61 @@ export const ExportActions: React.FC<ExportActionProps> = (props) => {
className="list-none overflow-hidden rounded-md border divide-y"
>
{EXPORT_OPTIONS.map((option) => {
- const configurableFormat = isConfigurable(option.format)
- ? option.format
- : null;
+ const optionLabel = labelForFormat(option.format, hasGeometry);
+ const configurableFormat =
+ isConfigurable(option.format) ||
+ (option.format === "parquet" &&
+ !!metadata?.geometry_columns.length)
+ ? option.format
+ : null;
const expanded =
configurableFormat !== null &&
expandedFormat === configurableFormat;
const panelId = `export-options-${option.format}`;
const rowFailure =
failure?.action.format === option.format ? failure : null;
- const summary = settingsSummary(settings, option.format);
+ const summary =
+ option.format === "parquet" && metadata?.geometry_columns.length
+ ? geometryColumn === ""
+ ? "(unnamed geometry)"
+ : (geometryColumn ?? "Choose geometry")
+ : settingsSummary(settings, option.format);
+ const selectedGeometryIndex =
+ metadata?.geometry_columns.findIndex(
+ (column) => column.name === geometryColumn,
+ ) ?? -1;
+ const parquetEligibility =
+ option.format === "parquet"
+ ? metadata?.formats.parquet
+ : undefined;
+ const missingPackageFailure =
+ rowFailure?.kind === "missing-packages" ? rowFailure : null;
+ const missingPackages =
+ missingPackageFailure?.packages ??
+ (parquetEligibility?.available === false
+ ? parquetEligibility.missing_packages
+ : []);
+ const parquetUnavailable =
+ option.format === "parquet" &&
+ ((!!getExportMetadata && !metadata) ||
+ metadataLoading ||
+ !!metadataError ||
+ parquetEligibility?.available === false ||
+ (!!metadata?.geometry_columns.length &&
+ geometryColumn === null));
return (
<li
key={option.format}
data-testid={`export-row-${option.format}`}
className={cn(expanded && "bg-muted/40")}
>
- <div className="grid min-h-[60px] grid-cols-[28px_1fr_auto] items-center gap-x-1 px-2.5 py-2 hover:bg-accent/50">
+ <div className="grid min-h-[60px] grid-cols-[28px_minmax(0,1fr)_auto] items-center gap-x-1 px-2.5 py-2 hover:bg-accent/50">
{configurableFormat ? (
<Button
type="button"
variant="ghost"
size="icon"
- aria-label={`${option.label} options`}
+ aria-label={`${optionLabel} options`}
aria-expanded={expanded}
aria-controls={panelId}
onClick={() => toggleExpanded(option.format)}
@@ -542,30 +694,39 @@ export const ExportActions: React.FC<ExportActionProps> = (props) => {
) : (
<span aria-hidden={true} className="h-6 w-6" />
)}
- <div className="grid gap-1 pl-1">
- <span className="flex items-baseline gap-x-2">
- <span className="text-sm font-medium leading-tight">
- {option.label}
- </span>
- {summary && (
- <span
- data-testid={`export-summary-${option.format}`}
- className="text-xs text-muted-foreground"
- >
- {summary}
+ <div className="flex min-w-0 items-center gap-3 pl-1">
+ <option.icon
+ aria-hidden={true}
+ data-testid={`export-format-icon-${option.format}`}
+ className="h-4 w-4 shrink-0 text-muted-foreground"
+ />
+ <div className="grid min-w-0 gap-1">
+ <span className="flex min-w-0 items-baseline gap-x-2">
+ <span className="text-sm font-medium leading-tight">
+ {optionLabel}
</span>
- )}
- </span>
- <span className="text-xs text-muted-foreground leading-tight">
- {option.description}
- </span>
+ {summary && (
+ <span
+ data-testid={`export-summary-${option.format}`}
+ className="min-w-0 truncate text-xs text-muted-foreground"
+ >
+ {summary}
+ </span>
+ )}
+ </span>
+ <span className="text-xs text-muted-foreground leading-tight">
+ {option.format === "parquet" && hasGeometry
+ ? "Parquet with geometry and CRS metadata"
+ : option.description}
+ </span>
+ </div>
</div>
<div className="flex items-center gap-0.5">
<Tooltip
content={
option.canDownload
- ? `Download ${option.label}`
- : `${option.label} is available for copy only.`
+ ? `Download ${optionLabel}`
+ : `${optionLabel} is available for copy only.`
}
>
<span className="inline-flex">
@@ -573,10 +734,16 @@ export const ExportActions: React.FC<ExportActionProps> = (props) => {
type="button"
variant="ghost"
size="icon"
- aria-label={`Download ${option.label}`}
- disabled={!option.canDownload}
+ aria-label={`Download ${optionLabel}`}
+ disabled={
+ !option.canDownload ||
+ parquetUnavailable ||
+ missingPackages.length > 0
+ }
onClick={
- option.canDownload
+ option.canDownload &&
+ !parquetUnavailable &&
+ missingPackages.length === 0
? () => {
void handleDownload(option.format);
}
@@ -590,8 +757,8 @@ export const ExportActions: React.FC<ExportActionProps> = (props) => {
<Tooltip
content={
option.canCopy
- ? `Copy ${option.label}`
- : `${option.label} is available for download only.`
+ ? `Copy ${optionLabel}`
+ : `${optionLabel} is available for download only.`
}
>
<span className="inline-flex">
@@ -599,7 +766,7 @@ export const ExportActions: React.FC<ExportActionProps> = (props) => {
type="button"
variant="ghost"
size="icon"
- aria-label={`Copy ${option.label}`}
+ aria-label={`Copy ${optionLabel}`}
disabled={!option.canCopy}
onClick={
option.canCopy
@@ -620,42 +787,110 @@ export const ExportActions: React.FC<ExportActionProps> = (props) => {
id={panelId}
className="grid grid-cols-2 gap-3 border-t bg-muted/50 px-3.5 py-3.5 pl-[52px]"
>
- <legend className="sr-only">{option.label} options</legend>
- <ExportSettingsFields
- format={configurableFormat}
- settings={settings}
- onChange={setSettings}
- />
+ <legend className="sr-only">{optionLabel} options</legend>
+ {configurableFormat === "parquet" ? (
+ <SettingField
+ id="export-primary-geometry"
+ label="Primary geometry"
+ help="Geometry column to use as the primary geometry in the GeoParquet file."
+ >
+ <NativeSelect
+ id="export-primary-geometry"
+ className="mb-0 w-full"
+ value={String(selectedGeometryIndex)}
+ onChange={(event) => {
+ const index = Number(event.target.value);
+ setGeometryColumn(
+ metadata?.geometry_columns[index]?.name ?? null,
+ );
+ }}
+ >
+ <option value="-1" disabled={true}>
+ Choose geometry
+ </option>
+ {metadata?.geometry_columns.map((column, index) => (
+ <option key={column.name} value={String(index)}>
+ {column.name || "(unnamed geometry)"}
+ </option>
+ ))}
+ </NativeSelect>
+ </SettingField>
+ ) : (
+ <ExportSettingsFields
+ format={configurableFormat}
+ settings={settings}
+ onChange={setSettings}
+ />
+ )}
</fieldset>
)}
- {rowFailure && (
+ {option.format === "parquet" &&
+ missingPackages.length === 0 &&
+ (parquetEligibility?.reason ||
+ metadataError ||
+ (!!metadata?.geometry_columns.length &&
+ geometryColumn === null)) && (
+ <div className="flex items-center justify-between gap-2 border-t px-3 py-2 pl-[42px] text-xs text-muted-foreground">
+ <span>
+ {metadataError
+ ? `Could not load geometry options: ${metadataError}`
+ : (parquetEligibility?.reason ??
+ "Choose a primary geometry column to export GeoParquet.")}
+ </span>
+ {metadataError && (
+ <Button
+ type="button"
+ size="xs"
+ variant="outline"
+ onClick={() =>
+ setMetadataRefresh((current) => current + 1)
+ }
+ >
+ Retry
+ </Button>
+ )}
+ </div>
+ )}
+ {missingPackages.length > 0 && (
<div className="border-t px-3 py-2 pl-[42px]">
- <Alert
- variant="destructive"
- className="p-3 has-[svg]:pl-9 [&>svg]:left-3 [&>svg]:top-3"
- >
- <AlertCircleIcon className="h-4 w-4" />
- <div>
- <AlertTitle className="text-sm">
- {rowFailure.title}
- </AlertTitle>
- <AlertDescription className="text-xs">
- {rowFailure.kind === "missing-packages" ? (
- <MissingPackagePrompt
- packages={rowFailure.packages}
- featureName={rowFailure.featureName}
- description={rowFailure.description}
- onInstall={() => retryAction(rowFailure.action)}
- className="items-start"
- />
- ) : (
- rowFailure.description
- )}
- </AlertDescription>
- </div>
- </Alert>
+ <MissingPackagePrompt
+ packages={missingPackages}
+ featureName={`${optionLabel} export`}
+ description={
+ missingPackageFailure?.description ??
+ parquetEligibility?.reason
+ }
+ onInstall={() => {
+ void retryAfterInstall(
+ missingPackageFailure?.action ?? {
+ destination: "download",
+ format: "parquet",
+ },
+ );
+ }}
+ className="items-start"
+ />
</div>
)}
+ {rowFailure?.kind === "error" &&
+ missingPackages.length === 0 && (
+ <div className="border-t px-3 py-2 pl-[42px]">
+ <Alert
+ variant="destructive"
+ className="p-3 has-[svg]:pl-9 [&>svg]:left-3 [&>svg]:top-3"
+ >
+ <AlertCircleIcon className="h-4 w-4" />
+ <div>
+ <AlertTitle className="text-sm">
+ {rowFailure.title}
+ </AlertTitle>
+ <AlertDescription className="text-xs">
+ {rowFailure.description}
+ </AlertDescription>
+ </div>
+ </Alert>
+ </div>
+ )}
</li>
);
})}
diff --git a/frontend/src/components/data-table/schemas.ts b/frontend/src/components/data-table/schemas.ts
index e16b6ed187..99ef318488 100644
--- a/frontend/src/components/data-table/schemas.ts
+++ b/frontend/src/components/data-table/schemas.ts
@@ -18,6 +18,7 @@ export interface DownloadAsOptions {
export interface DownloadAsRequest {
format: DownloadFormat;
options?: DownloadAsOptions;
+ geometry_column?: string | null;
}
export type DownloadAsArgs = (req: DownloadAsRequest) => Promise<{
@@ -25,12 +26,23 @@ export type DownloadAsArgs = (req: DownloadAsRequest) => Promise<{
filename: string;
error?: string | null;
missing_packages?: string[] | null;
+ code?:
+ | "geometry_required"
+ | "invalid_geometry"
+ | "invalid_metadata"
+ | "unsupported_representation"
+ | "unsupported_version"
+ | "missing_packages"
+ | "conversion_failed"
+ | null;
+ column?: string | null;
}>;
export const DownloadAsSchema = rpc
.input(
z.object({
format: z.enum(["csv", "json", "parquet", "tsv"]),
+ geometry_column: z.string().nullish(),
options: z
.object({
separator: z.string().optional(),
@@ -46,5 +58,46 @@ export const DownloadAsSchema = rpc
filename: z.string(),
error: z.string().nullish(),
missing_packages: z.array(z.string()).nullish(),
+ code: z
+ .enum([
+ "geometry_required",
+ "invalid_geometry",
+ "invalid_metadata",
+ "unsupported_representation",
+ "unsupported_version",
+ "missing_packages",
+ "conversion_failed",
+ ])
+ .nullish(),
+ column: z.string().nullish(),
}),
);
+
+const ExportMetadataSchema = z.object({
+ geometry_columns: z.array(
+ z.object({
+ name: z.string(),
+ encoding: z.enum(["objects", "wkb", "wkt", "other"]),
+ crs: z.union([z.string(), z.record(z.string(), z.unknown())]).nullable(),
+ }),
+ ),
+ primary_geometry_column: z.string().nullable(),
+ default_geometry_column: z.string().nullable(),
+ formats: z.record(
+ z.string(),
+ z.object({
+ available: z.boolean(),
+ reason: z.string().nullable(),
+ missing_packages: z.array(z.string()),
+ }),
+ ),
+});
+
+export type ExportMetadata = z.infer<typeof ExportMetadataSchema>;
+export type GetExportMetadata = (
+ opts: Record<string, never>,
+) => Promise<ExportMetadata>;
+
+export const GetExportMetadataSchema = rpc
+ .input(z.object({}))
+ .output(ExportMetadataSchema);
diff --git a/frontend/src/plugins/impl/DataTablePlugin.tsx b/frontend/src/plugins/impl/DataTablePlugin.tsx
index 8d12ae9601..c21dcab556 100644
--- a/frontend/src/plugins/impl/DataTablePlugin.tsx
+++ b/frontend/src/plugins/impl/DataTablePlugin.tsx
@@ -37,6 +37,8 @@ import { LoadingTable } from "@/components/data-table/loading-table";
import {
type DownloadAsArgs,
DownloadAsSchema,
+ type GetExportMetadata,
+ GetExportMetadataSchema,
} from "@/components/data-table/schemas";
import { TableExplorerPanel } from "@/components/data-table/table-explorer-panel/table-explorer-panel";
import {
@@ -210,6 +212,7 @@ interface Data<T> {
// oxlint-disable-next-line typescript/consistent-type-definitions
type DataTableFunctions = {
download_as: DownloadAsArgs;
+ get_export_metadata: GetExportMetadata;
get_column_summaries: <T>(opts: {}) => Promise<ColumnSummaries<T>>;
search: <T>(req: {
sort?: {
@@ -302,6 +305,7 @@ export const DataTablePlugin = createPlugin<S>("marimo-table")
)
.withFunctions<DataTableFunctions>({
download_as: DownloadAsSchema,
+ get_export_metadata: GetExportMetadataSchema,
get_column_summaries: rpc.input(z.looseObject({})).output(
z.object({
data: z.union([z.string(), z.array(z.looseObject({}))]).nullable(),
@@ -840,6 +844,7 @@ const DataTableComponent = ({
paginationState,
setPaginationState,
download_as: downloadAs,
+ get_export_metadata: getExportMetadata,
columnSummaries,
className,
setValue,
@@ -1168,6 +1173,7 @@ const DataTableComponent = ({
hoverTemplate={hoverTemplate}
cellHoverTexts={cellHoverTexts}
downloadAs={showDownload ? downloadAs : undefined}
+ getExportMetadata={showDownload ? getExportMetadata : undefined}
showSearch={showSearch}
searchQuery={searchQuery}
onSearchQueryChange={setSearchQuery}
diff --git a/frontend/src/plugins/impl/__tests__/DataTablePlugin.test.tsx b/frontend/src/plugins/impl/__tests__/DataTablePlugin.test.tsx
index 2080d56b01..18f94a06ab 100644
--- a/frontend/src/plugins/impl/__tests__/DataTablePlugin.test.tsx
+++ b/frontend/src/plugins/impl/__tests__/DataTablePlugin.test.tsx
@@ -69,6 +69,7 @@ describe("LoadingDataTableComponent", () => {
cell_hover_texts: null,
})}
download_as={vi.fn() as DownloadAsArgs}
+ get_export_metadata={vi.fn()}
get_column_summaries={vi.fn()}
get_data_url={vi.fn() as GetDataUrl}
get_row_ids={vi.fn() as GetRowIds}
@@ -123,6 +124,7 @@ describe("LoadingDataTableComponent", () => {
value: [] as (number | string | { rowId: string; columnName?: string })[],
setValue: vi.fn(),
download_as: vi.fn() as DownloadAsArgs,
+ get_export_metadata: vi.fn(),
get_column_summaries: vi.fn(),
get_data_url: vi.fn() as GetDataUrl,
get_row_ids: vi.fn() as GetRowIds,
@@ -264,6 +266,7 @@ describe("LoadingDataTableComponent", () => {
value: [] as (number | string | { rowId: string; columnName?: string })[],
setValue,
download_as: vi.fn() as DownloadAsArgs,
+ get_export_metadata: vi.fn(),
get_column_summaries: vi.fn().mockResolvedValue({
data: null,
stats: {},
@@ -380,6 +383,7 @@ describe("static notebook control suppression", () => {
cell_hover_texts: null,
}),
download_as: vi.fn() as DownloadAsArgs,
+ get_export_metadata: vi.fn(),
get_column_summaries: vi.fn().mockResolvedValue({
data: null,
stats: {},
diff --git a/frontend/src/plugins/impl/data-frames/DataFramePlugin.tsx b/frontend/src/plugins/impl/data-frames/DataFramePlugin.tsx
index f316585d6f..439a901cd9 100644
--- a/frontend/src/plugins/impl/data-frames/DataFramePlugin.tsx
+++ b/frontend/src/plugins/impl/data-frames/DataFramePlugin.tsx
@@ -14,6 +14,8 @@ import { z } from "zod";
import {
type DownloadAsArgs,
DownloadAsSchema,
+ type GetExportMetadata,
+ GetExportMetadataSchema,
} from "@/components/data-table/schemas";
import type { FieldTypesWithExternalType } from "@/components/data-table/types";
import { ReadonlyCode } from "@/components/editor/code/readonly-python-code";
@@ -83,6 +85,7 @@ type PluginFunctions = {
total_rows: number;
}>;
download_as: DownloadAsArgs;
+ get_export_metadata: GetExportMetadata;
get_size_bytes: (opts: Record<string, never>) => Promise<{
size_bytes?: number | null;
}>;
@@ -153,6 +156,7 @@ export const DataFramePlugin = createPlugin<S>("marimo-dataframe")
}),
),
download_as: DownloadAsSchema,
+ get_export_metadata: GetExportMetadataSchema,
get_size_bytes: rpc
.input(z.object({}))
.output(z.object({ size_bytes: z.number().nullish() })),
@@ -194,6 +198,7 @@ export const DataFrameComponent = memo(
get_column_values,
search,
download_as,
+ get_export_metadata,
get_size_bytes,
host,
}: DataTableProps): JSX.Element => {
@@ -337,6 +342,7 @@ export const DataFrameComponent = memo(
rowHeaders={row_headers || Arrays.EMPTY}
showDownload={showDownload}
download_as={download_as}
+ get_export_metadata={get_export_metadata}
get_size_bytes={get_size_bytes}
showSearch={false}
showFilters={false}
diff --git a/frontend/src/stories/dataframe.stories.tsx b/frontend/src/stories/dataframe.stories.tsx
index 6dbb05c533..0a3ec1f12e 100644
--- a/frontend/src/stories/dataframe.stories.tsx
+++ b/frontend/src/stories/dataframe.stories.tsx
@@ -42,6 +42,12 @@ export const DataFrame: StoryObj = {
host={document.body}
showDownload={false}
download_as={async () => ({ url: "", filename: "" })}
+ get_export_metadata={async () => ({
+ geometry_columns: [],
+ primary_geometry_column: null,
+ default_geometry_column: null,
+ formats: {},
+ })}
get_size_bytes={async () => ({ size_bytes: null })}
lazy={false}
/>
diff --git a/marimo/_plugins/ui/_impl/dataframes/dataframe.py b/marimo/_plugins/ui/_impl/dataframes/dataframe.py
index 31b7f7deb8..59595e9017 100644
--- a/marimo/_plugins/ui/_impl/dataframes/dataframe.py
+++ b/marimo/_plugins/ui/_impl/dataframes/dataframe.py
@@ -35,6 +35,11 @@ from marimo._plugins.ui._impl.table import (
TableSearchError,
)
from marimo._plugins.ui._impl.tables.format import FormatMapping
+from marimo._plugins.ui._impl.tables.geometry_export import (
+ ExportMetadata,
+ GeometryExportError,
+ get_export_metadata,
+)
from marimo._plugins.ui._impl.tables.table_manager import (
FieldTypes,
TableManager,
@@ -230,6 +235,11 @@ class dataframe(UIElement[dict[str, Any], DataFrameType]):
arg_cls=DownloadAsArgs,
function=self._download_as,
),
+ Function(
+ name="get_export_metadata",
+ arg_cls=EmptyArgs,
+ function=self._get_export_metadata,
+ ),
Function(
name="get_size_bytes",
arg_cls=EmptyArgs,
@@ -348,6 +358,10 @@ class dataframe(UIElement[dict[str, Any], DataFrameType]):
total_rows=result.get_num_rows(force=True) or 0,
)
+ def _get_export_metadata(self, args: EmptyArgs) -> ExportMetadata:
+ del args
+ return get_export_metadata(get_table_manager(self._value))
+
def _download_as(self, args: DownloadAsArgs) -> DownloadAsResponse:
"""Download the transformed dataframe in the specified format.
@@ -379,12 +393,21 @@ class dataframe(UIElement[dict[str, Any], DataFrameType]):
widget_defaults
)
- url, filename = download_as(
- manager,
- args.format,
- options=options,
- filename=bound_filename,
- )
+ try:
+ url, filename = download_as(
+ manager,
+ args.format,
+ options=options,
+ filename=bound_filename,
+ geometry_column=args.geometry_column,
+ )
+ except GeometryExportError as e:
+ return DownloadAsResponse(
+ error=str(e),
+ code=e.code,
+ column=e.column,
+ missing_packages=e.missing_packages,
+ )
return DownloadAsResponse(url=url, filename=filename)
def _apply_filters_query_sort(
diff --git a/marimo/_plugins/ui/_impl/table.py b/marimo/_plugins/ui/_impl/table.py
index 21f23939f5..daf37bf282 100644
--- a/marimo/_plugins/ui/_impl/table.py
+++ b/marimo/_plugins/ui/_impl/table.py
@@ -41,6 +41,13 @@ from marimo._plugins.ui._impl.dataframes.transforms.types import (
TransformType,
validate_operator_for_dtype,
)
+from marimo._plugins.ui._impl.tables.geometry_export import (
+ ExportMetadata,
+ GeometryExportError,
+ GeometryExportErrorCode,
+ get_export_metadata,
+ has_geometry_columns,
+)
from marimo._plugins.ui._impl.tables.selection import (
INDEX_COLUMN_NAME,
add_selection_column,
@@ -133,6 +140,7 @@ class DownloadAsOptions:
class DownloadAsArgs:
format: Literal["csv", "tsv", "json", "parquet"]
options: DownloadAsOptions | None = None
+ geometry_column: str | None = None
@dataclass
@@ -144,6 +152,8 @@ class DownloadAsResponse:
# ColumnPreview so the frontend can reuse its install-prompt flow.
error: str | None = None
missing_packages: list[str] | None = None
+ code: GeometryExportErrorCode | None = None
+ column: str | None = None
@dataclass
@@ -967,6 +977,11 @@ class table(
arg_cls=DownloadAsArgs,
function=self._download_as,
),
+ Function(
+ name="get_export_metadata",
+ arg_cls=EmptyArgs,
+ function=self._get_export_metadata,
+ ),
Function(
name="get_column_summaries",
arg_cls=ColumnSummariesArgs,
@@ -1059,6 +1074,10 @@ class table(
manager = self._searched_manager or self._manager
return GetSizeBytesResponse(size_bytes=manager.estimate_size_bytes())
+ def _get_export_metadata(self, args: EmptyArgs) -> ExportMetadata:
+ del args
+ return get_export_metadata(self._manager)
+
def _download_as(self, args: DownloadAsArgs) -> DownloadAsResponse:
"""Download the table data in the specified format.
@@ -1090,8 +1109,10 @@ class table(
something other than a `TableManager` (e.g., a raw list
of `TableCell` from cell-selection modes).
"""
- # Short-circuit Parquet when no parquet-capable lib is importable.
- if args.format == "parquet":
+ # Short-circuit ordinary Parquet when no parquet-capable lib is importable.
+ if args.format == "parquet" and not has_geometry_columns(
+ self._manager
+ ):
has_polars = DependencyManager.polars.has()
has_pandas = DependencyManager.pandas.has()
has_pyarrow = DependencyManager.pyarrow.has()
@@ -1137,13 +1158,22 @@ class table(
options = (args.options or DownloadAsOptions()).resolve(
DownloadOptions()
)
- url, filename = download_as(
- manager_candidate,
- args.format,
- drop_marimo_index=True,
- options=options,
- filename=bound_filename,
- )
+ try:
+ url, filename = download_as(
+ manager_candidate,
+ args.format,
+ drop_marimo_index=True,
+ options=options,
+ filename=bound_filename,
+ geometry_column=args.geometry_column,
+ )
+ except GeometryExportError as e:
+ return DownloadAsResponse(
+ error=str(e),
+ code=e.code,
+ column=e.column,
+ missing_packages=e.missing_packages,
+ )
return DownloadAsResponse(url=url, filename=filename)
else:
raise NotImplementedError(
diff --git a/marimo/_plugins/ui/_impl/tables/geometry_export.py b/marimo/_plugins/ui/_impl/tables/geometry_export.py
index 0fc44d0fa5..a1ed9e113b 100644
--- a/marimo/_plugins/ui/_impl/tables/geometry_export.py
+++ b/marimo/_plugins/ui/_impl/tables/geometry_export.py
@@ -1,12 +1,19 @@
# Copyright 2026 Marimo. All rights reserved.
from __future__ import annotations
-from typing import TYPE_CHECKING, Any
+import json
+from dataclasses import dataclass, field
+from io import BytesIO
+from typing import TYPE_CHECKING, Any, Literal
import narwhals.stable.v2 as nw
from marimo._dependencies.dependencies import DependencyManager
-from marimo._plugins.ui._impl.tables.geometry import GeometryColumnInfo
+from marimo._plugins.ui._impl.tables.geometry import (
+ GeometryColumnInfo,
+ GeometryEncoding,
+ find_geometry_columns,
+)
from marimo._plugins.ui._impl.tables.narwhals_table import (
NarwhalsTableManager,
)
@@ -17,6 +24,410 @@ if TYPE_CHECKING:
import pyarrow as pa
+GeometryExportErrorCode = Literal[
+ "geometry_required",
+ "invalid_geometry",
+ "invalid_metadata",
+ "unsupported_representation",
+ "unsupported_version",
+ "missing_packages",
+ "conversion_failed",
+]
+
+_MIN_GEOPANDAS_GEOPARQUET_VERSION = "0.14.1"
+_GEOPANDAS_UPGRADE_MESSAGE = (
+ "Update geopandas to 0.14.1 or newer to export GeoParquet."
+)
+
+
+class GeometryExportError(Exception):
+ """An export failure that the dialog can explain and recover from.
+
+ Args:
+ code (GeometryExportErrorCode): Machine-readable failure category.
+ message (str): User-facing failure explanation.
+ column (str | None, optional): Affected geometry column.
+ missing_packages (list[str] | None, optional): Required packages.
+ """
+
+ def __init__(
+ self,
+ code: GeometryExportErrorCode,
+ message: str,
+ column: str | None = None,
+ missing_packages: list[str] | None = None,
+ ) -> None:
+ super().__init__(message)
+ self.code = code
+ self.column = column
+ self.missing_packages = missing_packages
+
+
+@dataclass(frozen=True)
+class GeometryExportColumn:
+ """Geometry declared by the export source.
+
+ Args:
+ name (str): Name of the geometry column.
+ encoding (GeometryEncoding): Declared value encoding.
+ crs (str | dict[str, Any] | None): Declared CRS, when available.
+ """
+
+ name: str
+ encoding: GeometryEncoding
+ crs: str | dict[str, Any] | None
+
+
+@dataclass(frozen=True)
+class ExportFormatEligibility:
+ """Known prerequisites for a format.
+
+ Args:
+ available (bool): Whether the source can use the format.
+ reason (str | None): Reason when the format is unavailable.
+ missing_packages (list[str]): Packages needed for the format.
+ """
+
+ available: bool
+ reason: str | None = None
+ missing_packages: list[str] = field(default_factory=list)
+
+
+@dataclass(frozen=True)
+class ExportMetadata:
+ """Schema-level geometry and format metadata for an export source.
+
+ Args:
+ geometry_columns (list[GeometryExportColumn]): All declared geometry
+ columns, including columns outside the visible preview.
+ primary_geometry_column (str | None): Valid source primary, if any.
+ default_geometry_column (str | None): Primary or sole geometry.
+ formats (dict[str, ExportFormatEligibility]): Eligibility for
+ geometry-aware formats. The serializer checks values separately.
+ """
+
+ geometry_columns: list[GeometryExportColumn] = field(default_factory=list)
+ primary_geometry_column: str | None = None
+ default_geometry_column: str | None = None
+ formats: dict[str, ExportFormatEligibility] = field(default_factory=dict)
+
+
+def get_export_metadata(manager: TableManager[Any]) -> ExportMetadata:
+ """Read geometry declarations without materializing source rows.
+
+ Args:
+ manager (TableManager[Any]): Manager for the export source.
+ """
+ if not isinstance(manager, NarwhalsTableManager):
+ return ExportMetadata()
+
+ info_by_name = find_geometry_columns(manager.data)
+ if not info_by_name:
+ return ExportMetadata()
+
+ source = manager.data
+ primary: str | None = None
+ columns: list[GeometryExportColumn] = []
+ if source.implementation.is_pandas():
+ native = source.to_native()
+ candidate = getattr(native, "_geometry_column_name", None)
+ if isinstance(candidate, str) and candidate in info_by_name:
+ primary = candidate
+ for name, info in info_by_name.items():
+ crs = native[name].array.crs
+ columns.append(
+ GeometryExportColumn(
+ name=name,
+ encoding=info.encoding,
+ crs=crs.to_string() if crs is not None else None,
+ )
+ )
+ is_supported_source = DependencyManager.geopandas.has()
+ elif source.implementation.is_pyarrow():
+ native = source.to_native()
+ for name, info in info_by_name.items():
+ columns.append(
+ GeometryExportColumn(
+ name=name,
+ encoding=info.encoding,
+ crs=_arrow_crs(native.schema.field(name).metadata),
+ )
+ )
+ is_supported_source = False
+ else:
+ columns = [
+ GeometryExportColumn(name=name, encoding=info.encoding, crs=None)
+ for name, info in info_by_name.items()
+ ]
+ is_supported_source = False
+
+ if not is_supported_source:
+ if source.implementation.is_pandas():
+ reason = "This pandas table needs geopandas to export GeoParquet."
+ missing_packages = ["geopandas"]
+ elif source.implementation.is_pyarrow():
+ reason = (
+ "GeoParquet export from Arrow tables is not supported yet."
+ )
+ missing_packages = []
+ else:
+ reason = "GeoParquet export is not supported for this table type."
+ missing_packages = []
+ parquet = ExportFormatEligibility(
+ available=False,
+ reason=reason,
+ missing_packages=missing_packages,
+ )
+ elif not DependencyManager.geopandas.has_at_version(
+ min_version=_MIN_GEOPANDAS_GEOPARQUET_VERSION, quiet=True
+ ):
+ parquet = ExportFormatEligibility(
+ available=False,
+ reason=_GEOPANDAS_UPGRADE_MESSAGE,
+ )
+ elif not DependencyManager.pyarrow.has():
+ parquet = ExportFormatEligibility(
+ available=False,
+ reason="GeoParquet export requires pyarrow.",
+ missing_packages=["pyarrow"],
+ )
+ else:
+ parquet = ExportFormatEligibility(available=True)
+
+ default = (
+ primary
+ if primary is not None
+ else (columns[0].name if len(columns) == 1 else None)
+ )
+ return ExportMetadata(
+ geometry_columns=columns,
+ primary_geometry_column=primary,
+ default_geometry_column=default,
+ formats={"parquet": parquet},
+ )
+
+
+def _arrow_crs(
+ metadata: dict[bytes, bytes] | None,
+) -> str | dict[str, Any] | None:
+ if not metadata:
+ return None
+ raw = metadata.get(b"ARROW:extension:metadata")
+ if raw is None:
+ return None
+ try:
+ declaration = json.loads(raw)
+ except (ValueError, UnicodeDecodeError):
+ return None
+ if not isinstance(declaration, dict):
+ return None
+ crs = declaration.get("crs")
+ return crs if isinstance(crs, (str, dict)) else None
+
+
+def has_geometry_columns(manager: TableManager[Any]) -> bool:
+ """Check geometry declarations without inspecting rows.
+
+ Args:
+ manager (TableManager[Any]): Manager for the export source.
+ """
+ return isinstance(manager, NarwhalsTableManager) and bool(
+ find_geometry_columns(manager.data)
+ )
+
+
+def serialize_geoparquet(
+ manager: TableManager[Any], geometry_column: str | None
+) -> bytes | None:
+ """Write declared GeoPandas geometry as GeoParquet 1.0.0.
+
+ Args:
+ manager (TableManager[Any]): Manager for the effective export rows.
+ geometry_column (str | None): Explicit primary geometry, if chosen.
+
+ Returns:
+ bytes | None: GeoParquet bytes, or None for an ordinary table.
+
+ Raises:
+ GeometryExportError: If a geometry source cannot produce valid
+ GeoParquet or the requested primary is invalid.
+ """
+ if not has_geometry_columns(manager):
+ if geometry_column is not None:
+ raise GeometryExportError(
+ "invalid_geometry",
+ f"{geometry_column!r} is not a geometry column in this table.",
+ column=geometry_column,
+ )
+ return None
+
+ if (
+ not isinstance(manager, NarwhalsTableManager)
+ or not manager.data.implementation.is_pandas()
+ ):
+ raise GeometryExportError(
+ "unsupported_representation",
+ "GeoParquet export from this table type is not supported yet.",
+ column=geometry_column,
+ )
+
+ native = manager.as_frame().to_native()
+ if not native.columns.is_unique:
+ raise GeometryExportError(
+ "invalid_metadata",
+ "GeoParquet export requires unique column names.",
+ )
+
+ try:
+ metadata = get_export_metadata(manager)
+ except Exception as e:
+ raise GeometryExportError(
+ "invalid_metadata", f"Could not read geometry metadata: {e}"
+ ) from e
+
+ names = {column.name for column in metadata.geometry_columns}
+ if geometry_column is not None and geometry_column not in names:
+ raise GeometryExportError(
+ "invalid_geometry",
+ f"{geometry_column!r} is not a geometry column in this table.",
+ column=geometry_column,
+ )
+ primary = (
+ geometry_column
+ if geometry_column is not None
+ else metadata.default_geometry_column
+ )
+ if primary is None:
+ raise GeometryExportError(
+ "geometry_required",
+ "Choose a primary geometry column for GeoParquet export.",
+ )
+
+ if not DependencyManager.geopandas.has():
+ raise GeometryExportError(
+ "missing_packages",
+ "This pandas table needs geopandas to export GeoParquet.",
+ missing_packages=["geopandas"],
+ )
+ if not DependencyManager.geopandas.has_at_version(
+ min_version=_MIN_GEOPANDAS_GEOPARQUET_VERSION, quiet=True
+ ):
+ raise GeometryExportError(
+ "unsupported_version", _GEOPANDAS_UPGRADE_MESSAGE
+ )
+ if not DependencyManager.pyarrow.has():
+ raise GeometryExportError(
+ "missing_packages",
+ "GeoParquet export requires pyarrow.",
+ missing_packages=["pyarrow"],
+ )
+
+ import geopandas as gpd # type: ignore[import-not-found,import-untyped,unused-ignore]
+
+ for name in names:
+ for geometry in native[name].array:
+ if geometry is not None and getattr(geometry, "has_m", False):
+ raise GeometryExportError(
+ "unsupported_representation",
+ f"Geometry column {name!r} contains M coordinates.",
+ column=name,
+ )
+
+ try:
+ export_frame = gpd.GeoDataFrame(
+ native.copy(deep=True), geometry=primary
+ )
+ buffer = BytesIO()
+ export_frame.to_parquet(
+ buffer,
+ index=None,
+ schema_version="1.0.0",
+ )
+ artifact = buffer.getvalue()
+ _validate_geoparquet(artifact, export_frame, primary, names)
+ except GeometryExportError:
+ raise
+ except Exception as e:
+ raise GeometryExportError(
+ "conversion_failed",
+ f"Could not export GeoParquet: {e}",
+ column=primary,
+ ) from e
+ return artifact
+
+
+def _validate_geoparquet(
+ artifact: bytes,
+ source: Any,
+ primary: str,
+ geometry_columns: set[str],
+) -> None:
+ import pyarrow as pa
+ import pyarrow.parquet as pq
+ from pyproj import CRS # type: ignore[import-not-found,import-untyped,unused-ignore]
+
+ try:
+ file_metadata = pq.read_metadata(BytesIO(artifact))
+ except Exception as e:
+ raise GeometryExportError(
+ "invalid_metadata", "The GeoParquet file is not valid Parquet."
+ ) from e
+ raw_geo = (file_metadata.metadata or {}).get(b"geo")
+ if raw_geo is None:
+ raise GeometryExportError(
+ "invalid_metadata", "The GeoParquet file has no geo metadata."
+ )
+ try:
+ geo = json.loads(raw_geo)
+ except (ValueError, UnicodeDecodeError) as e:
+ raise GeometryExportError(
+ "invalid_metadata", "The GeoParquet metadata is not valid JSON."
+ ) from e
+ if (
+ not isinstance(geo, dict)
+ or geo.get("version") != "1.0.0"
+ or geo.get("primary_column") != primary
+ or not isinstance(geo.get("columns"), dict)
+ or set(geo["columns"]) != geometry_columns
+ ):
+ raise GeometryExportError(
+ "invalid_metadata", "The GeoParquet file metadata is incomplete."
+ )
+ schema = file_metadata.schema.to_arrow_schema()
+ for name in geometry_columns:
+ column = geo["columns"][name]
+ if (
+ not isinstance(column, dict)
+ or column.get("encoding") != "WKB"
+ or not isinstance(column.get("geometry_types"), list)
+ or "crs" not in column
+ or name not in schema.names
+ or not pa.types.is_binary(schema.field(name).type)
+ ):
+ raise GeometryExportError(
+ "invalid_metadata",
+ f"The GeoParquet metadata for {name!r} is invalid.",
+ column=name,
+ )
+ expected_crs = source[name].crs
+ stored_crs = column["crs"]
+ if expected_crs is None:
+ valid_crs = stored_crs is None
+ else:
+ try:
+ valid_crs = isinstance(stored_crs, dict) and (
+ CRS.from_json_dict(stored_crs) == expected_crs
+ )
+ except Exception:
+ valid_crs = False
+ if not valid_crs:
+ raise GeometryExportError(
+ "invalid_metadata",
+ f"The GeoParquet CRS for {name!r} is invalid.",
+ column=name,
+ )
+
+
def prepare_geometry_text_export(
manager: TableManager[Any],
) -> TableManager[Any]:
diff --git a/marimo/_plugins/ui/_impl/utils/dataframe.py b/marimo/_plugins/ui/_impl/utils/dataframe.py
index db2b9cfd6f..c644dfd7b3 100644
--- a/marimo/_plugins/ui/_impl/utils/dataframe.py
+++ b/marimo/_plugins/ui/_impl/utils/dataframe.py
@@ -11,7 +11,9 @@ from marimo._output.data import data as mo_data
from marimo._output.mime import MIME
from marimo._plugins.core.web_component import JSONType
from marimo._plugins.ui._impl.tables.geometry_export import (
+ GeometryExportError,
prepare_geometry_text_export,
+ serialize_geoparquet,
)
from marimo._plugins.ui._impl.tables.selection import INDEX_COLUMN_NAME
from marimo._plugins.ui._impl.tables.table_manager import TableManager
@@ -172,6 +174,7 @@ def download_as(
drop_marimo_index: bool = False,
options: DownloadOptions | None = None,
filename: str | None = None,
+ geometry_column: str | None = None,
) -> tuple[str, str]:
"""Download the table data in the specified format.
@@ -186,6 +189,8 @@ def download_as(
Defaults to each format's defaults.
filename (str | None, optional): The filename to use for the
downloaded file. Defaults to None, which uses a random filename.
+ geometry_column (str | None, optional): Primary geometry requested
+ for a geometry-aware format. Defaults to None.
Returns:
tuple: (url, user-facing filename with extension) for the downloaded file.
@@ -204,10 +209,22 @@ def download_as(
raise ValueError(f"format must be one of {allowed}.")
if ext in ("csv", "tsv", "json"):
+ if geometry_column is not None:
+ raise GeometryExportError(
+ "invalid_geometry",
+ "A primary geometry can only be chosen for GeoParquet export.",
+ column=geometry_column,
+ )
manager = prepare_geometry_text_export(manager)
+ artifact = (
+ serialize_geoparquet(manager, geometry_column)
+ if ext == "parquet"
+ else None
+ )
vfile = mo_data.any_data(
- fmt.serialize(manager, options), ext=fmt.extension
+ artifact if artifact is not None else fmt.serialize(manager, options),
+ ext=fmt.extension,
)
base_name = filename if filename is not None else "download"
return (vfile.url, f"{base_name}.{fmt.extension}")
diff --git a/tests/_plugins/ui/_impl/test_export_metadata.py b/tests/_plugins/ui/_impl/test_export_metadata.py
new file mode 100644
index 0000000000..086dd8d833
--- /dev/null
+++ b/tests/_plugins/ui/_impl/test_export_metadata.py
@@ -0,0 +1,239 @@
+# Copyright 2026 Marimo. All rights reserved.
+from __future__ import annotations
+
+from dataclasses import asdict
+from typing import Any
+from unittest.mock import patch
+
+import pytest
+
+from marimo._dependencies.dependencies import DependencyManager
+from marimo._plugins import ui
+from marimo._plugins.ui._impl.table import DownloadAsArgs
+from marimo._plugins.ui._impl.tables.narwhals_table import NarwhalsTableManager
+from marimo._runtime.functions import EmptyArgs
+from marimo._utils.parse_dataclass import parse_raw
+from tests._plugins.ui._impl.tables import geometry_fixtures as fixtures
+
+
[email protected](params=[ui.table, ui.dataframe], ids=["table", "dataframe"])
+def widget(request: pytest.FixtureRequest) -> Any:
+ return request.param
+
+
[email protected]("geopandas", "pyarrow")
+def test_multigeometry_metadata_does_not_read_rows(widget: Any) -> None:
+ source = fixtures.gdf_multi_geometry()
+ subject = widget(source)
+ with (
+ patch.object(
+ NarwhalsTableManager,
+ "as_frame",
+ side_effect=AssertionError("Metadata must not collect rows"),
+ ),
+ patch.object(
+ NarwhalsTableManager,
+ "to_json_str",
+ side_effect=AssertionError("Metadata must not serialize rows"),
+ ),
+ ):
+ result = subject._get_export_metadata(EmptyArgs())
+
+ assert asdict(result) == {
+ "geometry_columns": [
+ {"name": "geom_a", "encoding": "objects", "crs": "EPSG:4326"},
+ {"name": "geom_b", "encoding": "objects", "crs": "EPSG:3857"},
+ ],
+ "primary_geometry_column": "geom_a",
+ "default_geometry_column": "geom_a",
+ "formats": {
+ "parquet": {
+ "available": True,
+ "reason": None,
+ "missing_packages": [],
+ },
+ },
+ }
+ assert source.geometry.name == "geom_a"
+
+
[email protected]("geopandas")
+ ("factory", "primary", "default"),
+ [
+ (fixtures.gdf_no_active_geometry, None, "g"),
+ (fixtures.gdf_stale_pointer, None, "geom"),
+ (fixtures.gdf_dropped_active, None, "geom_b"),
+ ],
+)
+def test_stale_or_missing_primary_uses_sole_geometry(
+ widget: Any, factory: Any, primary: str | None, default: str
+) -> None:
+ result = widget(factory())._get_export_metadata(EmptyArgs())
+ assert (
+ result.primary_geometry_column,
+ result.default_geometry_column,
+ ) == (
+ primary,
+ default,
+ )
+
+
[email protected]("geopandas")
+def test_ambiguous_geometry_has_no_default(widget: Any) -> None:
+ import pandas as pd
+
+ result = widget(
+ pd.DataFrame(fixtures.gdf_multi_geometry())
+ )._get_export_metadata(EmptyArgs())
+ assert [column.name for column in result.geometry_columns] == [
+ "geom_a",
+ "geom_b",
+ ]
+ assert result.primary_geometry_column is None
+ assert result.default_geometry_column is None
+
+
[email protected]("geopandas")
+def test_old_geopandas_is_not_eligible(widget: Any) -> None:
+ with patch.object(
+ DependencyManager.geopandas, "get_version", return_value="0.14.0"
+ ):
+ result = widget(fixtures.gdf_multi_geometry())._get_export_metadata(
+ EmptyArgs()
+ )
+
+ assert asdict(result.formats["parquet"]) == {
+ "available": False,
+ "reason": "Update geopandas to 0.14.1 or newer to export GeoParquet.",
+ "missing_packages": [],
+ }
+
+
[email protected]("geopandas")
+def test_empty_name_can_be_the_source_primary(widget: Any) -> None:
+ source = fixtures.gdf_multi_geometry().rename(columns={"geom_b": ""})
+ source = source.set_geometry("")
+ result = widget(source)._get_export_metadata(EmptyArgs())
+ assert result.primary_geometry_column == ""
+ assert result.default_geometry_column == ""
+
+
[email protected]("geopandas")
+def test_empty_geometry_retains_unknown_crs(widget: Any) -> None:
+ source = fixtures.gdf_all_null().head(0)
+ result = widget(source)._get_export_metadata(EmptyArgs())
+ assert [asdict(column) for column in result.geometry_columns] == [
+ {"name": "geometry", "encoding": "objects", "crs": None},
+ ]
+
+
[email protected]("geopandas")
+def test_missing_pyarrow_is_reported(widget: Any) -> None:
+ subject = widget(fixtures.gdf_multi_geometry())
+ with patch.object(DependencyManager.pyarrow, "has", return_value=False):
+ result = subject._get_export_metadata(EmptyArgs())
+ assert asdict(result.formats["parquet"]) == {
+ "available": False,
+ "reason": "GeoParquet export requires pyarrow.",
+ "missing_packages": ["pyarrow"],
+ }
+
+
[email protected]("geopandas")
+def test_missing_geopandas_is_reported(widget: Any) -> None:
+ subject = widget(fixtures.gdf_multi_geometry())
+ with patch.object(DependencyManager.geopandas, "has", return_value=False):
+ result = subject._get_export_metadata(EmptyArgs())
+ assert asdict(result.formats["parquet"]) == {
+ "available": False,
+ "reason": "This pandas table needs geopandas to export GeoParquet.",
+ "missing_packages": ["geopandas"],
+ }
+
+
+def test_arrow_crs_comes_from_field_metadata(widget: Any) -> None:
+ subject = widget(fixtures.arrow_wkb_known_crs())
+ result = subject._get_export_metadata(EmptyArgs())
+ assert [asdict(column) for column in result.geometry_columns] == [
+ {"name": "geom", "encoding": "wkb", "crs": "EPSG:4326"},
+ ]
+ assert asdict(result.formats["parquet"]) == {
+ "available": False,
+ "reason": "GeoParquet export from Arrow tables is not supported yet.",
+ "missing_packages": [],
+ }
+
+
+def test_ordinary_metadata_needs_no_geospatial_packages(widget: Any) -> None:
+ import pandas as pd
+
+ subject = widget(pd.DataFrame({"geometry": ["POINT (1 2)"]}))
+ with patch.object(DependencyManager.geopandas, "has", return_value=False):
+ assert asdict(subject._get_export_metadata(EmptyArgs())) == {
+ "geometry_columns": [],
+ "primary_geometry_column": None,
+ "default_geometry_column": None,
+ "formats": {},
+ }
+
+
[email protected]("geopandas")
+def test_table_metadata_includes_columns_outside_the_preview() -> None:
+ subject = ui.table(
+ fixtures.gdf_multi_geometry(),
+ max_columns=1,
+ hidden_columns=["geom_b"],
+ )
+ result = subject._get_export_metadata(EmptyArgs())
+ assert [column.name for column in result.geometry_columns] == [
+ "geom_a",
+ "geom_b",
+ ]
+
+
[email protected]("geopandas")
+def test_dataframe_metadata_follows_transformed_columns() -> None:
+ subject = ui.dataframe(fixtures.gdf_multi_geometry())
+ subject._update(
+ {"transforms": [{"type": "select_columns", "column_ids": ["geom_b"]}]}
+ )
+ result = subject._get_export_metadata(EmptyArgs())
+ assert [column.name for column in result.geometry_columns] == ["geom_b"]
+ assert result.default_geometry_column == "geom_b"
+
+
+ "payload",
+ [
+ {"format": "parquet"},
+ {"format": "parquet", "geometry_column": "geom_b"},
+ {"format": "csv", "options": {"separator": ";"}},
+ ],
+)
+def test_download_request_accepts_optional_geometry(
+ payload: dict[str, Any],
+) -> None:
+ parsed = parse_raw(payload, DownloadAsArgs)
+ assert parsed.geometry_column == payload.get("geometry_column")
+ assert parsed.format == payload["format"]
+
+
[email protected]("geopandas", "pyarrow")
+def test_widget_passes_geometry_request_to_export(widget: Any) -> None:
+ subject = widget(fixtures.gdf_multi_geometry())
+ module = (
+ "marimo._plugins.ui._impl.table"
+ if widget is ui.table
+ else "marimo._plugins.ui._impl.dataframes.dataframe"
+ )
+ with patch(
+ f"{module}.download_as", return_value=("data:test", "test.parquet")
+ ) as export:
+ response = subject._download_as(
+ DownloadAsArgs(format="parquet", geometry_column="geom_b")
+ )
+ assert response.url == "data:test"
+ assert export.call_args.kwargs["geometry_column"] == "geom_b"
diff --git a/tests/_plugins/ui/_impl/test_geoparquet_export.py b/tests/_plugins/ui/_impl/test_geoparquet_export.py
new file mode 100644
index 0000000000..540b4c2c0d
--- /dev/null
+++ b/tests/_plugins/ui/_impl/test_geoparquet_export.py
@@ -0,0 +1,401 @@
+# Copyright 2026 Marimo. All rights reserved.
+from __future__ import annotations
+
+import io
+import json
+from typing import Any
+from unittest.mock import patch
+
+import pytest
+
+from marimo._dependencies.dependencies import DependencyManager
+from marimo._plugins import ui
+from marimo._plugins.ui._impl.table import DownloadAsArgs
+from marimo._runtime.functions import EmptyArgs
+from marimo._utils.data_uri import from_data_uri
+from tests._plugins.ui._impl.tables import geometry_fixtures as fixtures
+
+
[email protected](params=[ui.table, ui.dataframe], ids=["table", "dataframe"])
+def widget(request: pytest.FixtureRequest) -> Any:
+ return request.param
+
+
+def _artifact(
+ widget: Any, geometry_column: str | None = None
+) -> tuple[bytes, str]:
+ response = widget._download_as(
+ DownloadAsArgs(format="parquet", geometry_column=geometry_column)
+ )
+ assert response.error is None
+ return from_data_uri(response.url)[1], response.filename
+
+
[email protected]("geopandas", "pyarrow")
[email protected]("choice", [None, "geom_b"])
+def test_geoparquet_keeps_every_geometry_and_crs(
+ widget: Any, choice: str | None
+) -> None:
+ import geopandas as gpd
+ import pyarrow as pa
+ import pyarrow.parquet as pq
+ from pyproj import CRS
+
+ source = fixtures.gdf_multi_geometry()
+ original = source.copy(deep=True)
+ artifact, filename = _artifact(widget(source), choice)
+ reader = pq.ParquetFile(io.BytesIO(artifact))
+ geo = json.loads(reader.metadata.metadata[b"geo"])
+ primary = choice or "geom_a"
+
+ assert filename.endswith(".parquet")
+ assert geo["version"] == "1.0.0"
+ assert geo["primary_column"] == primary
+ assert set(geo["columns"]) == {"geom_a", "geom_b"}
+ table = reader.read()
+ for name in ("geom_a", "geom_b"):
+ assert geo["columns"][name]["encoding"] == "WKB"
+ assert (
+ CRS.from_json_dict(geo["columns"][name]["crs"]) == source[name].crs
+ )
+ assert pa.types.is_binary(table.schema.field(name).type)
+ assert table[name][0].as_py() == source[name].iloc[0].wkb
+
+ restored = gpd.read_parquet(io.BytesIO(artifact))
+ assert restored.geometry.name == primary
+ assert restored["geom_a"].crs == source["geom_a"].crs
+ assert restored["geom_b"].crs == source["geom_b"].crs
+ assert source.geometry.name == "geom_a"
+ assert source.equals(original)
+
+
[email protected]("geopandas", "pyarrow")
+def test_geoparquet_uses_writer_supported_by_geopandas_0141(
+ widget: Any,
+) -> None:
+ import geopandas as gpd
+ import pyarrow.parquet as pq
+
+ original_writer = gpd.GeoDataFrame.to_parquet
+
+ def compatible_writer(
+ self: Any,
+ path: Any,
+ *,
+ index: bool | None = None,
+ schema_version: str | None = None,
+ ) -> None:
+ original_writer(self, path, index=index, schema_version=schema_version)
+
+ with patch.object(gpd.GeoDataFrame, "to_parquet", compatible_writer):
+ artifact, _ = _artifact(widget(fixtures.gdf_multi_geometry()))
+
+ geo = json.loads(pq.read_metadata(io.BytesIO(artifact)).metadata[b"geo"])
+ assert geo["version"] == "1.0.0"
+ assert all(
+ column["encoding"] == "WKB" for column in geo["columns"].values()
+ )
+
+
[email protected]("geopandas")
+def test_geoparquet_rejects_old_geopandas(widget: Any) -> None:
+ with patch.object(
+ DependencyManager.geopandas, "get_version", return_value="0.14.0"
+ ):
+ response = widget(fixtures.gdf_multi_geometry())._download_as(
+ DownloadAsArgs(format="parquet")
+ )
+
+ assert response.url == ""
+ assert response.code == "unsupported_version"
+ assert (
+ response.error
+ == "Update geopandas to 0.14.1 or newer to export GeoParquet."
+ )
+ assert response.missing_packages is None
+
+
[email protected]("geopandas", "pyarrow")
+def test_empty_name_is_a_valid_explicit_primary(widget: Any) -> None:
+ import pyarrow.parquet as pq
+
+ source = fixtures.gdf_multi_geometry().rename(columns={"geom_b": ""})
+ artifact, _ = _artifact(widget(source), "")
+ geo = json.loads(pq.read_metadata(io.BytesIO(artifact)).metadata[b"geo"])
+ assert geo["primary_column"] == ""
+ assert set(geo["columns"]) == {"geom_a", ""}
+
+
[email protected]("geopandas", "pyarrow")
+def test_ambiguous_pandas_geometry_requires_choice(widget: Any) -> None:
+ import pandas as pd
+
+ source = pd.DataFrame(fixtures.gdf_multi_geometry())
+ subject = widget(source)
+ response = subject._download_as(DownloadAsArgs(format="parquet"))
+ assert (
+ response.url,
+ response.filename,
+ response.code,
+ response.column,
+ ) == (
+ "",
+ "",
+ "geometry_required",
+ None,
+ )
+ artifact, _ = _artifact(subject, "geom_b")
+ import pyarrow.parquet as pq
+
+ geo = json.loads(pq.read_metadata(io.BytesIO(artifact)).metadata[b"geo"])
+ assert geo["primary_column"] == "geom_b"
+ assert not hasattr(source, "_geometry_column_name")
+
+
[email protected]("geopandas", "pyarrow")
+ "factory",
+ [fixtures.gdf_no_active_geometry, fixtures.gdf_stale_pointer],
+)
+def test_sole_geometry_survives_missing_or_stale_primary(
+ widget: Any, factory: Any
+) -> None:
+ import pyarrow.parquet as pq
+
+ source = factory()
+ artifact, _ = _artifact(widget(source))
+ geo = json.loads(pq.read_metadata(io.BytesIO(artifact)).metadata[b"geo"])
+ assert geo["primary_column"] in {"g", "geom"}
+
+
[email protected]("geopandas", "pyarrow")
[email protected]("empty", [False, True])
+def test_unknown_crs_is_explicit_for_empty_or_null_geometry(
+ widget: Any, empty: bool
+) -> None:
+ import pyarrow.parquet as pq
+
+ source = fixtures.gdf_all_null()
+ if empty:
+ source = source.head(0)
+ artifact, _ = _artifact(widget(source))
+ reader = pq.ParquetFile(io.BytesIO(artifact))
+ geo = json.loads(reader.metadata.metadata[b"geo"])
+ assert geo["columns"]["geometry"]["crs"] is None
+ assert reader.metadata.num_rows == len(source)
+
+
[email protected]("geopandas", "pyarrow")
+def test_geoparquet_keeps_supported_z_geometry(widget: Any) -> None:
+ import pyarrow.parquet as pq
+
+ artifact, _ = _artifact(widget(fixtures.gdf_3d()))
+ geo = json.loads(pq.read_metadata(io.BytesIO(artifact)).metadata[b"geo"])
+ assert "Point Z" in geo["columns"]["geometry"]["geometry_types"]
+
+
[email protected]("geopandas", "pyarrow")
+def test_geoparquet_rejects_unsupported_m_geometry(widget: Any) -> None:
+ import geopandas as gpd
+ from shapely import from_wkt
+
+ geometry = from_wkt("POINT M (1 2 3)")
+ if not getattr(geometry, "has_m", False):
+ pytest.skip("Installed Shapely cannot represent M coordinates")
+ source = gpd.GeoDataFrame({"geometry": [geometry]}, geometry="geometry")
+ response = widget(source)._download_as(DownloadAsArgs(format="parquet"))
+ assert (
+ response.url,
+ response.filename,
+ response.code,
+ response.column,
+ ) == (
+ "",
+ "",
+ "unsupported_representation",
+ "geometry",
+ )
+
+
[email protected]("geopandas", "pyarrow")
+def test_invalid_geometry_choice_publishes_no_artifact(widget: Any) -> None:
+ subject = widget(fixtures.gdf_multi_geometry())
+ with patch(
+ "marimo._plugins.ui._impl.utils.dataframe.mo_data.any_data"
+ ) as publish:
+ response = subject._download_as(
+ DownloadAsArgs(format="parquet", geometry_column="missing")
+ )
+ assert (
+ response.url,
+ response.filename,
+ response.code,
+ response.column,
+ ) == (
+ "",
+ "",
+ "invalid_geometry",
+ "missing",
+ )
+ publish.assert_not_called()
+
+
[email protected]("geopandas")
+def test_missing_pyarrow_is_a_structured_failure(widget: Any) -> None:
+ subject = widget(fixtures.gdf_multi_geometry())
+ with patch.object(DependencyManager.pyarrow, "has", return_value=False):
+ response = subject._download_as(DownloadAsArgs(format="parquet"))
+ assert (response.url, response.code, response.missing_packages) == (
+ "",
+ "missing_packages",
+ ["pyarrow"],
+ )
+
+
+def test_declared_arrow_geometry_is_not_labeled_geoparquet(
+ widget: Any,
+) -> None:
+ response = widget(fixtures.arrow_wkb_known_crs())._download_as(
+ DownloadAsArgs(format="parquet")
+ )
+ assert response.code == "unsupported_representation"
+ assert response.url == ""
+
+
[email protected]("geopandas", "pyarrow")
[email protected]("choice", [None, "geom_b"])
+def test_table_exports_only_selected_rows(choice: str | None) -> None:
+ import pyarrow.parquet as pq
+
+ subject = ui.table(fixtures.gdf_multi_geometry(), selection="multi")
+ subject._convert_value(["1"])
+ artifact, _ = _artifact(subject, choice)
+ table = pq.read_table(io.BytesIO(artifact))
+ assert table.num_rows == 1
+ assert table["geom_b"][0].as_py() == subject.data["geom_b"].iloc[1].wkb
+ geo = json.loads(pq.read_metadata(io.BytesIO(artifact)).metadata[b"geo"])
+ assert geo["primary_column"] == (choice or "geom_a")
+
+
[email protected]("geopandas", "pyarrow")
+def test_dataframe_exports_current_value() -> None:
+ import pyarrow.parquet as pq
+
+ source = fixtures.gdf_multi_geometry()
+ subject = ui.dataframe(source)
+ subject._value = source.iloc[[1]].copy()
+ artifact, _ = _artifact(subject, "geom_b")
+ table = pq.read_table(io.BytesIO(artifact))
+ assert table.num_rows == 1
+ assert table["geom_b"][0].as_py() == source["geom_b"].iloc[1].wkb
+
+
[email protected]("geopandas", "pyarrow")
+def test_dataframe_exports_transformed_geometry_columns() -> None:
+ import pyarrow.parquet as pq
+
+ subject = ui.dataframe(fixtures.gdf_multi_geometry())
+ subject._update(
+ {"transforms": [{"type": "select_columns", "column_ids": ["geom_b"]}]}
+ )
+ artifact, _ = _artifact(subject)
+ reader = pq.ParquetFile(io.BytesIO(artifact))
+ geo = json.loads(reader.metadata.metadata[b"geo"])
+ assert geo["primary_column"] == "geom_b"
+ assert set(geo["columns"]) == {"geom_b"}
+
+
[email protected]("geopandas", "pyarrow")
+def test_stale_choice_is_rejected_after_dataframe_source_changes() -> None:
+ source = fixtures.gdf_multi_geometry()
+ subject = ui.dataframe(source)
+ assert len(subject._get_export_metadata(EmptyArgs()).geometry_columns) == 2
+ subject._value = source.drop(columns=["geom_b"])
+ response = subject._download_as(
+ DownloadAsArgs(format="parquet", geometry_column="geom_b")
+ )
+ assert (response.url, response.code, response.column) == (
+ "",
+ "invalid_geometry",
+ "geom_b",
+ )
+
+
[email protected]("geopandas", "pyarrow")
+def test_malformed_writer_output_publishes_no_artifact(widget: Any) -> None:
+ import geopandas as gpd
+ import pyarrow as pa
+ import pyarrow.parquet as pq
+
+ def write_plain_parquet(_source: Any, path: Any, **_kwargs: Any) -> None:
+ pq.write_table(pa.table({"geom_a": [b"invalid"]}), path)
+
+ subject = widget(fixtures.gdf_multi_geometry())
+ with (
+ patch.object(gpd.GeoDataFrame, "to_parquet", write_plain_parquet),
+ patch(
+ "marimo._plugins.ui._impl.utils.dataframe.mo_data.any_data"
+ ) as publish,
+ ):
+ response = subject._download_as(DownloadAsArgs(format="parquet"))
+ assert (response.url, response.filename, response.code) == (
+ "",
+ "",
+ "invalid_metadata",
+ )
+ publish.assert_not_called()
+
+
[email protected]("geopandas", "pyarrow")
+def test_writer_failure_publishes_no_artifact(widget: Any) -> None:
+ import geopandas as gpd
+
+ subject = widget(fixtures.gdf_multi_geometry())
+ with (
+ patch.object(
+ gpd.GeoDataFrame,
+ "to_parquet",
+ side_effect=ValueError("invalid geometry value"),
+ ),
+ patch(
+ "marimo._plugins.ui._impl.utils.dataframe.mo_data.any_data"
+ ) as publish,
+ ):
+ response = subject._download_as(DownloadAsArgs(format="parquet"))
+ assert (response.url, response.filename, response.code) == (
+ "",
+ "",
+ "conversion_failed",
+ )
+ publish.assert_not_called()
+
+
[email protected]("pandas", "pyarrow")
[email protected]("format_type", ["parquet", "csv"])
+def test_ordinary_formats_reject_a_geometry_choice(
+ widget: Any, format_type: str
+) -> None:
+ import pandas as pd
+
+ subject = widget(pd.DataFrame({"value": [1, 2]}))
+ response = subject._download_as(
+ DownloadAsArgs(format=format_type, geometry_column="value")
+ )
+ assert (response.url, response.code, response.column) == (
+ "",
+ "invalid_geometry",
+ "value",
+ )
+
+
[email protected]("pandas", "pyarrow")
+def test_ordinary_parquet_bytes_remain_unchanged() -> None:
+ import pandas as pd
+
+ subject = ui.dataframe(pd.DataFrame({"value": [1, 2], "name": ["a", "b"]}))
+ expected = subject._get_cached_table_manager(
+ subject._value, None
+ ).to_parquet()
+ artifact, _ = _artifact(subject)
+ assert artifact == expected