Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Metric format converter #1321

Merged
merged 23 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
6c12783
Adding custom converter to convert sprintf-js to d3format for plotly
namnguyen20999 May 26, 2024
9b4b1a8
create a re-usable file for format converter
namnguyen20999 May 28, 2024
2a7e04b
adding unit test
namnguyen20999 May 29, 2024
7643c5a
Merge branch 'develop' into 1297-value-format
namnguyen20999 May 29, 2024
2a9876b
fix test case name
namnguyen20999 May 29, 2024
cdcd954
Merge branch '1297-value-format' of github.com:Avaiga/taipy into 1297…
namnguyen20999 May 29, 2024
95c19d1
refactoring the format & delta_format
namnguyen20999 May 29, 2024
d03df5d
Merge branch 'develop' into 1297-value-format
namnguyen20999 May 29, 2024
f762e9d
adding edge test case
namnguyen20999 May 30, 2024
32b0604
Merge branch '1297-value-format' of github.com:Avaiga/taipy into 1297…
namnguyen20999 May 30, 2024
68ca3e2
Merge branch 'develop' into 1297-value-format
namnguyen20999 May 30, 2024
9fe190a
Merge branch 'develop' into 1297-value-format
namnguyen20999 May 31, 2024
a9d3dd8
refactor code base on reviews
namnguyen20999 Jun 2, 2024
aaa2323
Merge branch 'develop' into 1297-value-format
namnguyen20999 Jun 2, 2024
6bfe97d
resolve comments
namnguyen20999 Jun 3, 2024
762cae3
resolve comments
namnguyen20999 Jun 3, 2024
c2798cc
Delete frontend/taipy-gui/public/stylekit/controls/metric.css
namnguyen20999 Jun 3, 2024
181c0be
Merge branch 'develop' into 1297-value-format
namnguyen20999 Jun 4, 2024
3693ce6
update to camelCase
namnguyen20999 Jun 4, 2024
678fa7f
update to camelCase
namnguyen20999 Jun 4, 2024
cdc0834
refactor code base on comments
namnguyen20999 Jun 4, 2024
32ade6d
Merge branch 'develop' into 1297-value-format
namnguyen20999 Jun 4, 2024
4436b1a
Merge branch 'develop' into 1297-value-format
namnguyen20999 Jun 4, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions frontend/taipy-gui/src/components/Taipy/Metric.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ interface MetricProps extends TaipyBaseProps, TaipyHoverProps {
height?: string | number;
showValue?: boolean;
format?: string;
formatDelta?: string;
deltaFormat?: string;
}

