Skip to main content

Python Web Framework built for learning purposes.

Project description

PyFrameKit

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)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyframekit-0.1.1.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

pyframekit-0.1.1-py2.py3-none-any.whl (2.8 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file pyframekit-0.1.1.tar.gz.

File metadata

  • Download URL: pyframekit-0.1.1.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.9.6

File hashes

Hashes for pyframekit-0.1.1.tar.gz
Algorithm Hash digest
SHA256 894c70f8ca3cf4d9f776f6b0ad3c4e26d80c009fde5b9af316e5585937b64f7f
MD5 8bf50dd1086aeb6972dd108b0063d5ac
BLAKE2b-256 bbe82b7b34017606d8b766c7aaee285e7c3cdf0b6e23064c3d0c39fd57ff0964

See more details on using hashes here.

File details

Details for the file pyframekit-0.1.1-py2.py3-none-any.whl.

File metadata

  • Download URL: pyframekit-0.1.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 2.8 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.9.6

File hashes

Hashes for pyframekit-0.1.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 778fec55018ccd217b2d32ffa22316420beed43e9de5543b42a3706c537a34cd
MD5 3d5ea4a12ff872421f512a2dfa6fbb27
BLAKE2b-256 52e0db79438f475322c64c6dad79c934b2c6e85b9f9454b61e51256494e15abd

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page