Pecan Web Framework
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
-
AttributeError: `pecan.state` is not bound to a context-local context.
cause Attempting to access `pecan.request` or `pecan.response` outside the scope of an active HTTP request (e.g., during application startup or in a background thread) or when `use_context_locals` is not enabled.fixEnsure `use_context_locals = True` is set in your application configuration (`config['app']` dict or passed to `make_app`). Access `pecan.request` and `pecan.response` only within controller methods or hooks that are executed during a request's lifecycle. -
TypeError: setup_app() missing 1 required positional argument: 'config' (or similar configuration loading errors)
cause The `pecan serve` command or `pecan.make_app` function was called without a valid configuration file path or a properly structured configuration dictionary.fixWhen using `pecan serve`, ensure you pass the path to a valid Python configuration file (e.g., `pecan serve config.py`). When using `make_app` directly, provide a dictionary for the `config` argument or pass root controller path and app config as keyword arguments, for example: `app = make_app('myapp.controllers.root.RootController', **app_config_dict)`.
Warnings
- breaking Pecan 1.8.0 (and subsequent versions) requires Python 3.10 or higher. Support for Python 3.8 and 3.9 has been removed.
- gotcha Pecan is a lean framework and does not provide out-of-the-box support for common 'full-stack' features like sessions or database integrations.
- breaking Pecan 1.4.0 introduced a requirement for `webob >= 1.8`. Older versions of `webob` may cause unexpected behavior or errors.
Install
-
pip install pecan
Imports
- expose
from pecan import expose
- make_app
from pecan import make_app
- conf
from pecan import conf
- RestController
from pecan.rest import RestController
Quickstart
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