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.

improvement: sort tables in dataframe by cell order (#1723)

Myles Scolnick48dace024d43
diff --git a/frontend/src/components/editor/chrome/panels/datasources-panel.tsx b/frontend/src/components/editor/chrome/panels/datasources-panel.tsx
index 72c6ccee11..160fd9341d 100644
--- a/frontend/src/components/editor/chrome/panels/datasources-panel.tsx
+++ b/frontend/src/components/editor/chrome/panels/datasources-panel.tsx
@@ -10,12 +10,16 @@ import { Command, CommandInput, CommandItem } from "@/components/ui/command";
import { CommandList } from "cmdk";
import { cn } from "@/utils/cn";
-import { useDatasets, useDatasetsActions } from "@/core/datasets/state";
+import {
+ datasetTablesAtom,
+ useDatasets,
+ useDatasetsActions,
+} from "@/core/datasets/state";
import { DATA_TYPE_ICON } from "@/components/datasets/icons";
import { Button } from "@/components/ui/button";
-import { useCellActions } from "@/core/cells/cells";
+import { cellIdsAtom, useCellActions } from "@/core/cells/cells";
import { lastFocusedCellIdAtom } from "@/core/cells/focus";
-import { useAtomValue } from "jotai";
+import { atom, useAtomValue } from "jotai";
import { Tooltip } from "@/components/ui/tooltip";
import { PanelEmptyState } from "./empty-state";
import { previewDatasetColumn } from "@/core/network/requests";
@@ -36,16 +40,44 @@ import {
DataTable,
DataTableColumn,
} from "@/core/kernel/messages";
+import { variablesAtom } from "@/core/variables/state";
+import { sortBy } from "lodash-es";
+
+const sortedTablesAtom = atom((get) => {
+ const tables = get(datasetTablesAtom);
+ const variables = get(variablesAtom);
+ const cellIds = get(cellIdsAtom);
+
+ // Sort tables by the index of the variable they are defined in
+ return sortBy(tables, (table) => {
+ // Put at the top
+ if (!table.variable_name) {
+ return -1;
+ }
+ const variable = Object.values(variables).find(
+ (v) => v.name === table.variable_name,
+ );
+ if (!variable) {
+ return 0;
+ }
+
+ const index = cellIds.indexOf(variable.declaredBy[0]);
+ if (index === -1) {
+ return 0;
+ }
+ return index;
+ });
+});
export const DataSourcesPanel: React.FC = () => {
const [searchValue, setSearchValue] = React.useState<string>("");
const autoInstantiate = useAtomValue(autoInstantiateAtom);
const lastFocusedCellId = useAtomValue(lastFocusedCellIdAtom);
- const { tables, expandedColumns, expandedTables, columnsPreviews } =
- useDatasets();
+ const { expandedColumns, expandedTables, columnsPreviews } = useDatasets();
const { toggleTable, toggleColumn, closeAllColumns } = useDatasetsActions();
const { createNewCell } = useCellActions();
+ const tables = useAtomValue(sortedTablesAtom);
if (tables.length === 0) {
return (
diff --git a/frontend/src/core/datasets/state.ts b/frontend/src/core/datasets/state.ts
index b51c251721..c8fbc07cda 100644
--- a/frontend/src/core/datasets/state.ts
+++ b/frontend/src/core/datasets/state.ts
@@ -2,7 +2,7 @@
import { createReducerAndAtoms } from "@/utils/createReducer";
import type { DatasetsState } from "./types";
-import { useAtomValue } from "jotai";
+import { atom, useAtomValue } from "jotai";
import { VariableName } from "../variables/types";
import { DataColumnPreview } from "../kernel/messages";
import { previewDatasetColumn } from "../network/requests";
@@ -105,6 +105,8 @@ const {
*/
export const useDatasets = () => useAtomValue(datasetsAtom);
+export const datasetTablesAtom = atom((get) => get(datasetsAtom).tables);
+
/**
* React hook to get the datasets actions.
*/