Vial
Vial is a small WSGI web framework. It is meant to be quick to start with, and small enough that you can read the framework when you want to know exactly what it does.
It does not pick your database, your project layout, or your server yet. You write handlers, Vial routes the request, and any WSGI server runs the app.
PyPI: https://pypi.org/project/vialframe/
Source: https://github.com/Alimov-8/vial
A web server accepts the HTTP request. A WSGI server calls your Vial app. Vial matches a route, runs the handler, and returns the response.
Requirements
Python 3.13.2 or newer.
Vial stands on a few libraries:
- WebOb for the request.
- parse for path parameters.
- Jinja2 for templates.
- WhiteNoise for static files.
Installation
$ pip install vialframe
The package name on PyPI is vialframe. You import it as vial.
Example
Create it
Create a file app.py:
from vial.app import Vial
app = Vial()
@app.route("/")
def index(request, response):
response.text = "Hello, Vial"
@app.route("/hello/{name}")
def greeting(request, response, name):
response.text = f"Hello, {name}"
@app.route("/json")
def json_handler(request, response):
response.json = {"framework": "vial"}
Run it
Create a file run.py next to it:
from wsgiref.simple_server import make_server
from app import app
with make_server("127.0.0.1", 8000, app) as server:
print("Serving on http://127.0.0.1:8000")
server.serve_forever()
$ python run.py
Serving on http://127.0.0.1:8000
For anything beyond a local check, use a production WSGI server:
$ pip install gunicorn
$ gunicorn app:app
Check it
Open http://127.0.0.1:8000/. The response body is:
Hello, Vial
Open http://127.0.0.1:8000/hello/Ada:
Hello, Ada
Open http://127.0.0.1:8000/json:
{"framework": "vial"}
You already have an app that:
- Serves
GET /with a plain-text body. - Reads a path parameter from
/hello/{name}. - Returns JSON, with
Content-Type: application/json.
An unknown path returns 404 and the body Not Found.
Routing
Every handler takes request and response. Path parameters are extra arguments.
The decorator and add_route do the same thing:
@app.route("/about")
def about(request, response):
response.text = "About"
def new_page(request, response):
response.text = "New page"
app.add_route("/new-page", new_page)
Registering the same path twice raises AssertionError.
Class-based handlers
One class owns one URL. The method name is the HTTP verb. A verb you did not define returns 405 Method Not Allowed.
@app.route("/books")
class Books:
def get(self, request, response):
response.text = "Read books"
def post(self, request, response):
response.text = "Create a book"
Allowed methods
A function handler accepts every method unless you list the ones you want:
@app.route("/home", allowed_methods=["get"])
def home(request, response):
response.text = "Hello"
POST /home then returns 405.
Request and response
request is a WebOb request. request.method, request.path, request.url, request.GET, request.POST, and request.body are the ones you will use most. The rest is in the WebOb request reference.
Set one attribute on response. Vial fills in the body and Content-Type.
| You set | Content-Type | Example |
|---|---|---|
response.text |
text/plain |
response.text = "Hello" |
response.html |
text/html |
response.html = "<h1>Hello</h1>" |
response.json |
application/json |
response.json = {"ok": True} |
response.status_code defaults to 200.
Templates
Put Jinja2 templates in a templates/ directory and run the server from the project root. app.template renders one and you assign it to response.html.
templates/home.html:
<h1>{{ title }}</h1>
<p>{{ body }}</p>
@app.route("/page")
def page(request, response):
response.html = app.template(
"home.html",
context={"title": "Vial", "body": "Hello"},
)
Pass another folder when you construct the app: Vial(templates_dir="theme").
Static files
Files in static/ are served from the site root. static/styles.css is available at /styles.css.
app = Vial(static_dir="assets")
Exceptions
With no handler, an exception propagates to the WSGI server. Add one and Vial calls it instead:
def on_exception(request, response, exception):
response.status_code = 500
response.text = "Something went wrong"
app.add_exception_handler(on_exception)
Middleware
Subclass Middleware and override process_request and process_response. Call super().__init__(app).
The last middleware you add runs first on the way in and last on the way out.
from vial.middleware import Middleware
class LoggingMiddleware(Middleware):
def process_request(self, request):
print("request", request.url)
def process_response(self, request, response):
print("response", request.url)
app.add_middleware(LoggingMiddleware)
Testing
app.test_session() returns a Requests session bound to the app. No server, no port.
from vial.app import Vial
def test_home():
app = Vial()
@app.route("/home")
def home(request, response):
response.text = "Home"
client = app.test_session()
response = client.get("http://testserver/home")
assert response.status_code == 200
assert response.text == "Home"
Use any host under http://testserver. That prefix is what the session is mounted on.
Development
Clone the repo if you want to change Vial itself:
$ git clone https://github.com/Alimov-8/vial.git
$ cd vial
$ python -m venv .venv
$ source .venv/bin/activate
$ pip install -e .
$ pip install -r requirements.txt
$ pytest
$ pytest --cov=vial
main.py is a sample app that uses the features above. pip install -e . installs that checkout, so imports use your local code.
License
MIT. See LICENSE.
Metadata
Release files for vialframe 0.1.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| vialframe-0.1.3.tar.gz | 7.9 kB | Without provenance | Details for vialframe-0.1.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| vialframe-0.1.3-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details for vialframe-0.1.3-py2.py3-none-any.whl |
Total release size: 14.8 kB
Release files / vialframe-0.1.3.tar.gz
| Download URL | vialframe-0.1.3.tar.gz |
|---|---|
| Size | 7.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8b96929ce221afb6b97d2616c3470a87ceece1ee4250a3e34414c909b126cd55
|
|
BLAKE2b-256 checksum How to use checksums |
f6e51b78fb3bc0ac0ba52f67ca23b58c0d7e68d0b200150f1351f64058b7e5b3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.2
|
Release files / vialframe-0.1.3-py2.py3-none-any.whl
| Download URL | vialframe-0.1.3-py2.py3-none-any.whl |
|---|---|
| Size | 6.9 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
96d27d684add927f9374a987658156fa7315918b543f620d47863db1a04ce268
|
|
BLAKE2b-256 checksum How to use checksums |
67002592c1c24ce62ddc11091125b0f879ad07935d6ed738e28917c04c5869da
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.2
|
Security for vialframe 0.1.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.