Skip to main content

PyFrameKit

Image

purpose PyPI - Version

PyFrameKit is a lightweight Python web framework built for learning. It's WSGI-compliant and can be used with servers like Gunicorn.

Installation

To install PyFrameKit, use pip:

pip install pyframekit

Hot to Use

Quick Start Example

Here's how you can createa a basic PyFrameKit application:

from pyframekit.app import PyFrameKitApp

app = PyFrameKitApp()

@app.route("/home")
def home(request, response):
    response.text = "Hello! This is the Home Page"

@app.route("/hello/{name}")
def greeting(request, response, name):
    response.text = f"Hello {name}"

@app.route("/books")
class Books:
    def get(self, request, response):
        response.text = "Book Page"

    def post(self, request, response):
        response.text = "Endpoint to create a book"

Advanced Features

Template Rendering

PyFrameKit supports template rendering for dynamic HTML content:

@app.route("/template")
def template_handler(req, resp):
    resp.html = app.template(
        "home.html",
        context={"new_title": "New Title", "new_body": "New Body"}
    )

JSON Response

Easily handle JSON data:

@app.route("/json")
def json_handler(req, resp):
    response_data = {"name": "some name", "type": "json"}
    resp.body = json.dumps(response_data).encode()
    resp.content_type = "application/json"

Unit Tests

The recommended way of writing unit tests is with pytest. There are two built in fixtures that you may want to use when writing unit tests with PyFrameKit.

def test_duplicate_routes_throws_exception(app):
    @app.route("/home")
    def home(req, resp):
        resp.text = "Hello from Home"

    with pytest.raises(AssertionError):
        @app.route("/home")
        def home2(req, resp):
            resp.text = "Hello from Home2"

The other one is client that you can use to send HTTP requests to your handlers. It is based on the famous request and it should feel very familiar:

def test_parameterized_routing(app, test_client):
    @app.route("/hello/{name}")
    def greeting(request, response, name):
        response.text = f"Hello {name}"

Templates

The default folder for templates is templates. You can customize this location:

app = PyFrameKitApp(templates_dir="path/to/your/templates")

Then you can use HTML files in that folder like so in a handler:

@app.route("/template")
def template_handler(req, resp):
    resp.html = app.template(
        "home.html",
        context={"new_title": "New Title", "new_body": "New Body"}
    )

Static Files

Static files are served from the static directory by default. This location is also configurable:

app = PyFrameKitApp(static_dir="path/to/your/static")

Then you can use the files inside this folder in HTML files:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{{title}}</title>
    <link rel="stylesheet" href="/static/home.css">
</head>
<body>
    <h1>{{body}}</h1>
    <p>This is a paragraph</p>
</body>
</html>

Middleware

Add custom middleware to process requests and responses. Middleware classes inherit from pyframekit.middleware.Middleware and override the process_request and process_response methods:

from pyframekit.app import PyFrameKitApp
from pyframekit.middleware import Middleware

app = PyFrameKitApp()

class Middleware:
    def process_request(self, req):
        print("Before dispatch", req.url)

    def process_response(self, req, resp):
        print("After dispatch", req.url)

app.add_middleware(Middleware)

Release files for pyframekit 0.2.0

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

Source distribution (sdist)

Source distribution for pyframekit 0.2.0
File Size Uploaded
pyframekit-0.2.0.tar.gz 8.1 kB Details

Built distribution (wheel)

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

Total release size: 14.9 kB

Release files / pyframekit-0.2.0.tar.gz

Download URL pyframekit-0.2.0.tar.gz
Size 8.1 kB
Tags Source
SHA-256 checksum
How to use checksums
67e4774c33c7070545b75b8f7723d83de9a157913c974777791b4f164210c870
BLAKE2b-256 checksum
How to use checksums
3e5806ae4b2095aee36538ccec996f052a2dc2b64de0c907f673a9864cf94a75
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.9.6

Release files / pyframekit-0.2.0-py2.py3-none-any.whl

Download URL pyframekit-0.2.0-py2.py3-none-any.whl
Size 6.8 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
0f7ef15e13ae99d7e7f6ef479aa4ab05bb48c231a5b8ebec482a2c57f8eef1c8
BLAKE2b-256 checksum
How to use checksums
ca2c67e3b13fae172b51753881fb4f4655a663b35e084343a5c49d266222db29
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.9.6

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

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