Skip to main content

karni-charts

Saved charts for Karni: charts defined and previewed in the admin, served through each resource's aggregate action under its access.

PyPI License: Apache-2.0 Docs

A chart's data is never stored or computed by the module: it is the source resource's own aggregate action, fetched as the person who views the chart, so a chart never shows a row the source's list would hide from them.

Experimental, like the aggregations it builds on: the model, the API, the settings and @karni/charts-ui may change in a minor release, with a CHANGELOG entry. The error codes keep their meaning.

Install

pip install karni-charts
karni add charts
make migrate

karni add charts puts karni_charts in INSTALLED_APPS. The API rides Karni's auto-router (no URL include), and the settings arrive through the module's settings fragment. The React component is in @karni/charts-ui:

npm install @karni/charts-ui

A chart

A source is any routed resource whose model declares an aggregation:

class Product(BaseModel):
    class Karni:
        aggregate = {
            "group_by": ["category", "channel", "launched_on"],
            "values": ["price"],
        }

In the admin, Charts holds the definitions:

Field What it is
name, slug, description what the chart is called, the slug the app asks for, a description screen readers hear
source the resource it reads (products), chosen from the resources that declare an aggregation
kind bar or line
x_field, x_bucket, x_label the x axis: a declared group, a date bucket (year, quarter, month) for a date, its label
y_metric, y_value, y_label the y axis: count, sum, avg, min, max or distinct_count, the value it reads, its label
z_field, z_bucket, z_mode an optional second group: stack (stacked bars) or series (one line or bar per value)
top keep the largest N groups of the x axis and put the rest in "Other"
default_filters list filters the chart always applies, {"channel": "online"}
allowed_filters list filters a viewer may add, ["channel", "launched_on__gte"]
position the order charts are listed in

Saving checks the axes against the source's declaration (CHART004), the filters (CHART003) and the source (CHART001). The change form shows a preview under the fields: the chart's data as a table, as the signed-in staff user may see it.

Main features

  • Charts defined and previewed in the admin: a source resource, its axes and its filters
  • Served at /api/charts/<slug>/data/ through the source's aggregate action, as the viewer
  • Filters a viewer may add, next to the ones the chart applies
  • Drawn in React with one component, SavedChart from @karni/charts-ui
  • Error codes CHART001 to CHART004, explained offline by karni explain

The API

Endpoint Answers
GET /api/charts/ the definitions, for whoever KARNI_CHARTS_READ admits
GET /api/charts/<slug>/ one definition
GET /api/charts/<slug>/data/?<allowed filters> {"chart": <definition>, "data": <aggregate>}

The data action merges the chart's default_filters with the viewer's query parameters, which must be in allowed_filters (anything else is a 400, CHART002), then calls the source's GET <source>/aggregate/ in-process, as the viewer. The source's access, field visibility, filters, throttles and query budget apply as if the viewer had called it. When the source refuses (a 403, a 404, a 400 with its E38x code), the data action answers the same. Charts are written in the admin only: the API reads.

In React

import { SavedChart } from "@karni/charts-ui";

<SavedChart chart="launches-per-year" filters={{ channel: "online" }} />

SavedChart fetches the definition and the data in one request and draws them with @karni/ui's DataChart (stacked bars for a stack chart). It takes DataChart's own props for formatting (valueFormatter, xTickFormatter, slots, className, width, height), and slots of its own for the loading and error states. useSavedChart(slug, filters) is the hook underneath.

Settings

Setting Env var Default Meaning
KARNI_CHARTS_READ KARNI_CHARTS_READ "authenticated" Who reads the chart definitions: authenticated, public (charts on a public page) or granted (the roles' grants on karni_charts.chart). The data always follows the source's own access.

Escape hatches

  • KARNI_CHARTS_READ, or ChartViewSet.get_access() in a subclass you register yourself, decides who reads the definitions.
  • karni_charts.services: sources() (the resources a chart may read), chart_filters() (how the viewer's filters merge with the chart's), check_chart() and run_chart() (how the source is called).
  • Chart.query() is the source's query string for a chart.
  • The admin is an explicit registration: admin.site.unregister(Chart), then register a subclass of karni_charts.admin.ChartAdmin.
  • A chart the model cannot express is useAggregate and DataChart from @karni/ui in a page of your own.

Error codes

Code Problem
CHART001 the source is not a routed resource with an aggregation
CHART002 a viewer's filter the chart does not allow
CHART003 default_filters or allowed_filters is not valid
CHART004 the axes do not fit the source's aggregation

karni explain CHART001 prints each one.

Metadata

Release files for karni-charts 0.7.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for karni-charts 0.7.0
File Size Uploaded Provenance Actions
karni_charts-0.7.0.tar.gz 18.6 kB Without provenance Details for karni_charts-0.7.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for karni-charts 0.7.0
File Interpreter ABI Platform Actions
karni_charts-0.7.0-py3-none-any.whl Python 3 none any Details for karni_charts-0.7.0-py3-none-any.whl

Total release size: 42.4 kB

Release files / karni_charts-0.7.0.tar.gz

Download URL karni_charts-0.7.0.tar.gz
Size 18.6 kB
Tags Source
SHA-256 checksum
How to use checksums
cd4b39c97d5a69985f5907c0c7dcda22c3800a4f2c8b4ea9b1ce0afc5105b127
BLAKE2b-256 checksum
How to use checksums
7859140d7e70d627e780b4d9f1071309085c751c73f63c6cbab020406e614a34
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / karni_charts-0.7.0-py3-none-any.whl

Download URL karni_charts-0.7.0-py3-none-any.whl
Size 23.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5f016d8a14bd79894eae25df3c2bde593e1d419aa0b8b586e59ea51951da3bf0
BLAKE2b-256 checksum
How to use checksums
75dd208ea25fcba97c346ba3e9b29a2665b9f00b8f866b469f55c9dc09c433d4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Security for karni-charts 0.7.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page