Skip to main content

xkcdkit

xkcd comics as a Python library, a command line tool and a terminal browser. Everything you look at is cached in a local SQLite database, together with the explanation from explainxkcd, so repeat lookups and searches never touch the network.

$ xkcd show 614
╭─ #614  Woodpecker ───────────────────────────────────────────────╮
│ If you don't have an extension cord I can get that too.  Because │
│ we're friends!  Right?                                           │
│ https://xkcd.com/614/                                            │
╰──────────────────────────────────────────────────── 2009-07-24 ──╯

On a terminal that supports the kitty graphics protocol or sixel, the comic image is drawn above the card.

Install

As a library

pip install xkcdkit
# or
uv add xkcdkit

As a command line tool and TUI

Install it in its own isolated environment so it never conflicts with your projects:

pipx install xkcdkit
# or
uv tool install xkcdkit

Either way you get the xkcd command.

Linux and macOS

The commands above work as written. If you prefer not to install anything permanently:

uvx xkcdkit show 614

Windows

On Windows the Python launcher is called py, and python is often not on your PATH (or opens the Microsoft Store). Use the launcher for everything that starts with python:

py -m pip install --user pipx
py -m pipx ensurepath
pipx install xkcdkit

or, with uv installed:

uv tool install xkcdkit

Images need Windows Terminal 1.22 or newer, which supports sixel. The old console host (conhost.exe) cannot draw images, and xkcdkit falls back to a text-only card there.

Command line

xkcd show 614            # one comic by number
xkcd show 614 --explain  # with the explainxkcd explanation
xkcd latest              # the newest comic
xkcd random              # a random comic
xkcd search "woodpecker" # fuzzy search over cached titles, alt text, transcripts and explanations
xkcd explain 614         # just the explanation, as plain text
xkcd sync                # refresh the archive index and the latest comic
xkcd sync --full         # download every comic and explanation (about an hour, see below)
xkcd resync              # re-fetch comics older than 30 days or still missing an explanation
xkcd tui                 # open the browser

Add --json to show, latest, random, search or explain for machine-readable output. Nothing but the JSON goes to stdout; errors go to stderr with exit code 1, so pipelines stay clean:

xkcd show 614 --json | jq .alt
xkcd search "sandwich" --json | jq '.[].num'

--no-image skips the picture. Images are also skipped automatically when stdout is not a terminal, so pipes always get clean text.

The browser

xkcd tui opens a two-pane browser in the spirit of lazygit: the archive on the left, the comic on the right with tabs for alt text, transcript and explanation.

Key Action
j/k move through the archive
/ filter the archive by title
Enter open the selected comic
r random comic
l latest comic
i show or hide the image pane
s sync the archive index
? key help
q quit

Library

The async client is the primary API. It is built for I/O bound programs such as bots and web services.

from xkcdkit import AsyncXKCD

async with AsyncXKCD() as xk:
    comic = await xk.get(614)
    print(comic.title, comic.alt, comic.img)
    print(comic.explanation_text[:200])

    latest = await xk.latest()
    hits = await xk.search("sandwich", limit=5)
    await xk.sync()  # archive index + latest comic

A blocking twin exists for scripts and the REPL. It runs each call on its own event loop, so it cannot be used from inside async code, and it opens and closes its connections per call (pass client_factory= rather than a client if you need to customise HTTP).

from xkcdkit import XKCD

with XKCD() as xk:
    print(xk.random().title)

In a long-running program such as a Discord bot, keep one AsyncXKCD open for the lifetime of the process: it holds the HTTP connection pool and the database connection.

from xkcdkit import AsyncXKCD, ComicNotFound

xk = AsyncXKCD()  # created once; call `await xk.close()` on shutdown


@bot.command()
async def xkcd(ctx, num: int):
    try:
        comic = await xk.get(num)
    except ComicNotFound:
        await ctx.send(f"There is no xkcd {num}.")
        return
    await ctx.send(f"**{comic.title}**\n{comic.img}\n*{comic.alt}*")

The Comic model

