Skip to content

Commit a0fb63c

Browse files
committed
@wq/analyst updates
- support overriding field numeric config - support passing config via props - support templates in id_url_prefix - update dependencies
1 parent 6cd09cf commit a0fb63c

File tree

9 files changed

+1909
-1461
lines changed

9 files changed

+1909
-1461
lines changed

package-lock.json

+1,855-1,436
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"@rollup/plugin-node-resolve": "^15.2.3",
2222
"@rollup/plugin-replace": "^5.0.3",
2323
"@rollup/plugin-terser": "^0.4.4",
24-
"@wq/rollup-plugin": "^2.0.0-beta.1",
24+
"@wq/rollup-plugin": "^2.1.0",
2525
"babel-plugin-direct-import": "^1.0.0",
2626
"babel-plugin-transform-rename-import": "^2.3.0",
2727
"eslint": "^8.51.0",

packages/analyst/package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@wq/analyst",
3-
"version": "2.0.0-alpha.1",
3+
"version": "2.0.0-alpha.2",
44
"description": "wq framework plugin providing interactive data tables & charts for Django REST Pandas",
55
"main": "index.js",
66
"unpkg": "dist/index.unpkg.js",
@@ -49,7 +49,7 @@
4949
"@wq/material-web": "*"
5050
},
5151
"devDependencies": {
52-
"@wq/material": "^2.0.0-alpha.1",
53-
"@wq/material-web": "^2.0.0-alpha.2"
52+
"@wq/material": "^2.1.0",
53+
"@wq/material-web": "^2.1.0"
5454
}
5555
}

packages/analyst/src/components/AnalystTable.jsx

