Sand is small Python web framework.
- WSGI compatible
- Routing via decorator or paramaters
- Use class based handlers
- Template support (jinja)
- Static file
- Custom exception handling
- Middleware
$(venv) pip install -r requirements.txt
gunicorn app:app
python setup.py test
For example apps see:
- Basic App
- Advanced App
- Sandcastle an example app using the Sand framework
from sand import Sand
app = Sand()
# using decorators
@app.route("/")
def home(req, resp):
resp.text = "Welcome home"
# using parameters
def about(req, resp):
resp.text = "This is what it's about"
app.add_route("/about", about)
When initialising Sand, the default template directory is /templates
. To use a different directory, use templates_dir
from sand import Sand
app = Sand(templates_dir='path/to/templates')
@app.route("/")
def home(req, resp):
context = {
"name": "Sand",
"title": "Home"
}
resp.body = app.template("index.html", context).encode()
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{{ title }}</title>
</head>
<body>
<h1>The name of the framework is {{ name }}</h1>
</body>
</html>
Like templates, when initializing sand, the default static directory is /static
. To use a different directory, use static_dir
from sand import Sand
app = Sand(static_dir='path/to/static')
...
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{{ title }}</title>
<link href="/static/main.css" type="text/css" rel="stylesheet">
</head>
<body>
<h1>The name of the framework is {{ name }}</h1>
</body>
</html>
from sand import Sand
app = Sand()
def custom_exception_handler(req, resp, exc_class):
resp.text = "Oops! Something went wrong."
app.add_exception_handler(custom_exception_handler)
...
from sand import Sand
from sand.middleware import Middleware
app = Sand()
class LogMiddleware(Middleware):
def process_request(self, req):
print("RX: {} {} {} - {}".format(req.client_addr, req.method, req.path, req.user_agent))
def process_response(self, req, resp):
print("TX: {} {} {} {} - {}".format(req.client_addr, req.method, req.path, resp.status, req.user_agent))
app.add_middleware(LogMiddleware)
...