Skip to content

Commit

Permalink
Merge pull request #2341 from quantified-uncertainty/add-title-to-cal…
Browse files Browse the repository at this point in the history
…culator

Add title to calculator and table, similar to plots
  • Loading branch information
OAGr authored Oct 18, 2023
2 parents 0c3efb1 + f60b2c4 commit 20b52b9
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 51 deletions.
18 changes: 13 additions & 5 deletions packages/components/src/components/Calculator/calculatorUI.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,21 @@ export const CalculatorUI: FC<UIProps> = ({
chartHeight: 200,
};

const hasTitleOrDescription = !!calculator.title || !!calculator.description;
return (
<div className="border border-slate-200 rounded-sm max-w-4xl mx-auto">
{calculator.description && (
<div className="py-3 px-5 border-b border-slate-200 bg-slate-100">
<ReactMarkdown className={"prose text-sm text-slate-800 max-w-4xl"}>
{calculator.description}
</ReactMarkdown>
{hasTitleOrDescription && (
<div className="py-3 px-5 border-b border-slate-200 bg-slate-100 max-w-4xl">
{calculator.title && (
<div className={"text-lg text-slate-900 font-semibold mb-1"}>
{calculator.title}
</div>
)}
{calculator.description && (
<ReactMarkdown className={"prose text-sm text-slate-700"}>
{calculator.description}
</ReactMarkdown>
)}
</div>
)}

Expand Down
89 changes: 48 additions & 41 deletions packages/components/src/components/TableChart/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,47 +59,54 @@ export const TableChart: FC<Props> = ({
};

return (
<div className="relative rounded-sm overflow-hidden border border-slate-200">
<table
className="table-fixed w-full"
style={{ minWidth: columnLength * 100 }}
>
{hasColumnNames && (
<thead className="text-xs text-gray-700 bg-gray-50 border-b border-slate-200">
<tr>
{Array.from({ length: columnLength }, (_, i) => (
<th
key={i}
scope="col"
className={clsx(
"px-2 py-2",
i !== 0 && "border-slate-200 border-l"
)}
>
{columnNames[i] || ""}
</th>
))}
</tr>
</thead>
)}
<tbody>
{rowsAndColumns.map((row, i) => (
<tr key={i} className="border-b border-slate-100">
{row.map((item, k) => (
<td
key={k}
className={clsx(
"px-1 overflow-hidden",
k !== 0 && "border-slate-100 border-l"
)}
>
{showItem(item, adjustedSettings)}
</td>
))}
</tr>
))}
</tbody>
</table>
<div>
{!!value.title && (
<div className="text-md text-slate-900 font-semibold mb-1">
{value.title}
</div>
)}
<div className="relative rounded-sm overflow-hidden border border-slate-200">
<table
className="table-fixed w-full"
style={{ minWidth: columnLength * 100 }}
>
{hasColumnNames && (
<thead className="text-xs text-gray-700 bg-gray-50 border-b border-slate-200">
<tr>
{Array.from({ length: columnLength }, (_, i) => (
<th
key={i}
scope="col"
className={clsx(
"px-2 py-2",
i !== 0 && "border-slate-200 border-l"
)}
>
{columnNames[i] || ""}
</th>
))}
</tr>
</thead>
)}
<tbody>
{rowsAndColumns.map((row, i) => (
<tr key={i} className="border-b border-slate-100">
{row.map((item, k) => (
<td
key={k}
className={clsx(
"px-1 overflow-hidden",
k !== 0 && "border-slate-100 border-l"
)}
>
{showItem(item, adjustedSettings)}
</td>
))}
</tr>
))}
</tbody>
</table>
</div>{" "}
</div>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ export const Basic: Story = {
code: `
f(a, b, c) = [a + b, a, c]
a = "\n## My favorite calculator\nA longer description of the calculator goes here...\n"
a = "A longer description of the calculator goes here...\n"
Calculator.make(
{
fn: f,
title: "My Calculator",
description: a,
fields: [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const Basic: Story = {
code: `
Table.make(
{
title: "Basic Table",
data: [1, 4, 5],
columns: [
{ fn: {|e|e}, name: "Number" },
Expand Down
4 changes: 3 additions & 1 deletion packages/squiggle-lang/src/fr/calculator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export const library = [
[
frDict(
["fn", frLambda],
["title", frOptional(frString)],
["description", frOptional(frString)],
[
"fields",
Expand All @@ -48,9 +49,10 @@ export const library = [
]
),
],
([{ fn, description, fields }]) => {
([{ fn, title, description, fields }]) => {
const calc = vCalculator({
fn,
title: title || undefined,
description: description || undefined,
fields: fields.map((vars) => ({
name: vars.name,
Expand Down
4 changes: 3 additions & 1 deletion packages/squiggle-lang/src/fr/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,17 @@ export const library = [
[
frDict(
["data", frArray(frAny)],
["title", frOptional(frString)],
[
"columns",
frArray(frDict(["fn", frLambda], ["name", frOptional(frString)])),
]
),
],
([{ data, columns }]) => {
([{ data, title, columns }]) => {
return vTableChart({
data,
title: title || undefined,
columns: columns.map(({ fn, name }) => ({
fn,
name: name ?? undefined,
Expand Down
4 changes: 4 additions & 0 deletions packages/squiggle-lang/src/public/SqValue/SqCalculator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ export class SqCalculator {
}
}

get title(): string | undefined {
return this._value.title;
}

get description(): string | undefined {
return this._value.description;
}
Expand Down
4 changes: 4 additions & 0 deletions packages/squiggle-lang/src/public/SqValue/SqTableChart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,8 @@ export class SqTableChart {
get columnNames(): (string | undefined)[] {
return this._value.columns.map(({ name }) => name);
}

get title(): string | undefined {
return this._value.title;
}
}
2 changes: 2 additions & 0 deletions packages/squiggle-lang/src/value/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,7 @@ export type Plot = CommonPlotArgs &

export type TableChart = {
data: Value[];
title?: string;
columns: { fn: Lambda; name: string | undefined }[];
};
class VTableChart extends BaseValue {
Expand All @@ -437,6 +438,7 @@ export type Calculator = {
fn: Lambda;
fields: { name: string; default: string; description?: string }[];
description?: string;
title?: string;
};

class VCalculator extends BaseValue {
Expand Down
6 changes: 4 additions & 2 deletions packages/website/src/pages/docs/Api/Calculator.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ Calculators can be useful for debugging functions or to present functions to end
```
Calculator.make: ({
fn: ...arguments => any,
description: string,
title?: string,
description?: string,
fields: list<{
name: string,
default?: string | number,
Expand All @@ -32,7 +33,8 @@ Examples:
defaultCode={`Calculator.make(
{
fn: {|a, b|a + b},
description: "### Sum() \nThis takes in two arguments, and outputs the sum of those two arguments.",
title: "Sum()"
description: "This takes in two arguments, and outputs the sum of those two arguments.",
fields: [
{
name: "First Param",
Expand Down
1 change: 1 addition & 0 deletions packages/website/src/pages/docs/Api/Table.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ The Table module allows you to make tables of data.
```
Table.make: ({
data: list<'a>,
title?: string,
columns: list<{
name?:string,
fn: 'a => any
Expand Down

3 comments on commit 20b52b9

@vercel
Copy link

@vercel vercel bot commented on 20b52b9 Oct 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 20b52b9 Oct 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 20b52b9 Oct 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.