Skip to main content

ChartHandler (Python)

Pure-Python charting with pluggable backends and base64 / data-URI output — designed so charts drop straight into HTML emails and into any web framework (Django, Flask, and ASGI apps under uvicorn).

from charthandler import Chart

# A PNG <img> tag you can paste into an HTML email — no external request.
html = Chart.pie({"Chrome": 63, "Firefox": 19, "Safari": 18}).title("Browser share").to_email_img()

Why this design

  • The SVG backend is pure Python with zero dependencies — installs instantly anywhere (slim Docker, Alpine, serverless) and works identically under Django, Flask, FastAPI/uvicorn, etc. (a chart is just str/bytes, so it's framework-agnostic).
  • Raster (PNG/JPEG/GIF/WebP) is an optional extra via Pillow — needed because SVG doesn't render in some email clients (Outlook desktop). Install only if you need it.

Features

  • Chart types: pie, donut, bar, stacked bar, line, area, scatter, and combo (mixed bars + line/area with an independently-scaled secondary axis).
  • Outputs: raw bytes, file, base64, data: URI, ready-to-embed <img> tag.
  • Fully type-hinted (py.typed), tested, mypy-strict + ruff clean.

Install

pip install pythoncharthandler            # SVG only — zero dependencies
pip install "pythoncharthandler[raster]"  # + PNG/JPEG/GIF/WebP via Pillow

Quickstart

from charthandler import Chart, Axis, Series, LegendPosition

# Pie -> SVG string (no dependencies)
Chart.pie({"Chrome": 63, "Firefox": 19, "Safari": 18}).title("Share").to_svg()

# Bar -> PNG bytes (needs the [raster] extra)
Chart.bar([12, 19, 7, 22, 15]).categories(["Jan", "Feb", "Mar", "Apr", "May"]).to_png()

# Multi-series line, saved (format inferred from the extension)
Chart.line([Series.from_values("2024", [10, 14, 9, 18])]) \
    .add_series(Series.from_values("2025", [13, 11, 17, 21])) \
    .categories(["Q1", "Q2", "Q3", "Q4"]).save("signups.svg")

# Stacked bar
Chart.stacked_bar([
    Series.from_values("Direct", [12, 19, 15]),
    Series.from_values("Organic", [20, 24, 28]),
]).categories(["Jan", "Feb", "Mar"]).to_png()

# Scatter from (x, y) pairs
Chart.scatter([(1, 5), (2, 9), (4, 3)]).add_points("B", [(1, 2), (3, 6)]).to_svg()

# Combo: grouped bars + a line on an independently-scaled right axis
Chart.combo() \
    .add_bar("Revenue", [120, 190, 70, 220]) \
    .add_line("Conversion %", [3.2, 4.1, 2.8, 5.0], axis=Axis.RIGHT) \
    .title("Revenue vs conversion").categories(["Q1", "Q2", "Q3", "Q4"]) \
    .to_email_img()

Output methods

Method Returns Notes
to_svg() str SVG markup
to_png() / to_jpeg() bytes needs the [raster] extra
to_data_uri(fmt=Format.PNG) str data:<mime>;base64,…
to_html_img(fmt=Format.PNG, **attrs) str <img src="data:…" …>
to_email_img(**attrs) str PNG <img> — use this for email
save(path) None format inferred from the extension
render(fmt) RenderedChart for full control

Using it in web frameworks

FastAPI / uvicorn:

from fastapi import Response
from charthandler import Chart

@app.get("/chart.png")
def chart() -> Response:
    png = Chart.bar({"Mon": 8, "Tue": 12, "Wed": 5}).title("This week").to_png()
    return Response(content=png, media_type="image/png")

Flask:

@app.get("/chart.png")
def chart():
    return Chart.bar([8, 12, 5]).to_png(), 200, {"Content-Type": "image/png"}

Django:

from django.http import HttpResponse

def chart(request):
    return HttpResponse(Chart.pie({"A": 60, "B": 40}).to_png(), content_type="image/png")

For inline charts in a template/email, pass chart.to_data_uri() and use <img src="{{ chart_uri }}">.

Charts in HTML email

Email clients can't fetch external images offline, and several (Outlook desktop, some Gmail setups) won't render inline SVG. The reliable approach is a base64 PNG embedded directly in the markup (to_email_img() does exactly this — it needs the [raster] extra):

img = Chart.bar({"Mon": 8, "Tue": 12, "Wed": 5}).title("This week").to_email_img(alt="Activity")
html = f"<h1>Your report</h1>{img}"
# -> <img src="data:image/png;base64,iVBORw0KGgo..." alt="Activity" />

Run the demo (Docker)

A FastAPI gallery is bundled. With Docker it serves on http://localhost:2200/:

docker compose up -d --build
# http://localhost:2200/                 — gallery (every type, inline PNG)
# http://localhost:2200/charts/combo.png — single chart as PNG
# http://localhost:2200/charts/pie.svg   — single chart as SVG

Without Docker:

pip install "pythoncharthandler[raster,demo]"
uvicorn examples.fastapi_app:app --reload   # http://127.0.0.1:8000/

Development

python -m venv .venv && . .venv/bin/activate
pip install -e ".[dev]"
pytest        # tests
mypy          # strict type-check
ruff check .  # lint

A FastAPI demo lives in examples/fastapi_app.py (run with uvicorn examples.fastapi_app:app --reload).

License

MIT — see LICENSE.

Metadata

Release files for pythoncharthandler 0.1.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 pythoncharthandler 0.1.0
File Size Uploaded Provenance Actions
pythoncharthandler-0.1.0.tar.gz 25.3 kB With provenance Details for pythoncharthandler-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pythoncharthandler 0.1.0
File Interpreter ABI Platform Actions
pythoncharthandler-0.1.0-py3-none-any.whl Python 3 none any Details for pythoncharthandler-0.1.0-py3-none-any.whl

Total release size: 48.5 kB

Release files / pythoncharthandler-0.1.0.tar.gz

Download URL pythoncharthandler-0.1.0.tar.gz
Size 25.3 kB
Tags Source
SHA-256 checksum
How to use checksums
e6d09e06574e90614908bf4ab0bb4fd6c7924b34bc29b2a24cf919eefc27e2dd
BLAKE2b-256 checksum
How to use checksums
2b0ea3016814f00148d8c28246d278f874e4c41f5dddf29d60c030803320b736
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 20, 2026.

Transparency log

Release files / pythoncharthandler-0.1.0-py3-none-any.whl

Download URL pythoncharthandler-0.1.0-py3-none-any.whl
Size 23.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d06a8d2e65b4a6d7292de140c2cb8154798ac0453f0ad8dd34c5f87b068ba1be
BLAKE2b-256 checksum
How to use checksums
9d1c050d2ac1a848fa7c7bcf648f51b6f26af41a5961c5ea26366a98c2f432f8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 20, 2026.

Transparency log

Security for pythoncharthandler 0.1.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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for pythoncharthandler-0.1.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 20, 2026.

Transparency log

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.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files Full provenance

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