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

Added kpi component to models #4

Merged
merged 6 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 4 additions & 0 deletions examples/iris/blue.css
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@
.pf-number-content {
color: blue;
}

.pf-kpi-content {
color: navy;
}
4 changes: 4 additions & 0 deletions examples/iris/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ def main():
pf.Number(len(iris_df.species.unique()), "Unique species"),
pf.Number(iris_df.sepal_length.max(), "Max Sepal Length"),
],
[
pf.KPI(round(taxi_df["tip"].mean(), 2), 3, "Average Tip"),
pf.KPI(taxi_df[taxi_df["tip"] < 1].shape[0], 1, "Tips under 1$"),
],
pf.Table(iris_df.head(10), "Iris dataset"),
[pf.Figure(width_figure), pf.Figure(length_figure)],
pf.PageBreak(),
Expand Down
Binary file modified examples/iris/out.pdf
Binary file not shown.
16 changes: 16 additions & 0 deletions pydfy/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,22 @@ class Number(Component):
template_path: str = field(kw_only=True, default="src/partials/number.html")


@dataclass
class KPI(Component):
current: Union[int, float]
target: Union[int, float]
title: str

template_path: str = field(kw_only=True, default="src/partials/kpi.html")

@property
def _percent(self) -> float:
if self.target == 0:
return float("inf")
else:
return (self.current - self.target) / self.target * 100


@dataclass
class Paragraph(Component):
content: str
Expand Down
8 changes: 8 additions & 0 deletions pydfy/template/src/partials/kpi.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<div class="pf-kpi border-solid border border-gray-100">
<div class="pf-kpi-title bg-gray-200 text-lg font-bold text-center align-top">{{ component.title }}</div>
Donnype marked this conversation as resolved.
Show resolved Hide resolved
<div class="pf-kpi-content mt-2 text-center">
<div class="text-3xl">{{ "{:,}".format(component.current) }}</div>
<div class="text-xs mb-2">{{ "Target: {:,} ({:+.2f} %)".format(component.target,
component._percent) }}</div>
</div>
</div>
1 change: 1 addition & 0 deletions tests/test_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ def test_all_components(tmp_path, out):
[
pf.Number(df.shape[0], "Sample size"),
pf.Number(df[1].max(), "Max"),
pf.KPI(482.537, 595.092, "Compare"),
],
pf.Table(df.head(10), "Test dataset"),
[pf.Image(asset_dir / "hist.png"), pf.Figure(figure)],
Expand Down
Loading