0.18.11: Single-file web apps

23 views
Skip to first unread message

Chad Whitacre

unread,
May 22, 2012, 11:44:52 AM5/22/12
to aspen-users
All,

I discovered recently that, as a nice side-effect of switching to Python for the configuration file syntax, Aspen can now be used to write single-file web apps. This is something one sees promoted in lightweight frameworks like Flask and CherryPy. Since Aspen depends so heavily on the filesystem for URL dispatch an Aspen site can end up with a fair number of files and directories on the filesystem for even a small app. But it turns out that you can pack an entire app into a configure-aspen.py script:

    $ cat greetings-program.py 
    from aspen import Response

    def greetings_program(request):
        request.fs = ''
        raise Response(200, "Greetings, program!")

    website.hooks.inbound_early.register(greetings_program)
    $ aspen -p. -f greetings-program.py 

Note that you have to set the project root to the current directory, and here we specify a configuration file called "greetings-program.py" (instead of using the default, "configure-aspen.py").

However, the above example has some warts, and it's slow to always raise Response. I've released 0.18.11 with a tweak to the website object that cleans this up. Now you can do the following:

    $ cat greetings-program.py 
    from aspen import Response

    def greetings_program(request):
        return Response(200, "Greetings, program!")

    website.handler = greetings_program
    $ aspen -p. -f greetings-program.py 

I think it's nice that Aspen can support this single-file pattern.




chad
Reply all
Reply to author
Forward
0 new messages