Skip to content

Commit

Permalink
Allow visualizing and comparing nested params (mlflow#13012)
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Escalada <[email protected]>
Signed-off-by: Juan Escalada <[email protected]>
  • Loading branch information
jescalada authored Oct 3, 2024
1 parent 7315087 commit 63ca2de
Showing 1 changed file with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,21 @@ export class CompareRunView extends Component<CompareRunViewProps, CompareRunVie
// @ts-expect-error TS(4111): Property 'onlyShowParamDiff' comes from an index s... Remove this comment to see the full error message
this.state.onlyShowParamDiff,
true,
(key: any, data: any) => key,
(value) => {
try {
const jsonValue = parsePythonDictString(value);

// Pretty print if parsed value is an object or array
if (typeof jsonValue === 'object' && jsonValue !== null) {
return this.renderPrettyJson(jsonValue);
} else {
return value;
}
} catch (e) {
return value;
}
},
);
if (dataRows.length === 0) {
return (
Expand All @@ -230,6 +245,10 @@ export class CompareRunView extends Component<CompareRunViewProps, CompareRunVie
);
}

renderPrettyJson(jsonValue: any) {
return <pre>{JSON.stringify(jsonValue, null, 2)}</pre>;
}

renderMetricTable(colWidth: any, experimentIds: any) {
const dataRows = this.renderDataRows(
this.props.metricLists,
Expand Down Expand Up @@ -714,4 +733,19 @@ const mapStateToProps = (state: any, ownProps: any) => {
};
};

/**
* Parse a Python dictionary in string format into a JSON object.
* @param value The Python dictionary string to parse
* @returns The parsed JSON object, or null if parsing fails
*/
const parsePythonDictString = (value: string) => {
try {
const jsonString = value.replace(/'/g, '"');
return JSON.parse(jsonString);
} catch (e) {
console.error('Failed to parse string to JSON:', e);
return null;
}
};

export default connect(mapStateToProps)(injectIntl(CompareRunView));

0 comments on commit 63ca2de

Please sign in to comment.