Mastodon

Our Tools Are Open: This Site Runs on Them

Three tools for Django developers are now open source. Two turn markdown into pages, and one catches slow database queries in your tests.


Three of the tools this site runs on are now published. spellbook-engine, dj-spellbook and dj-mercury are on PyPI under MPL-2.0, and the Spellbook documentation is live at django-spellbook.org.

What each one does

{~ shelf ~}
{~ package name="spellbook-engine" icon="/static/images/packages/spellbook-engine.svg" href="https://git.stormdevelopments.ca/official-public/spellbook-engine" about="The syntax: finds SpellBlocks, refuses bad documents" /~}
{~ package name="dj-spellbook" icon="/static/images/packages/dj-spellbook.svg" href="https://git.stormdevelopments.ca/official-public/dj-spellbook" about="The Django half: renders each block through a template" /~}
{~ package name="dj-mercury" icon="/static/images/packages/dj-mercury.svg" href="https://git.stormdevelopments.ca/official-public/dj-mercury" about="Counts a test's database trips, fails it on a bug" /~}
{~~}

A SpellBlock, written and rendered

An author writes this in a document.

{~ alert type="warning" ~}
Make a backup **first**.
{~~}

The reader sees this.

The words stay in the document and the markup stays in the template, so the person writing a page never touches HTML and a design change never touches a page.

Make it look like your site

dj-spellbook ships one small stylesheet for its three built-in blocks. It reads eight CSS variables, so a theme is a few lines in your own CSS, loaded after it.

:root {
  --spell-radius: 0;
  --spell-surface: #14110f;
  --spell-warning: #e2a03f;
}

The built-in blocks keep stable class names, such as spell-callout, spell-card and spell-accordion, so your own rules can hang off them. The stylesheet page lists every variable and its default.

The stylesheet is optional. A block you write yourself carries your own classes and your own CSS and needs nothing from it. The steps and the timeline below are styled that way.

Where this site uses them

Every blog post, every page of the guide and every proposal on this site is a markdown document that dj-spellbook renders. The guide and the proposals define blocks of their own. Here are three of them, each with the line an author writes.

One line in the Proxmox guide page becomes the config builder.

{~ s3config app="proxmox" /~}

A list in the Alpha 2 proposal becomes the road a change takes, with the third step marked.

{~ steps here="3" ~}
- Tested locally
- Tested on staging
- The alpha
- Customer nodes
{~~}

Another list becomes a timeline.

{~ timeline ~}
- **Now**: We publish the plan to hear what you think first.
- **A few months out**: Alpha 2 opens, and this proposal closes.
- **Six months later**: The alpha closes. If your data is still on it, we email you every month, every week in the last month, and every day in the last week.
{~~}

Before a deploy rolls, every stored document is checked against the new version. If the engine refuses one, the deploy stops and the old site keeps serving until the document is fixed.

dj-mercury, on our own tests

dj-mercury wraps the tests for the blog and the Storm Buckets dashboard, and a page that asks the database for more than its limit fails its test before it ships. This is the test for the blog post index.

def test_index_returns_200(self):
    with performance_monitor(queries=5):
        resp = self.client.get(reverse('blog:index'))
    self.assertEqual(resp.status_code, 200)

It failed the first time it ran, because the index asked for each post's tags in a way that skipped the ones already loaded, so every post on the page cost one more query. The fix shipped with the test.

Why we published them

These tools are not new. django-spellbook and django-mercury-performance have been public for years, but they were a little messy, and they weren't under the wing of Storm Developments. dj-spellbook and dj-mercury are the rewrites, and the old packages are now deprecated. Moving over is a fresh install under the new package names, and in dj-mercury monitor is now performance_monitor.

We want our framework to be available to everyone. It is part of our sustainability principle to help create a flourishing open source ecosystem.

What this means for you

If you develop with Django, you have new libraries to choose from: one for fast & simple front-end content, and one for fast & simple tests on performance. As long as Storm Developments continues to operate, as long as this website still serves, you can be assured that spellbook-engine, dj-spellbook and dj-mercury will be maintained and updated.

The documentation is hosted on montreal-1

django-spellbook.org is a static site in a Storm Bucket on montreal-1, served on its own domain over HTTPS with an automatic certificate. The same site hosting is open to every bucket.

A small Rust program builds the site and uses the engine crate to read its pages, and every example on it is the saved output of the real dj-spellbook.

Install dj-spellbook

pip install dj-spellbook

It needs Python 3.12 or newer and Django 5.2 or newer, and it installs the engine with it. Add dj_spellbook to INSTALLED_APPS, and the three built-in blocks and their stylesheet are ready. The quick start renders a first document, and Writing your own block covers a block of your own.

Install dj-mercury

pip install dj-mercury

It needs Python 3.12 or newer and Django 5.2 or newer, and Django is its only dependency. The test helper needs no setup. Import it, then wrap the part of a test that loads the page, the way the blog post index test above does.

from dj_mercury import performance_monitor

With no limit written, a block still fails on two things that are bugs on any page. One is the same query sent from the same line of code three or more times, which is a loop asking the database once per row. The other is an exact duplicate, the same query with the same values sent more than once.

Every other limit is yours to write, on one block, at the top of a test file, or for the whole project in settings. This is the one line in ours.

MERCURY_PERFORMANCE_TESTING = {'round_trip_ms': 0.15}

That number is the average delay on one round trip to our database. With it set, a failure says what the page costs in production as well as how many queries it made.

Add dj_mercury to INSTALLED_APPS for the two commands. The first runs only the tests that are wrapped. The second is a hunt, which runs every test you already have, fails nothing, and writes a report of the repeated queries it saw, most common first.

python manage.py mercury
python manage.py mercury --hunt

The README lists every option.

Source, issues and pull requests

The source is on our forge, for spellbook-engine, dj-spellbook and dj-mercury. Signups on the forge are closed for security, so issues and pull requests are welcome on the GitHub mirrors. Every one gets read and a reply, and features in line with the design goals of dj-spellbook and dj-mercury are very welcome.

The forge is the main home of the code. A pull request's change is applied there, with you credited as a co-author on the commit, and the mirror picks it up from there. On GitHub the pull request shows as closed.

Questions

Do I need Django to use spellbook-engine?

No. The engine only reads a document and reports its blocks, from Rust or from Python. dj-spellbook is the part that needs Django.

Can I write my own blocks?

Yes. A block is a class and a Django template in a spellblocks.py file in any installed app, found at startup.

What licence are they under?

MPL-2.0, all three.

Can I use them on a closed-source site?

Yes. Your own code stays yours. If you change a file in one of these packages and distribute it, you share that file. We picked MPL-2.0 because a fix one person makes should reach everyone who uses the tool.

Does dj-mercury time my tests?

It reports the stopwatch time and never fails a test on it. A count is the same on every machine.

Hosted and operated in Canada.