Skip to content

Commit

Permalink
@wq/analyst updates
Browse files Browse the repository at this point in the history
 - support overriding field numeric config
 - support passing config via props
 - support templates in id_url_prefix
 - update dependencies
  • Loading branch information
sheppard committed Apr 4, 2024
1 parent 6cd09cf commit a0fb63c
Show file tree
Hide file tree
Showing 9 changed files with 1,909 additions and 1,461 deletions.
3,291 changes: 1,855 additions & 1,436 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"@rollup/plugin-node-resolve": "^15.2.3",
"@rollup/plugin-replace": "^5.0.3",
"@rollup/plugin-terser": "^0.4.4",
"@wq/rollup-plugin": "^2.0.0-beta.1",
"@wq/rollup-plugin": "^2.1.0",
"babel-plugin-direct-import": "^1.0.0",
"babel-plugin-transform-rename-import": "^2.3.0",
"eslint": "^8.51.0",
Expand Down
6 changes: 3 additions & 3 deletions packages/analyst/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@wq/analyst",
"version": "2.0.0-alpha.1",
"version": "2.0.0-alpha.2",
"description": "wq framework plugin providing interactive data tables & charts for Django REST Pandas",
"main": "index.js",
"unpkg": "dist/index.unpkg.js",
Expand Down Expand Up @@ -49,7 +49,7 @@
"@wq/material-web": "*"
},
"devDependencies": {
"@wq/material": "^2.0.0-alpha.1",
"@wq/material-web": "^2.0.0-alpha.2"
"@wq/material": "^2.1.0",
"@wq/material-web": "^2.1.0"
}
}
12 changes: 7 additions & 5 deletions packages/analyst/src/components/AnalystTable.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import PropTypes from "prop-types";
export default function AnalystTable({
data: initialData,
url,
fields,
initial_rows,
initial_order,
id_column,
id_url_prefix,
compact,
}) {
const [data, error] = useAnalystData(url, initialData),
const [data, error] = useAnalystData(url, initialData, fields),
[columns, setColumns] = useState(),
[filters, setFilters] = useState({}),
[orders, setOrders] = useState(initial_order || {}),
Expand Down Expand Up @@ -223,8 +224,8 @@ export default function AnalystTable({
orders[column.name] === "desc"
? "sort-desc"
: orders[column.name]
? "sort-asc"
: "sort-none"
? "sort-asc"
: "sort-none"
}
color={orders[column.name] && "secondary"}
onClick={() => toggleOrder(column.name)}
Expand Down Expand Up @@ -277,8 +278,8 @@ export default function AnalystTable({
{orders[column.name] === "desc"
? " ↓"
: orders[column.name]
? " ↑"
: ""}
? " ↑"
: ""}
</TableTitle>
);
}
Expand Down Expand Up @@ -379,6 +380,7 @@ export default function AnalystTable({
AnalystTable.propTypes = {
data: PropTypes.arrayOf(PropTypes.object),
url: PropTypes.string,
fields: PropTypes.arrayOf(PropTypes.object),
initial_rows: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
compact: PropTypes.bool,
initial_order: PropTypes.object,
Expand Down
28 changes: 19 additions & 9 deletions packages/analyst/src/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import { get as getPandasCsv } from "@wq/pandas";
import Mustache from "mustache";
import { labelWithIcon } from "./components/Icon.jsx";

export function useAnalyst() {
const config = useAnalystConfig(),
export function useAnalyst(props) {
const config = useAnalystConfig(props),
[data, dataError] = useAnalystData(
config.url || null,
config.data || null,
config.fields || null,
),
modes = useAnalystModes(data, config),
[form, options, setOptions] = useAnalystForm(modes);
Expand All @@ -24,16 +25,24 @@ export function useAnalyst() {
};
}

export function useAnalystConfig() {
export function useAnalystConfig(props) {
const {
page_config: { name, analyst = {} },
page_config: { name, analyst: routeProps },
} = useRouteInfo(),
context = useRenderContext();

const analyst = { ...routeProps, ...props };

if (!analyst.url) {
return {
error: `The config for "${name}" should include an analyst.url property.`,
};
if (routeProps) {
return {
error: `The config for "${name}" should include an analyst.url property.`,
};
} else {
return {
error: `Specify analyst.url in the config for "${name}" or as a prop.`,
};
}
}

return {
Expand All @@ -44,6 +53,7 @@ export function useAnalystConfig() {
: analyst.initial_order,
url: render(analyst.url, context),
title: render(analyst.title, context),
id_url_prefix: render(analyst.id_url_prefix, context),
};
}

Expand All @@ -55,7 +65,7 @@ function render(value, context) {
}
}

export function useAnalystData(url, initialData) {
export function useAnalystData(url, initialData, fields) {
const [data, setData] = useState(initialData),
[error, setError] = useState(null);

Expand All @@ -65,7 +75,7 @@ export function useAnalystData(url, initialData) {
}
async function loadData() {
try {
const data = await getPandasCsv(url, { flatten: true });
const data = await getPandasCsv(url, { flatten: true, fields });
if (data && data.length > 0) {
setData(data);
} else {
Expand Down
2 changes: 1 addition & 1 deletion packages/analyst/src/version.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default "2.0.0a1";
export default "2.0.0a2";
6 changes: 4 additions & 2 deletions packages/analyst/src/views/Analyst.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import { Series, Scatter, Boxplot } from "@wq/chart";
import { useAnalyst } from "../hooks.js";
import PropTypes from "prop-types";

export default function Analyst({ children }) {
export default function Analyst({ children, ...props }) {
const { View, Typography, AnalystDownload, AnalystTable, AnalystForm } =
useComponents(),
{
url,
data,
fields,
error,
title,
formats,
Expand All @@ -20,7 +21,7 @@ export default function Analyst({ children }) {
form,
options,
setOptions,
} = useAnalyst();
} = useAnalyst(props);

if (error) {
return (
Expand Down Expand Up @@ -80,6 +81,7 @@ export default function Analyst({ children }) {
(options.mode === "table" && (
<AnalystTable
data={data}
fields={fields}
initial_rows={initial_rows}
initial_order={initial_order}
id_column={id_column}
Expand Down
2 changes: 1 addition & 1 deletion packages/chart/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@wq/chart",
"version": "2.0.0-alpha.1",
"version": "2.0.0-alpha.2",
"description": "wq framework plugin providing Plotly charts for Django REST Pandas",
"main": "index.js",
"unpkg": "dist/index.unpkg.js",
Expand Down
21 changes: 18 additions & 3 deletions packages/pandas/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ export function parse(str, options = {}) {
col2dataset = [],
data,
valuesHeader,
rows;
rows,
fields = { ...options.fields };
if (str.charAt(0) != ",") {
// Assume plain CSV (single series with one-row header)
data = [];
Expand All @@ -60,7 +61,7 @@ export function parse(str, options = {}) {
for (key in row) {
val = row[key];
if (row[key] !== "") {
row[key] = isNaN(+val) ? val : +val;
row[key] = parseVal(val, key);
}
}
data.push(row);
Expand Down Expand Up @@ -118,6 +119,20 @@ export function parse(str, options = {}) {
idColumns = row.slice(0, row.indexOf(""));
}

function parseVal(val, name) {
if (!fields[name]) {
fields[name] = {};
}
const field = fields[name];
if (field.numeric === true) {
return +val;
} else if (field.numeric === false) {
return val;
} else {
return isNaN(+val) ? val : +val;
}
}

function findDatasets() {
// Ensure that datasets[] has only one entry for each dataset, as
// datasets that may span multiple columns. Hash the metadata values
Expand Down Expand Up @@ -174,7 +189,7 @@ export function parse(str, options = {}) {
}
rowdata[dsi] = item;
}
item[valname] = isNaN(+d) ? d : +d;
item[valname] = parseVal(d, valname);
});
rowdata.forEach(function (d, i) {
datasets[i].data.push(d);
Expand Down

0 comments on commit a0fb63c

Please sign in to comment.