Comic is a pydantic model with every field from the xkcd JSON (num, title, safe_title, alt, img, transcript, link, news, year, month, day) plus:

Field Meaning
explanation full explainxkcd page wikitext, or None
explanation_revid the wiki revision that was fetched
explanation_missing True when explainxkcd had no page at sync time
synced_at when the row was written (UTC)
explanation_text property: the Explanation section as plain text
date, url properties: publication date and the comic's page

comic.model_dump_json() gives you the same JSON the CLI prints.

Data, caching and freshness

  • The database lives in your platform's user data directory (~/.local/share/xkcdkit/xkcd.sqlite3 on Linux, ~/Library/Application Support/xkcdkit/ on macOS, %LOCALAPPDATA%\xkcdkit\ on Windows). Set XKCDKIT_DB or pass --db / db= to put it elsewhere.
  • Comics are immutable once stored. A cached comic is served as is, except when its explanation was missing more than a day ago; then explainxkcd is asked again on the next access.
  • resync re-fetches rows older than --max-age days (default 30) and every row still flagged explanation_missing. Recently synced data is trusted.
  • Roughly 1 to 2 percent of comics have no explainxkcd page yet. Those rows carry explanation_missing = True so your code can decide what to do.

Being polite to explainxkcd

explainxkcd is a volunteer-run wiki. Its robots policy asks for one request per second, so a full sync fetches one explanation per second and sends a User-Agent that names this project and its repository. A complete first sync --full therefore takes about an hour. On-demand lookups are not delayed.

Terminal image support

Terminal Protocol Status
kitty, Ghostty kitty works
WezTerm, foot, iTerm2 sixel works
Windows Terminal 1.22+ sixel works
VS Code terminal sixel enable terminal.integrated.enableImages
tmux – falls back to half-block unicode
GNOME Terminal, conhost – falls back to half-block unicode

Detection and rendering are done by textual-image.

Development

git clone https://github.com/eivl/xkcdkit
cd xkcdkit
uv sync --all-groups
uv run pre-commit install
uv run pytest
uv run tox          # all supported Python versions

The code base is deliberately small: seven modules, short functions, a docstring on everything. ruff (with pydocstyle), mypy strict, flake8 and zizmor run in pre-commit and in CI.

Releasing

Commits follow Conventional Commits. On every push to main, python-semantic-release reads the commit messages, bumps the version in pyproject.toml, updates CHANGELOG.md, tags the release and publishes to PyPI through trusted publishing. feat: bumps the minor version, fix: the patch version, and a BREAKING CHANGE: footer the major version. The release commit also re-locks uv.lock so that uv sync --locked keeps working after the bump.

Trusted publishing has to be registered once on PyPI under Publishing → Add a new pending publisher with these values:

Field Value
PyPI project xkcdkit
Owner eivl
Repository xkcdkit
Workflow release.yml
Environment pypi

and a GitHub environment named pypi must exist in the repository settings.

License

MIT

Metadata

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

Built distribution (wheel)

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

Total release size: 38.6 kB

Release files / xkcdkit-0.1.0.tar.gz

Download URL xkcdkit-0.1.0.tar.gz
Size 18.2 kB
Tags Source
SHA-256 checksum
How to use checksums
83985732c37cda5bd14a4cec3a4f2414e9d30c89a75d86528f7fa39dac184079
BLAKE2b-256 checksum
How to use checksums
ec2ad31a4f1ca5d467a9ede996fa5a0dbad48d58d3f66e836f0dd86e001a237a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Oct 27, 2026.

Transparency log

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

Download URL xkcdkit-0.1.0-py3-none-any.whl
Size 20.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a42df77a6ccb08a4298e4eda600473c5ddaff4312b97fd976cbb8fe8ed3147bd
BLAKE2b-256 checksum
How to use checksums
2bfc7fcf4885d9460aa300dfdae0489c97e7a4c1ef1857fdb32379ea950fbe5e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Oct 27, 2026.

Transparency log

Security for xkcdkit 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 xkcdkit-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 Oct 27, 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.

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