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.

format CellSelectionStats (proper aggregation, 3 decimal places) (#8564)

Shahmir Varqhae9966f77e821
diff --git a/frontend/src/components/data-table/range-focus/__tests__/cell-selection-stats.test.tsx b/frontend/src/components/data-table/range-focus/__tests__/cell-selection-stats.test.tsx
index d2d6543bde..9d3a8c36d3 100644
--- a/frontend/src/components/data-table/range-focus/__tests__/cell-selection-stats.test.tsx
+++ b/frontend/src/components/data-table/range-focus/__tests__/cell-selection-stats.test.tsx
@@ -3,6 +3,7 @@
import type { Table } from "@tanstack/react-table";
import { render, screen } from "@testing-library/react";
import { useEffect } from "react";
+import { I18nProvider } from "react-aria";
import { describe, expect, it } from "vitest";
import { SELECT_COLUMN_ID } from "../../types";
import { useCellSelectionReducerActions } from "../atoms";
@@ -21,7 +22,11 @@ const TestHarness = ({
useEffect(() => {
actions.setSelectedCells(selectedCellIds);
}, [actions, selectedCellIds]);
- return <CellSelectionStats table={table} />;
+ return (
+ <I18nProvider locale="en-US">
+ <CellSelectionStats table={table} />
+ </I18nProvider>
+ );
};
describe("CellSelectionStats", () => {
@@ -107,7 +112,7 @@ describe("CellSelectionStats", () => {
expect(screen.queryByText(/Average:/)).not.toBeInTheDocument();
});
- it("should round sum and average to 8 decimal places", () => {
+ it("should round sum and average to 3 decimal places", () => {
const row = createMockRow("0", [
createMockCell("0_0", 0.112_233_441_1),
createMockCell("0_1", 0.112_233_441_1),
@@ -120,14 +125,14 @@ describe("CellSelectionStats", () => {
</CellSelectionProvider>,
);
- expect(screen.getByText("Sum: 0.22446688")).toBeInTheDocument(); // Round 0.2244668866 to 8 decimal places
- expect(screen.getByText("Average: 0.11223344")).toBeInTheDocument(); // Round 0.1122334411 to 8 decimal places
+ expect(screen.getByText("Sum: 0.224")).toBeInTheDocument();
+ expect(screen.getByText("Average: 0.112")).toBeInTheDocument();
});
- it("should correctly round sum and average to 8 decimal places", () => {
+ it("should correctly round sum and average to 3 decimal places", () => {
const row = createMockRow("0", [
- createMockCell("0_0", 0.112_233_443_3),
- createMockCell("0_1", 0.112_233_443_3),
+ createMockCell("0_0", 0.112_833_443_3),
+ createMockCell("0_1", 0.112_833_443_3),
]);
const table = createMockTable([row], []);
@@ -137,8 +142,8 @@ describe("CellSelectionStats", () => {
</CellSelectionProvider>,
);
- expect(screen.getByText("Sum: 0.22446689")).toBeInTheDocument(); // Round 0.2244668866 to 8 decimal places
- expect(screen.getByText("Average: 0.11223344")).toBeInTheDocument(); // Round 0.1122334433 to 8 decimal places
+ expect(screen.getByText("Sum: 0.226")).toBeInTheDocument();
+ expect(screen.getByText("Average: 0.113")).toBeInTheDocument();
});
it("should not add extra decimal places to sum and average", () => {
diff --git a/frontend/src/components/data-table/range-focus/cell-selection-stats.tsx b/frontend/src/components/data-table/range-focus/cell-selection-stats.tsx
index 07d8f7fa7a..0b5c14bcb2 100644
--- a/frontend/src/components/data-table/range-focus/cell-selection-stats.tsx
+++ b/frontend/src/components/data-table/range-focus/cell-selection-stats.tsx
@@ -2,6 +2,7 @@
import type { Table } from "@tanstack/react-table";
import { useAtomValue } from "jotai";
+import { useLocale } from "react-aria";
import { cn } from "@/utils/cn";
import { selectedCellsAtom } from "./atoms";
import {
@@ -9,6 +10,9 @@ import {
getNumericValuesFromSelectedCells,
} from "./utils";
+// Offers a good default for most use cases.
+const MAX_FRACTION_DIGITS = 3;
+
/**
* Displays summary stats (Count, Sum, Average) for the current cell selection.
* Renders only when 2+ data cells are selected (checkbox column excluded).
@@ -22,6 +26,7 @@ export const CellSelectionStats = <TData,>({
table: Table<TData>;
className?: string;
}) => {
+ const { locale } = useLocale();
const selectedCells = useAtomValue(selectedCellsAtom);
const dataCellCount = countDataCellsInSelection(selectedCells);
@@ -38,42 +43,68 @@ export const CellSelectionStats = <TData,>({
className,
)}
>
- <CountStat count={dataCellCount} />
- <SumStat numericValues={numericValues} />
- <AverageStat numericValues={numericValues} />
+ <CountStat count={dataCellCount} locale={locale} />
+ <SumStat numericValues={numericValues} locale={locale} />
+ <AverageStat numericValues={numericValues} locale={locale} />
</div>
);
};
-const StatSpan = (statName: string, statValue: number) => {
+const formatNumber = (value: number, locale: string): string => {
+ return value.toLocaleString(locale, {
+ maximumFractionDigits: MAX_FRACTION_DIGITS,
+ });
+};
+
+const StatSpan = ({
+ name,
+ value,
+ locale,
+}: {
+ name: string;
+ value: number;
+ locale: string;
+}) => {
return (
<span>
- {statName}: {statValue}
+ {name}: {formatNumber(value, locale)}
</span>
);
};
-const CountStat = ({ count }: { count: number }) => {
- return StatSpan("Count", count);
+const CountStat = ({ count, locale }: { count: number; locale: string }) => {
+ return <StatSpan name="Count" value={count} locale={locale} />;
};
-const SumStat = ({ numericValues }: { numericValues: number[] }) => {
+const SumStat = ({
+ numericValues,
+ locale,
+}: {
+ numericValues: number[];
+ locale: string;
+}) => {
if (numericValues.length === 0) {
return null;
}
const sum = numericValues.reduce((acc, n) => acc + n, 0);
- const sumRounded = Number(sum.toFixed(8));
- return StatSpan("Sum", sumRounded);
+ const sumRounded = Number(sum.toFixed(MAX_FRACTION_DIGITS));
+ return <StatSpan name="Sum" value={sumRounded} locale={locale} />;
};
-const AverageStat = ({ numericValues }: { numericValues: number[] }) => {
+const AverageStat = ({
+ numericValues,
+ locale,
+}: {
+ numericValues: number[];
+ locale: string;
+}) => {
if (numericValues.length === 0) {
return null;
}
const average =
numericValues.reduce((acc, n) => acc + n, 0) / numericValues.length;
- const averageRounded = Number(average.toFixed(8));
- return StatSpan("Average", averageRounded);
+ const averageRounded = Number(average.toFixed(MAX_FRACTION_DIGITS));
+ return <StatSpan name="Average" value={averageRounded} locale={locale} />;
};