const emptyLayout = {} as Record<string, Record<string, unknown>>;
Expand Down Expand Up @@ -73,9 +73,9 @@ const Metric = (props: MetricProps) => {
},
delta: {
reference: typeof value === 'number' && typeof delta === 'number' ? value - delta : undefined,
prefix: extractPrefix(props.formatDelta),
suffix: extractSuffix(props.formatDelta),
valueformat: extractFormatSpecifier(props.formatDelta)
prefix: extractPrefix(props.deltaFormat),
suffix: extractSuffix(props.deltaFormat),
valueformat: extractFormatSpecifier(props.deltaFormat)
},
gauge: {
axis: {
Expand All @@ -95,7 +95,7 @@ const Metric = (props: MetricProps) => {
];
}, [
props.format,
props.formatDelta,
props.deltaFormat,
props.min,
props.max,
props.type,
Expand All @@ -122,6 +122,7 @@ const Metric = (props: MetricProps) => {
data={data as Data[]}
layout={baseLayout}
style={style}
useResizeHandler
/>
</Suspense>
</Box>
Expand Down
82 changes: 30 additions & 52 deletions frontend/taipy-gui/src/utils/formatConversion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,51 +11,46 @@
* specific language governing permissions and limitations under the License.
*/

const FORMAT_REPLACE_REGEX = /%([0-9]*)([.][0-9]+)?([bdieufgosxX])/g;

/**
* Converts a sprintf format string to a D3 format string.
*
* This function takes a sprintf format string as input and returns a D3 format string.
* The conversion is done by replacing each sprintf format specifier with the corresponding D3 format specifier.
* If the input format string is undefined, the function returns undefined.
* If an error occurs during the conversion, the function logs the error to the console and returns undefined.
* Convert sprintf-like format string to D3 format string.
*
* @param format - The sprintf format string to convert.
* @returns The converted D3 format string, or undefined if the input is undefined or if an error occurs.
* @param format - The sprintf-like format string.
* @returns The converted D3 format string or undefined if an error occurs.
*/
const sprintfToD3Converter = (format: string) => {
try {
return format?.replace(FORMAT_REPLACE_REGEX, (match, width, precision, type) => {
/**
* Helper function to handle precision formatting.
*
* @param precision - The precision part of the format string.
* @param specifier - The type of formatting.
* @returns The D3 precision format string.
*/
const precisionFormat = (precision: string | undefined, specifier: string) => {

Check warning on line 28 in frontend/taipy-gui/src/utils/formatConversion.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🕹️ Function is not covered

Warning! Not covered function
namnguyen20999 marked this conversation as resolved.
Show resolved Hide resolved
// Default to precision of 2 if not specified
return "." + (precision?.slice(1) ?? "2") + specifier;

Check warning on line 30 in frontend/taipy-gui/src/utils/formatConversion.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 30 in frontend/taipy-gui/src/utils/formatConversion.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

Check warning on line 30 in frontend/taipy-gui/src/utils/formatConversion.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

Check warning on line 30 in frontend/taipy-gui/src/utils/formatConversion.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

Check warning on line 30 in frontend/taipy-gui/src/utils/formatConversion.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

Check warning on line 30 in frontend/taipy-gui/src/utils/formatConversion.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

Check warning on line 30 in frontend/taipy-gui/src/utils/formatConversion.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

Check warning on line 30 in frontend/taipy-gui/src/utils/formatConversion.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

Check warning on line 30 in frontend/taipy-gui/src/utils/formatConversion.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
}

Check warning on line 31 in frontend/taipy-gui/src/utils/formatConversion.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

return format?.replace(/%([0-9]*)([.][0-9]+)?([bdieufgoxX])/g, (match, width, precision, type) => {

Check warning on line 33 in frontend/taipy-gui/src/utils/formatConversion.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

Check warning on line 33 in frontend/taipy-gui/src/utils/formatConversion.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

Check warning on line 33 in frontend/taipy-gui/src/utils/formatConversion.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

Check warning on line 33 in frontend/taipy-gui/src/utils/formatConversion.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🕹️ Function is not covered

Warning! Not covered function
console.log(`Match: ${match}, Width: ${width}, Precision: ${precision}, Type: ${type}`)

Check warning on line 34 in frontend/taipy-gui/src/utils/formatConversion.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
switch (type) {
namnguyen20999 marked this conversation as resolved.
Show resolved Hide resolved
case "b":
return "b";
case "d":
return "d";
case "i":
return "d";
case "e":
return "e";
case "f":
return "." + (precision?.slice(1) ?? "2") + "f";
case "g":
return "." + (precision?.slice(1) ?? "2") + "g";
case "o":
return "o";
case "s":
return "";
case "x":
return "x";
case "X":
return "X";
return type;

Check warning on line 43 in frontend/taipy-gui/src/utils/formatConversion.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 43 in frontend/taipy-gui/src/utils/formatConversion.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
case "f":
namnguyen20999 marked this conversation as resolved.
Show resolved Hide resolved
return precisionFormat(precision, "f");

Check warning on line 45 in frontend/taipy-gui/src/utils/formatConversion.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 45 in frontend/taipy-gui/src/utils/formatConversion.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
case "g":
return precisionFormat(precision, "g");

Check warning on line 47 in frontend/taipy-gui/src/utils/formatConversion.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 47 in frontend/taipy-gui/src/utils/formatConversion.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
case "u":
return "("

Check warning on line 49 in frontend/taipy-gui/src/utils/formatConversion.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 49 in frontend/taipy-gui/src/utils/formatConversion.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
default:
return "";
}

Check warning on line 52 in frontend/taipy-gui/src/utils/formatConversion.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
});

Check warning on line 53 in frontend/taipy-gui/src/utils/formatConversion.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 53 in frontend/taipy-gui/src/utils/formatConversion.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
} catch (error) {
console.error(`Failed to convert format "${format}" to D3 format: ${error}`);
return undefined;
}
}

/**
namnguyen20999 marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -67,14 +62,8 @@
if (format === undefined) {
return undefined;
}

try {
const PREFIX_MATCH_REGEX: RegExp = /.*?(?=%)/;
namnguyen20999 marked this conversation as resolved.
Show resolved Hide resolved
return format.match(PREFIX_MATCH_REGEX)?.[0] ?? "";
} catch (error) {
console.error(`Failed to extract prefix from format "${format}": ${error}`);
return undefined;
}
}

/**
namnguyen20999 marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -86,34 +75,23 @@
if (format === undefined) {
return undefined;
}

try {
const SURFIX_MATCH_REGEX: RegExp = /(?<=[bdieufgosxX])./;
const SURFIX_MATCH_REGEX: RegExp = /(?<=[bdieufgsxX])./;

Check warning on line 78 in frontend/taipy-gui/src/utils/formatConversion.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
namnguyen20999 marked this conversation as resolved.
Show resolved Hide resolved
return format.match(SURFIX_MATCH_REGEX)?.[0] ?? "";
} catch (error) {
console.error(`Failed to extract suffix from format "${format}": ${error}`);
return undefined;
}
}

/**
* Extracts the format specifier from the input string.
* The input string is expected to be in sprintf format.
* The function returns the format specifier (one of 'b', 'd', 'i', 'e', 'f', 'g', 'o', 's', 'x', 'X') if it exists, or undefined otherwise.
* The function returns the format specifier (one of 'b', 'd', 'i', 'e', 'u', 'f', 'g', 'o', 'x', 'X') if it exists, or undefined otherwise.
* @param input - The input string in sprintf format.
* @returns The extracted format specifier, or undefined if no specifier is found or if the input is undefined.
*/
export const extractFormatSpecifier = (input: string | undefined): string | undefined => {
if (input === undefined) {
return undefined;
}

try {
const regex: RegExp = /.*%?([bdieufgosxX]).*/g;
const convertedInput = sprintfToD3Converter(input);
return convertedInput ? convertedInput.replace(regex, '$1') : undefined;
} catch (error) {
console.error(`Failed to extract format specifier from input "${input}": ${error}`);
return undefined;
}
const regex: RegExp = /%.*\.?[bdieufgoxX]/;

Check warning on line 93 in frontend/taipy-gui/src/utils/formatConversion.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
const match = input.match(regex);

Check warning on line 94 in frontend/taipy-gui/src/utils/formatConversion.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
const format = match ? match[0] : undefined;

Check warning on line 95 in frontend/taipy-gui/src/utils/formatConversion.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 95 in frontend/taipy-gui/src/utils/formatConversion.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

Check warning on line 95 in frontend/taipy-gui/src/utils/formatConversion.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
return format ? sprintfToD3Converter(format) : undefined;

Check warning on line 96 in frontend/taipy-gui/src/utils/formatConversion.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 96 in frontend/taipy-gui/src/utils/formatConversion.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

Check warning on line 96 in frontend/taipy-gui/src/utils/formatConversion.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
}
1 change: 1 addition & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ filterwarnings =
ignore::FutureWarning:pyarrow
markers =
teste2e:End-to-end tests
extension:Taipy GUI Extension tests
namnguyen20999 marked this conversation as resolved.
Show resolved Hide resolved
orchestrator_dispatcher:Orchestrator dispatcher tests
standalone:Tests starting a standalone dispatcher thread
2 changes: 1 addition & 1 deletion taipy/gui/_renderers/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ class _Factory:
("height", PropertyType.string_or_number),
("show_value", PropertyType.boolean, True),
("format", PropertyType.string),
("format_delta", PropertyType.string),
("delta_format", PropertyType.string),
]
),
"navbar": lambda gui, control_type, attrs: _Builder(
Expand Down
12 changes: 12 additions & 0 deletions taipy/gui/viselements.json
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,18 @@
"type": "str|number",
"default_value": "None",
"doc": "The height, in CSS units, of the metric."
},
{
"name": "format",
"type": "str",
"default_value": "None",
namnguyen20999 marked this conversation as resolved.
Show resolved Hide resolved
"doc": "The format to use when displaying the value.<br/>This uses the <code>printf</code> syntax."
},
{
"name": "delta_format",
"type": "str",
"default_value": "None",
"doc": "The format to use when displaying the delta value.<br/>This uses the <code>printf</code> syntax."
}
]
}
Expand Down
Loading
Loading