Pecan Web Framework

1.8.0 · active · verified Thu Apr 16

Pecan is a lean Python web framework (version 1.8.0) that uses an object-dispatching style for routing, inspired by CherryPy, TurboGears, and Pylons. It focuses on building HTTP-based applications with minimal dependencies, rather than being a 'full-stack' solution. The project maintains an active development status with regular releases addressing Python version compatibility and feature enhancements.

Common errors

Warnings

Install

Imports

Quickstart

This minimal example demonstrates how to define a `RootController` with two exposed methods (`index` and `hello`). The `make_app` function initializes the WSGI application using this controller and a simple configuration. To run a Pecan application, the standard method is `pecan serve config.py`, where `config.py` contains the application's configuration. The `if __name__ == '__main__':` block provides a way to run it directly for demonstration purposes without the `pecan` command-line tool, serving on `0.0.0.0:8080`.

import os
from wsgiref.simple_server import make_server

from pecan import make_app, expose

# app.py
class RootController(object):
    @expose()
    def index(self):
        return 'Hello, Pecan!'

    @expose('json')
    def hello(self, name='World'):
        return {'message': f'Hello, {name}!'}

# config.py (simulated for quickstart)
config = {
    'root': 'app.RootController',
    'template_path': 'templates',
    'debug': True,
    'app': {
        'modules': ['app'],
        'static_root': 'public'
    },
    'server': {
        'port': os.environ.get('PORT', '8080'),
        'host': '0.0.0.0'
    }
}

# To run this, you would typically use `pecan serve config.py`
# For a self-contained example:
if __name__ == '__main__':
    app = make_app(config['root'], **config['app'])
    server = make_server(config['server']['host'], int(config['server']['port']), app)
    print(f"Serving Pecan on http://{config['server']['host']}:{config['server']['port']}")
    print("Visit / and /hello?name=Pecan")
    try:
        server.serve_forever()
    except KeyboardInterrupt:
        pass

view raw JSON →