Skip to content

CSV export

Because grid cells are aggregates, “export” means three different things — and the engine serializes all of them so the CSV always matches what the math produced. Export the raw source rows behind a selection, the aggregated roll-up tree exactly as shown (filter + sort + top-N applied), or a pivot cross-tab flattened to columns. Values ship as raw numerics (machine-readable) by default, or formatted ($420.00, 45%) to match the display. The built-in Export button below emits the selection-or-all CSV.

from dash import dcc, html, Input, Output, callback
from dash_tensor_grid import build_grid
from dash_tensor_grid.engine import GridDataEngine
engine = GridDataEngine(sales)
payload = build_grid(sales, {"rows": ["region", "country"], "measures": {"revenue": "sum"}})
layout = html.Div([
# ...TensorGrid(...),
html.Button("Export", id="export"),
dcc.Download(id="download"),
])
@callback(Output("download", "data"), Input("export", "n_clicks"), prevent_initial_call=True)
def export(_):
csv = engine.to_csv() # SOURCE rows (raw frame)
# csv = engine.tree_to_csv(payload["row_data"], # AGGREGATED tree as shown
# ["region", "country"], ["revenue"])
# pv = build_grid(sales, {"rows": ["region"], "columns": ["category"],
# "measures": {"revenue": "sum"}})
# csv = engine.pivot_to_csv(pv, ["region"]) # PIVOT cross-tab, flattened
return dcc.send_string(csv, "export.csv")
  • Source vs aggregated vs pivot. exportMode: 'source' (default) emits the raw underlying rows under the selection, or all rows if none is selected (core rowsForSelection + toCsv, engine to_csv). 'aggregated' emits the roll-up tree as shown — filter, sort, and top-N applied (core treeToCsv, engine tree_to_csv). A pivot flattens its cross-tab to one column per row dimension plus one per measure leaf (core pivotToCsv, engine pivot_to_csv).
  • exportFormatted — with exportMode: 'aggregated', render each measure through its formatStr ($420.00, 45%) instead of raw numerics. Ignored for 'source' export.
  • The engine owns serialization. The JS toCsv / treeToCsv / pivotToCsv are parity-verified ports of Polars write_csv (RFC-4180 quoting, null → empty field), so a Dash export and a React/Vue/Vanilla export of the same view are byte-identical.
  • Combine with aggregation or a pivot to decide what the exported view contains, and turn on exportFormatted when the CSV should read like the on-screen table.