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 (a couple of minutes)
xkcd sync --full --no-explain # comics only, under a minute; explanations come later
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.
Images are drawn at the size the terminal reports for them, which can be
small on a high-resolution screen. --scale 2 (or -s 3, any factor) makes
them bigger, and --fit fills the terminal while keeping the aspect ratio:
xkcd show 614 --scale 2
xkcd latest --fit
Settings file
Defaults for these live in a small TOML file that the first run creates for
you, with every key and a comment explaining it. xkcd config prints its
location and the values in effect; xkcd config --show-defaults prints the
default file, which is also the complete list of keys. Anything missing from
your file keeps its default, so upgrading never requires editing it.
| Platform | Location |
|---|---|
| Linux | ~/.config/xkcdkit/config.toml |
| macOS | ~/Library/Application Support/xkcdkit/config.toml |
| Windows | %APPDATA%\xkcdkit\config.toml |
Set XKCDKIT_CONFIG to use another path.
[image]
scale = 2.0 # CLI image size multiplier
fit = false # CLI: fill the terminal instead
[tui]
image_pane = 3 # browser: image pane height relative to the text pane (1 to 8)
[cli]
margin = 1 # blank space around a printed comic; 0 for a tight layout
# top = 1 # override single sides: lines above and below,
# bottom = 1 # columns left and right, and gap = lines between
# left = 2 # the image and the text card; unset ones follow margin
# right = 2
# gap = 1
Command line flags always win over the file.
Shell completion
xkcd --install-completion # bash, zsh, fish or PowerShell, detected from your shell
xkcd --show-completion # print the script instead, to install it yourself
Open a new shell afterwards.
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; up/down still move the list, Esc returns to it |
Enter |
open the selected comic |
r |
random comic |
l |
latest comic |
i |
show or hide the image pane |
+/- |
grow or shrink the image pane |
s |
sync the archive index |
? |
key help |
q |
quit |
The image always fills its pane while keeping its aspect ratio, so the pane
height is the zoom. The starting height comes from image_pane in the
settings file.
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.sqlite3on Linux,~/Library/Application Support/xkcdkit/on macOS,%LOCALAPPDATA%\xkcdkit\on Windows). SetXKCDKIT_DBor 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.
resyncre-fetches rows older than--max-agedays (default 30) and every row still flaggedexplanation_missing. Recently synced data is trusted.- Roughly 1 to 2 percent of comics have no explainxkcd page yet. Those rows
carry
explanation_missing = Trueso 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 bulk syncs send at most one explainxkcd request per second and
a User-Agent that names this project and its repository. To stay fast anyway,
each request asks the MediaWiki API for fifty pages at once, the maximum it
allows, so the whole archive's explanations arrive in about seventy requests.
xkcd.com has no such limit and is fetched by several workers at once. A
complete first sync --full takes a couple of minutes.
xkcd sync --full --no-explainfetches only the comics, in under a minute. Explanations are then filled in on demand when you open a comic, or in bulk withxkcd resync, paced the same way.- Ctrl-C at any point is safe. Each batch of fifty is committed as soon as it
is fetched, and the next
sync --fullorresyncskips everything already cached, so you can sync in several sittings.
The sync walks every number from 1 to the latest comic, so it does not depend on the archive page. On-demand lookups are never 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.6.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| xkcdkit-0.6.0.tar.gz | 23.2 kB | With provenance | Details for xkcdkit-0.6.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| xkcdkit-0.6.0-py3-none-any.whl | Python 3 | none | any | Details for xkcdkit-0.6.0-py3-none-any.whl |
Total release size: 49.0 kB
Release files / xkcdkit-0.6.0.tar.gz
| Download URL | xkcdkit-0.6.0.tar.gz |
|---|---|
| Size | 23.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f12fcb926d63b508f54dfb996c9779a64ce6f5ced437c33e5c9533db45a4cf18
|
|
BLAKE2b-256 checksum How to use checksums |
fc22a5c651ef61456fbb3456dfdb190140afff1188ab1215a3d23b7d6ba09ceb
|
| 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 logRelease files / xkcdkit-0.6.0-py3-none-any.whl
| Download URL | xkcdkit-0.6.0-py3-none-any.whl |
|---|---|
| Size | 25.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ee971559d4768f6e7e3f3a6524db68b54514ac7e698c1248fd8bfccdc3240c52
|
|
BLAKE2b-256 checksum How to use checksums |
d2397c84a3b00c0dbae4da9d5e7db2a4a0522cce94c7b9825a99064a440a88bc
|
| 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 logSecurity for xkcdkit 0.6.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.
Attestation for xkcdkit-0.6.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 logA 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.