Skip to main content

Python API for generating XKCD-style charts

Project description

chart.xkcd

A Python + JavaScript library for creating xkcd-style charts. See this repository for the original code.

Setup

Install Python dependencies (requires Python 3.13+):

uv pip install -e ".[dev]"

Install JavaScript dependencies:

cd js && npm install && cd -

Generating example data

The examples use data from snailz, a synthetic data generator. The configuration is in examples/snailz.json. To regenerate the SQLite database and CSV files:

task data

This runs snailz to create data/snailz.db, then runs each SQL file in examples/*.sql against it to produce CSV files in tmp/.

Building

Build the font data, JavaScript bundle, and Python package:

task build

This:

  1. Encodes assets/xkcd-script.ttf as a base64 data URL in js/src/utils/fontData.js.
  2. Bundles the JavaScript source with esbuild into src/chart_xkcd/static/chart.xkcd.js.
  3. Builds the Python wheel and sdist with python -m build.

Examples

Python command-line examples (examples/*.py)

Each chart type has a standalone script that reads a CSV file and writes a static HTML page:

Script Chart type Input CSV Description
bar.py Bar tmp/bar.csv Samples per person
stacked_bar.py StackedBar tmp/stacked_bar.csv Samples by variety and grid
line.py Line tmp/line.csv Samples collected per week
scatter.py Scatter tmp/scatter.csv Snail mass vs diameter
pie.py Pie tmp/pie.csv Samples by variety
radar.py Radar tmp/radar.csv Samples by variety and grid

Run them all at once with:

task ex_py

Or run one individually:

python examples/bar.py tmp/bar.csv tmp/bar.html

Marimo notebook (examples/notebook.py)

A marimo notebook that displays all six chart types as interactive widgets. Each cell reads a CSV file from tmp/ and calls to_widget() to render the chart.

marimo run examples/notebook.py

Selection test notebook (examples/test_selection.py)

A marimo notebook demonstrating click, shift-click, and box-select interactions. Each chart is wrapped with mo.ui.anywidget() so that selection changes trigger reactive cell updates.

marimo run examples/test_selection.py

JavaScript examples (js/examples/)

A standalone HTML page (example.html) that renders all six chart types using the JavaScript library directly. The data is loaded dynamically from CSV files via fetch(). A symlink js/examples/tmp points to the project-level tmp/ directory.

To view the JavaScript examples with a dev server:

task ex_js

Then open the URL printed by the dev server in a browser.

Project structure

assets/                  xkcd-script.ttf font file
bin/                     build scripts (font_encode.py)
examples/                Python examples, SQL queries, marimo notebooks
js/src/                  JavaScript chart source
  Bar.js, Line.js, ...   chart classes
  config.js              shared constants
  widget.js              anywidget entry point
  index.js               standalone library entry point
  components/Tooltip.js  tooltip component
  utils/                 shared helpers (axes, labels, legend, font, filter)
src/chart_xkcd/          Python package
  bar.py, line.py, ...   chart classes
  charts.py              base classes and validation
  widget.py              anywidget adapter (ChartWidget, to_widget)
  renderer.py            HTML rendering (render, to_html)
  config.py              positionType constants
  main.py                CLI entry point
  static/                bundled JS (built artifact)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

chart_xkcd-0.4.1.tar.gz (891.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

chart_xkcd-0.4.1-py3-none-any.whl (95.3 kB view details)

Uploaded Python 3

File details

Details for the file chart_xkcd-0.4.1.tar.gz.

File metadata

  • Download URL: chart_xkcd-0.4.1.tar.gz
  • Upload date:
  • Size: 891.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for chart_xkcd-0.4.1.tar.gz
Algorithm Hash digest
SHA256 a05e8913ea9a0ef07fd333a08e8052872845e63ebc025e6e0d8c4c99505a028a
MD5 7d31420f3ddf81414830ee44ca1792d1
BLAKE2b-256 0fbe6523f342a9035de7dfc544a06c762b740ca57c5553bf051d98854c7c7181

See more details on using hashes here.

File details

Details for the file chart_xkcd-0.4.1-py3-none-any.whl.

File metadata

  • Download URL: chart_xkcd-0.4.1-py3-none-any.whl
  • Upload date:
  • Size: 95.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for chart_xkcd-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 46a71676c3a45cef0415bb54692dbd5bc1f4fa305cd7db7155fef24e1c986c47
MD5 f56320a8b190c6906315aa2f01aeebe8
BLAKE2b-256 f4b48a9896e6d87e2f1fa25d804bf318ac05c14cb18d9094662538de47fb2fdc

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page