Aspen

A Python web framework that makes the most of the filesystem.
Simplates are the main attraction.

Simplates

Simplates are the file format Aspen uses to represent HTTP resources. They are Aspen’s main attraction. Here’s what a simplate looks like:

"""This is my simplate.
"""
import random

[---]
n = random.choice(range(10))
extra_excitement = "!" * n

[---]
Greetings, program!%(extra_excitement)s

Simplates are a single file with one or more pages. The page separator is [---].

Assuming you’ve gone through the Quick Start, edit index.html.spt with the above content, then refresh.

You’re off and running!

Give it a shot: Anything you define in the first two (Python) pages is available in the third (template) page.

Can you guess what the difference is between the first two Python pages? The first one is run once, the first time the page is hit (not on startup), and again any time the simplate is changed. It’s designed for imports and constants. The second is run once for each request.

The first page has these variables defined and available to you:

__file__   the absolute path to the simplate file (symlinks preserved)

website    an instance of a Website object that gives you access to
           configuration objects

The second page has those, and adds:

request    a request object

response   a response object

The template page uses the template language from python by default, though others are available.

By default, simplate files are presumed to be ASCII; if non-ASCII characters (utf8, for example) are to be included in the file, an encoding must be specifed as per PEP 263, which means putting the phrase 'encoding: utf8' somewhere on the first two lines of the file.

All simplate pages are cached in a compiled state. This cache is invalidated whenever the underlying file changes, so simplate changes are always immediately available. When you change Python libraries or configuration files, aspen can restart automatically. But even this extra second or so is a drag during development. When you change a simplate, the changes are always available immediately, without restarting the server.

Different Kinds of Simplates

Aspen uses the simplate file format to represent four different kinds of resources. Media types are inferred from the file extension.

  1. Static Simplates—Any text/* or application/json file with no \[---] in it is served directly as a static file. Other media types are static unless they start with a docstring or an import.
  2. Rendered Simplates—This is the main kind. It’s what was introduced above. The first two pages are Python and then the third is a templating syntax.
  3. Negotiated Simplates—If a simplate file doesn’t have a file extension, then it can be used to serve multiple media types.

Ack! Mixing logic and presentation?!?!?

Well, I like to think that simplates bring code and presentation as close together as possible without mixing them. There are no arbitrary Python blocks inside of the template page, à la PHP/ASP.

Simplates are more like MVC, but with controllers, views, and templates together in the same file instead of in separate files. Your models live in a Python library, use an ORM, etc. And URL routing is done via the filesystem. There’s all around less boilerplate.

Simplates make it easy to kick off development of an app or feature of an app. Simplates are, in fact, downright slippery. You won’t be able to stop innovating. You just open a single file and start writing Python, JavaScript, CSS, HTML, SQL—whatever. It takes zero wiring to get it on the network for the first time. And as apps/features mature, you will very naturally move code out of your single simplate into separate Python libraries, template files, JSON endpoint simplates, JavaScript libraries, CSS files, etc.

Don’t be scared! This isn’t PHP! It’s MVC with less crap.

History

My friend Steve came up with the original idea for simplates in the early aughts, when we were running a web design shop together. We were heavily into Zope and Plone at the time. This was in the Zope 2 days. The original idea was something like, “What if you could put a Script (Python) and a ZPT in THE SAME FILE!?” Steve has since switched careers to philosophy. I debuted simplates in Aspen 0.8, which came out in December of 2007. I made simplates the main attraction in Aspen 0.9.

Home Request