+7-5
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ import PropTypes from "prop-types";
77
export default function AnalystTable({
88
data: initialData,
99
url,
10+
fields,
1011
initial_rows,
1112
initial_order,
1213
id_column,
1314
id_url_prefix,
1415
compact,
1516
}) {
16-
const [data, error] = useAnalystData(url, initialData),
17+
const [data, error] = useAnalystData(url, initialData, fields),
1718
[columns, setColumns] = useState(),
1819
[filters, setFilters] = useState({}),
1920
[orders, setOrders] = useState(initial_order || {}),
@@ -223,8 +224,8 @@ export default function AnalystTable({
223224
orders[column.name] === "desc"
224225
? "sort-desc"
225226
: orders[column.name]
226-
? "sort-asc"
227-
: "sort-none"
227+
? "sort-asc"
228+
: "sort-none"
228229
}
229230
color={orders[column.name] && "secondary"}
230231
onClick={() => toggleOrder(column.name)}
@@ -277,8 +278,8 @@ export default function AnalystTable({
277278
{orders[column.name] === "desc"
278279
? " ↓"
279280
: orders[column.name]
280-
? " ↑"
281-
: ""}
281+
? " ↑"
282+
: ""}
282283
</TableTitle>
283284
);
284285
}
@@ -379,6 +380,7 @@ export default function AnalystTable({
379380
AnalystTable.propTypes = {
380381
data: PropTypes.arrayOf(PropTypes.object),
381382
url: PropTypes.string,
383+
fields: PropTypes.arrayOf(PropTypes.object),
382384
initial_rows: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
383385
compact: PropTypes.bool,
384386
initial_order: PropTypes.object,

packages/analyst/src/hooks.js

+19-9
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ import { get as getPandasCsv } from "@wq/pandas";
44
import Mustache from "mustache";
55
import { labelWithIcon } from "./components/Icon.jsx";
66

7-
export function useAnalyst() {
8-
const config = useAnalystConfig(),
7+
export function useAnalyst(props) {
8+
const config = useAnalystConfig(props),
99
[data, dataError] = useAnalystData(
1010
config.url || null,
1111
config.data || null,
12+
config.fields || null,
1213
),
1314
modes = useAnalystModes(data, config),
1415
[form, options, setOptions] = useAnalystForm(modes);
@@ -24,16 +25,24 @@ export function useAnalyst() {
2425
};
2526
}
2627

27-
export function useAnalystConfig() {
28+
export function useAnalystConfig(props) {
2829
const {
29-
page_config: { name, analyst = {} },
30+
page_config: { name, analyst: routeProps },
3031
} = useRouteInfo(),
3132
context = useRenderContext();
3233

34+
const analyst = { ...routeProps, ...props };
35+
3336
if (!analyst.url) {
34-
return {
35-
error: `The config for "${name}" should include an analyst.url property.`,
36-
};
37+
if (routeProps) {
38+
return {
39+
error: `The config for "${name}" should include an analyst.url property.`,
40+
};
41+
} else {
42+
return {
43+
error: `Specify analyst.url in the config for "${name}" or as a prop.`,
44+
};
45+
}
3746
}
3847

3948
return {
@@ -44,6 +53,7 @@ export function useAnalystConfig() {
4453
: analyst.initial_order,
4554
url: render(analyst.url, context),
4655
title: render(analyst.title, context),
56+
id_url_prefix: render(analyst.id_url_prefix, context),
4757
};
4858
}
4959

@@ -55,7 +65,7 @@ function render(value, context) {
5565
}
5666
}
5767

58-
export function useAnalystData(url, initialData) {
68+
export function useAnalystData(url, initialData, fields) {
5969
const [data, setData] = useState(initialData),
6070
[error, setError] = useState(null);
6171

@@ -65,7 +75,7 @@ export function useAnalystData(url, initialData) {
6575
}
6676
async function loadData() {
6777
try {
68-
const data = await getPandasCsv(url, { flatten: true });
78+
const data = await getPandasCsv(url, { flatten: true, fields });
6979
if (data && data.length > 0) {
7080
setData(data);
7181
} else {

packages/analyst/src/version.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export default "2.0.0a1";
1+
export default "2.0.0a2";

packages/analyst/src/views/Analyst.jsx

+4-2
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ import { Series, Scatter, Boxplot } from "@wq/chart";
44
import { useAnalyst } from "../hooks.js";
55
import PropTypes from "prop-types";
66

7-
export default function Analyst({ children }) {
7+
export default function Analyst({ children, ...props }) {
88
const { View, Typography, AnalystDownload, AnalystTable, AnalystForm } =
99
useComponents(),
1010
{
1111
url,
1212
data,
13+
fields,
1314
error,
1415
title,
1516
formats,
@@ -20,7 +21,7 @@ export default function Analyst({ children }) {
2021
form,
2122
options,
2223
setOptions,
23-
} = useAnalyst();
24+
} = useAnalyst(props);
2425

2526
if (error) {
2627
return (
@@ -80,6 +81,7 @@ export default function Analyst({ children }) {
8081
(options.mode === "table" && (
8182
<AnalystTable
8283
data={data}
84+
fields={fields}
8385
initial_rows={initial_rows}
8486
initial_order={initial_order}
8587
id_column={id_column}

packages/chart/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@wq/chart",
3-
"version": "2.0.0-alpha.1",
3+
"version": "2.0.0-alpha.2",
44
"description": "wq framework plugin providing Plotly charts for Django REST Pandas",
55
"main": "index.js",
66
"unpkg": "dist/index.unpkg.js",

packages/pandas/src/index.js

+18-3
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ export function parse(str, options = {}) {
5151
col2dataset = [],
5252
data,
5353
valuesHeader,
54-
rows;
54+
rows,
55+
fields = { ...options.fields };
5556
if (str.charAt(0) != ",") {
5657
// Assume plain CSV (single series with one-row header)
5758
data = [];
@@ -60,7 +61,7 @@ export function parse(str, options = {}) {
6061
for (key in row) {
6162
val = row[key];
6263
if (row[key] !== "") {
63-
row[key] = isNaN(+val) ? val : +val;
64+
row[key] = parseVal(val, key);
6465
}
6566
}
6667
data.push(row);
@@ -118,6 +119,20 @@ export function parse(str, options = {}) {
118119
idColumns = row.slice(0, row.indexOf(""));
119120
}
120121

122+
function parseVal(val, name) {
123+
if (!fields[name]) {
124+
fields[name] = {};
125+
}
126+
const field = fields[name];
127+
if (field.numeric === true) {
128+
return +val;
129+
} else if (field.numeric === false) {
130+
return val;
131+
} else {
132+
return isNaN(+val) ? val : +val;
133+
}
134+
}
135+
121136
function findDatasets() {
122137
// Ensure that datasets[] has only one entry for each dataset, as
123138
// datasets that may span multiple columns. Hash the metadata values
@@ -174,7 +189,7 @@ export function parse(str, options = {}) {
174189
}
175190
rowdata[dsi] = item;
176191
}
177-
item[valname] = isNaN(+d) ? d : +d;
192+
item[valname] = parseVal(d, valname);
178193
});
179194
rowdata.forEach(function (d, i) {
180195
datasets[i].data.push(d);

0 commit comments

Comments
 (0)