Skip to main content

Vial

PyPI Python License: MIT

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

How a request reaches 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:

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.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for vialframe 0.1.2
File Size Uploaded Provenance Actions
vialframe-0.1.2.tar.gz 7.8 kB Without provenance Details for vialframe-0.1.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for vialframe 0.1.2
File Interpreter ABI Platform Actions
vialframe-0.1.2-py2.py3-none-any.whl Python 3, Python 2 none any Details for vialframe-0.1.2-py2.py3-none-any.whl

Total release size: 14.7 kB

Release files / vialframe-0.1.2.tar.gz

Download URL vialframe-0.1.2.tar.gz
Size 7.8 kB
Tags Source
SHA-256 checksum
How to use checksums
78d815439c7560a20e752784a2531c6bc56d16cd5f2d755aa42e3a49cc82d91f
BLAKE2b-256 checksum
How to use checksums
4af89fb2795a1d40a008f177bd70a663a59eb223aab55bcab6e3253e591c96ef
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.2-py2.py3-none-any.whl

Download URL vialframe-0.1.2-py2.py3-none-any.whl
Size 6.9 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
eacb6678b29839e8b4f4184fe4f3a73e068cc10e31db81d7a58932ca3ed09b15
BLAKE2b-256 checksum
How to use checksums
f547965cc447a000e806b42b076942a276d610b55c519f22be852b74e4659ae7
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.2

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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page