Skip to content

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.

feat: stat plugin (#210)

Myles Scolnick5d3ab8fd73c9
diff --git a/docs/api/layouts/index.md b/docs/api/layouts/index.md
index fa8a46a67e..48376fbdcc 100644
--- a/docs/api/layouts/index.md
+++ b/docs/api/layouts/index.md
@@ -10,6 +10,7 @@
image
justify
stacks
+ stat
tabs
tree
```
@@ -28,7 +29,8 @@ certain way.
marimo.hstack
marimo.left
marimo.right
- marimo.vstack
+ marimo.stat
marimo.tabs
marimo.tree
+ marimo.vstack
```
diff --git a/docs/api/layouts/stat.md b/docs/api/layouts/stat.md
new file mode 100644
index 0000000000..ff3d2967d8
--- /dev/null
+++ b/docs/api/layouts/stat.md
@@ -0,0 +1,7 @@
+# Stat
+
+<!-- <iframe class="demo medium" src="https://components.marimo.io/?component=stat" frameborder="no"></iframe> -->
+
+```{eval-rst}
+.. autofunction:: marimo.stat
+```
diff --git a/frontend/src/plugins/layout/StatPlugin.tsx b/frontend/src/plugins/layout/StatPlugin.tsx
new file mode 100644
index 0000000000..c4fea4a39e
--- /dev/null
+++ b/frontend/src/plugins/layout/StatPlugin.tsx
@@ -0,0 +1,75 @@
+/* Copyright 2023 Marimo. All rights reserved. */
+
+import { z } from "zod";
+import { IStatelessPlugin, IStatelessPluginProps } from "../stateless-plugin";
+import { TriangleIcon } from "lucide-react";
+import { cn } from "@/lib/utils";
+
+interface Data {
+ value: string;
+ label?: string;
+ caption?: string;
+ bordered?: boolean;
+ direction?: "increase" | "decrease";
+}
+
+export class StatPlugin implements IStatelessPlugin<Data> {
+ tagName = "marimo-stat";
+
+ validator = z.object({
+ value: z.string(),
+ label: z.string().optional(),
+ caption: z.string().optional(),
+ bordered: z.boolean().default(false),
+ direction: z.enum(["increase", "decrease"]).optional(),
+ });
+
+ render({ data }: IStatelessPluginProps<Data>): JSX.Element {
+ return <StatComponent {...data} />;
+ }
+}
+
+export const StatComponent: React.FC<Data> = ({
+ value,
+ label,
+ caption,
+ bordered,
+ direction,
+}) => {
+ return (
+ <div
+ className={cn(
+ "text-card-foreground bg-card",
+ bordered && "rounded-xl border shadow"
+ )}
+ >
+ {label && (
+ <div className="p-6 flex flex-row items-center justify-between space-y-0 pb-2">
+ <h3 className="tracking-tight text-sm font-medium">{label}</h3>
+ </div>
+ )}
+ <div className="p-6 pt-0">
+ <div className="text-2xl font-bold">{value}</div>
+ {caption && (
+ <p className="pt-1 text-xs text-muted-foreground flex align-center">
+ {direction === "increase" && (
+ <TriangleIcon
+ className="w-4 h-4 mr-1 p-0.5"
+ fill="var(--grass-8)"
+ stroke="var(--grass-9)"
+ />
+ )}
+ {direction === "decrease" && (
+ <TriangleIcon
+ className="w-4 h-4 mr-1 p-0.5 transform rotate-180"
+ fill="var(--red-8)"
+ stroke="var(--red-9)"
+ />
+ )}
+ {caption}
+ </p>
+ )}
+ </div>
+ </div>
+ );
+};
diff --git a/frontend/src/plugins/plugins.ts b/frontend/src/plugins/plugins.ts
index c4d5fcc400..d77a6d724b 100644
--- a/frontend/src/plugins/plugins.ts
+++ b/frontend/src/plugins/plugins.ts
@@ -28,6 +28,7 @@ import { RefreshPlugin } from "./impl/RefreshPlugin";
import { MicrophonePlugin } from "./impl/MicrophonePlugin";
import { DownloadPlugin } from "./layout/DownloadPlugin";
import { VegaPlugin } from "./impl/vega/VegaPlugin";
+import { StatPlugin } from "./layout/StatPlugin";
// List of UI plugins
const UI_PLUGINS: Array<IPlugin<any, unknown>> = [
@@ -57,6 +58,7 @@ const LAYOUT_PLUGINS: Array<IStatelessPlugin<unknown>> = [
new CalloutPlugin(),
new DownloadPlugin(),
new JsonOutputPlugin(),
+ new StatPlugin(),
new TabsPlugin(),
new TexPlugin(),
];
diff --git a/frontend/src/stories/stat.stories.tsx b/frontend/src/stories/stat.stories.tsx
new file mode 100644
index 0000000000..d5122af61d
--- /dev/null
+++ b/frontend/src/stories/stat.stories.tsx
@@ -0,0 +1,122 @@
+/* Copyright 2023 Marimo. All rights reserved. */
+import { StatComponent } from "@/plugins/layout/StatPlugin";
+import type { Meta, StoryFn } from "@storybook/react";
+
+const meta: Meta<typeof StatComponent> = {
+ title: "Stat",
+ component: StatComponent,
+ args: {},
+};
+
+export default meta;
+
+const Template: StoryFn<typeof StatComponent> = (args) => (
+ <div className="max-w-4xl">
+ <StatComponent {...args} />
+ </div>
+);
+
+export const Full = Template.bind({});
+Full.args = {
+ label: "Revenue",
+ value: "$80,000",
+ caption: "Last 30 days",
+};
+
+export const Increase = Template.bind({});
+Increase.args = {
+ label: "Revenue",
+ value: "$80,000",
+ caption: "+42%",
+ direction: "increase",
+};
+
+export const Decrease = Template.bind({});
+Decrease.args = {
+ label: "Churn",
+ value: "6.4%",
+ caption: "-4%",
+ direction: "decrease",
+};
+
+export const Bordered = Template.bind({});
+Bordered.args = {
+ label: "Revenue",
+ value: "$80,000",
+ caption: "Last 30 days",
+ bordered: true,
+};
+
+export const Grid = () => {
+ return (
+ <div className="grid grid-cols-4 gap-4">
+ <StatComponent
+ label="Revenue"
+ value="$80,000"
+ caption="Last 30 days"
+ bordered={true}
+ />
+ <StatComponent
+ label="Profit"
+ value="$30,000"
+ caption="Last 30 days"
+ bordered={true}
+ />
+ <StatComponent
+ label="Marketing Spend"
+ value="$10,000"
+ caption="+42%"
+ direction="increase"
+ bordered={true}
+ />
+ <StatComponent
+ label="Churn"
+ value="6.4%"
+ caption="-4%"
+ direction="decrease"
+ bordered={true}
+ />
+ </div>
+ );
+};
+
+export const Flex = () => {
+ return (
+ <div className="flex flex-row gap-4">
+ <div className="flex-1">
+ <StatComponent
+ label="Revenue"
+ value="$80,000"
+ caption="Last 30 days"
+ bordered={true}
+ />
+ </div>
+ <div className="flex-1">
+ <StatComponent
+ label="Profit"
+ value="$30,000"
+ caption="Last 30 days"
+ bordered={true}
+ />
+ </div>
+ <div className="flex-1">
+ <StatComponent
+ label="Marketing Spend"
+ value="$10,000"
+ caption="+42%"
+ direction="increase"
+ bordered={true}
+ />
+ </div>
+ <div className="flex-1">
+ <StatComponent
+ label="Churn"
+ value="6.4%"
+ caption="-4%"
+ direction="decrease"
+ bordered={true}
+ />
+ </div>
+ </div>
+ );
+};
diff --git a/marimo/__init__.py b/marimo/__init__.py
index b0c8fdc4c0..062c6fbf47 100644
--- a/marimo/__init__.py
+++ b/marimo/__init__.py
@@ -38,6 +38,7 @@ __all__ = [
"redirect_stdout",
"refs",
"right",
+ "stat",
"state",
"stop",
"tabs",
@@ -63,6 +64,7 @@ from marimo._plugins.stateless.flex import hstack, vstack
from marimo._plugins.stateless.image import image
from marimo._plugins.stateless.pdf import pdf
from marimo._plugins.stateless.plain_text import plain_text
+from marimo._plugins.stateless.stat import stat
from marimo._plugins.stateless.tabs import tabs
from marimo._plugins.stateless.tree import tree
from marimo._runtime import output
diff --git a/marimo/_plugins/stateless/stat.py b/marimo/_plugins/stateless/stat.py
new file mode 100644
index 0000000000..b79808c1c0
--- /dev/null
+++ b/marimo/_plugins/stateless/stat.py
@@ -0,0 +1,50 @@
+# Copyright 2023 Marimo. All rights reserved.
+from __future__ import annotations
+
+from typing import Literal, Optional, Union
+
+from marimo._output.hypertext import Html
+from marimo._output.rich_help import mddoc
+from marimo._plugins.core.web_component import build_stateless_plugin
+from marimo._plugins.utils import remove_none_values
+
+
+@mddoc
+def stat(
+ value: Union[str, int, float],
+ label: Optional[str] = None,
+ caption: Optional[str] = None,
+ direction: Optional[Literal["increase", "decrease"]] = None,
+ bordered: bool = False,
+) -> Html:
+ """Display a statistic.
+
+ Optionally include a label, caption, and direction.
+
+ **Args.**
+
+ - `value`: the value to display
+ - `label`: the label to display
+ - `caption`: the caption to display
+ - `direction`: the direction of the statistic,
+ either `increase` or `decrease`
+ - `bordered`: whether to display a border around the statistic
+
+ **Returns.**
+
+ An `Html` object representing the statistic.
+ """
+ return Html(
+ build_stateless_plugin(
+ component_name="marimo-stat",
+ args=remove_none_values(
+ {
+ "value": value,
+ "label": label,
+ "caption": caption,
+ "direction": direction,
+ "bordered": bordered,
+ }
+ ),
+ )
+ )
diff --git a/marimo/_plugins/utils.py b/marimo/_plugins/utils.py
new file mode 100644
index 0000000000..aad6426114
--- /dev/null
+++ b/marimo/_plugins/utils.py
@@ -0,0 +1,11 @@
+# Copyright 2023 Marimo. All rights reserved.
+from __future__ import annotations
+
+from typing import TypeVar
+
+S = TypeVar("S")
+T = TypeVar("T")
+
+
+def remove_none_values(d: dict[S, T]) -> dict[S, T]:
+ return {k: v for k, v in d.items() if v is not None}
diff --git a/marimo/_smoke_tests/stats.py b/marimo/_smoke_tests/stats.py
new file mode 100644
index 0000000000..6ab42410e5
--- /dev/null
+++ b/marimo/_smoke_tests/stats.py
@@ -0,0 +1,65 @@
+# Copyright 2023 Marimo. All rights reserved.
+import marimo
+
+__generated_with = "0.1.29"
+app = marimo.App()
+
+
+def __():
+ import marimo as mo
+ return mo,
+
+
+def __(mo):
+ _stats = [
+ mo.stat("$100", label="Revenue", caption="+ 10%", direction="increase"),
+ mo.stat(
+ "$20", label="Marketing spend", caption="+ 10%", direction="increase"
+ ),
+ mo.stat("$80", label="Profit", caption="+ 10%", direction="increase"),
+ mo.stat("2%", label="Churn", caption="- 2%", direction="decrease"),
+ ]
+ mo.hstack(_stats)
+ return
+
+
+def __(mo):
+ _stats = [
+ mo.stat(
+ "$100",
+ label="Revenue",
+ caption="+ 10%",
+ direction="increase",
+ bordered=True,
+ ),
+ mo.stat(
+ "$20",
+ label="Marketing spend",
+ caption="+ 10%",
+ direction="increase",
+ bordered=True,
+ ),
+ mo.stat(
+ "$80",
+ label="Profit",
+ caption="+ 10%",
+ direction="increase",
+ bordered=True,
+ ),
+ mo.stat(
+ "2%",
+ label="Churn",
+ caption="- 2%",
+ direction="decrease",
+ bordered=True,
+ ),
+ ]
+ mo.hstack(_stats, widths="equal", gap=1)
+ return
+
+
+if __name__ == "__main__":
+ app.run()