A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
feat: Contextually change stat color (#7175)
Kyle Goodrick3c21a9744159
diff --git a/frontend/src/plugins/layout/StatPlugin.tsx b/frontend/src/plugins/layout/StatPlugin.tsxindex 60f71c00b8..1fec7b8369 100644--- a/frontend/src/plugins/layout/StatPlugin.tsx+++ b/frontend/src/plugins/layout/StatPlugin.tsx@@ -17,6 +17,7 @@ interface Data {caption?: string;bordered?: boolean;direction?: "increase" | "decrease";+ target_direction?: "increase" | "decrease";}export class StatPlugin implements IStatelessPlugin<Data> {@@ -28,6 +29,7 @@ export class StatPlugin implements IStatelessPlugin<Data> {caption: z.string().optional(),bordered: z.boolean().default(false),direction: z.enum(["increase", "decrease"]).optional(),+ target_direction: z.enum(["increase", "decrease"]).default("increase"),});render({ data }: IStatelessPluginProps<Data>): JSX.Element {@@ -41,6 +43,7 @@ export const StatComponent: React.FC<Data> = ({caption,bordered,direction,+ target_direction,}) => {const { locale } = useLocale();@@ -64,6 +67,10 @@ export const StatComponent: React.FC<Data> = ({return String(value);};+ const onTarget = direction === target_direction;+ const fillColor = onTarget ? "var(--grass-8)" : "var(--red-8)";+ const strokeColor = onTarget ? "var(--grass-9)" : "var(--red-9)";+return (<divclassName={cn(@@ -83,15 +90,15 @@ export const StatComponent: React.FC<Data> = ({{direction === "increase" && (<TriangleIconclassName="w-4 h-4 mr-1 p-0.5"- fill="var(--grass-8)"- stroke="var(--grass-9)"+ fill={fillColor}+ stroke={strokeColor}/>)}{direction === "decrease" && (<TriangleIconclassName="w-4 h-4 mr-1 p-0.5 transform rotate-180"- fill="var(--red-8)"- stroke="var(--red-9)"+ fill={fillColor}+ stroke={strokeColor}/>)}{caption}diff --git a/marimo/_plugins/stateless/stat.py b/marimo/_plugins/stateless/stat.pyindex 5f02671ed7..bd9e84b71c 100644--- a/marimo/_plugins/stateless/stat.py+++ b/marimo/_plugins/stateless/stat.py@@ -16,6 +16,7 @@ def stat(caption: Optional[str] = None,direction: Optional[Literal["increase", "decrease"]] = None,bordered: bool = False,+ target_direction: Optional[Literal["increase", "decrease"]] = "increase",) -> Html:"""Display a statistic.@@ -28,6 +29,12 @@ def stat(direction: the direction of the statistic,either `increase` or `decrease`bordered: whether to display a border around the statistic+ target_direction: the direction of the statistic+ corresponding to a positive or desirable outcome. Set to+ `increase` when higher values are better, or `decrease`+ when lower values are better. By default the target+ direction is `increase`.+Returns:An `Html` object representing the statistic.@@ -42,6 +49,7 @@ def stat("caption": caption,"direction": direction,"bordered": bordered,+ "target_direction": target_direction,}),)diff --git a/marimo/_smoke_tests/stats.py b/marimo/_smoke_tests/stats.pyindex c10885fd7a..f75c810ffa 100644--- a/marimo/_smoke_tests/stats.py+++ b/marimo/_smoke_tests/stats.py@@ -17,10 +17,20 @@ def _(mo):_stats = [mo.stat("$100", label="Revenue", caption="+ 10%", direction="increase"),mo.stat(- "$20", label="Marketing spend", caption="+ 10%", direction="increase"+ "$20",+ label="Marketing spend",+ caption="+ 10%",+ direction="increase",+ target_direction="decrease",+ ),+ mo.stat("$80", label="Profit", caption="- 10%", direction="decrease"),+ mo.stat(+ "2%",+ label="Churn",+ caption="- 2%",+ direction="decrease",+ target_direction="decrease",),- mo.stat("$80", label="Profit", caption="+ 10%", direction="increase"),- mo.stat("2%", label="Churn", caption="- 2%", direction="decrease"),]mo.hstack(_stats)return@@ -42,12 +52,13 @@ def _(mo):caption="+ 10%",direction="increase",bordered=True,+ target_direction="decrease",),mo.stat("$80",label="Profit",- caption="+ 10%",- direction="increase",+ caption="- 10%",+ direction="decrease",bordered=True,),mo.stat(@@ -56,6 +67,7 @@ def _(mo):caption="- 2%",direction="decrease",bordered=True,+ target_direction="decrease",),]mo.hstack(_stats, widths="equal", gap=1)