Skip to main content

My Pyhton Web Framework, Based on the testdriven.io tutorial "Building your own Python web framework".

Project description

MPWF

Based on the testdriven.io tutorial "Building your own Python web framework".

purpose

It's a WSGI framework and can be used with any WSGI application server such as Gunicorn.

This documentation is an adapted copy of the original one from the tutorial.

Installation

pip install mpwf

How to use it

Basic usage:

from mpwf.api import API

app = API()

# Simple route
@app.route("/home")
def home(request, response):
    response.text = "Hello from the HOME page"

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

# Class Based 
@app.route("/book")
class BooksResource:
    def get(self, req, resp):
        resp.text = "Books Page"

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

# With Template  
@app.route("/templates")
def template_handler(req, resp):
    resp.html = app.template("index.html", context={"name": "MPWF", "title": "A title"})

# JSON  
@app.route("/templates")
def template_handler(req, resp):
    resp.json = {"name": "data", "type": "JSON"}

Templates

The default folder for templates is templates. You can change it when initializing the main API() class:

app = API(templates_dir="templates_dir_name")

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

@app.route("/show/templates")
def handler_with_template(req, resp):
    resp.html = app.template(
        "example.html", context={"title": "Awesome Framework", "body": "welcome to the future!"})

Static Files

Just like templates, the default folder for static files is static and you can override it:

app = API(static_dir="static_dir_name")

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 href="/app/static/main.css" rel="stylesheet" type="text/css">
</head>

<body>
    <h1>{{body}}</h1>
    <p>This is a paragraph</p>
</body>
</html>

Middleware

You can create custom middleware classes by inheriting from the bumbo.middleware.Middleware class and overriding its two methods that are called before and after each request:

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

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


app.add_middleware(SimpleCustomMiddleware)

Custom Exception Handling

You can create custom exception handlers like so:

def custom_exception_handler(request, response, exception_cls):
    if request.method == 'GET':
        response.text = f"Error: {str(exception_cls)}"

# Then add it to the framework: 
app.add_exception_handler(custom_exception_handler) 

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

mpwf-0.0.3.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mpwf-0.0.3-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file mpwf-0.0.3.tar.gz.

File metadata

  • Download URL: mpwf-0.0.3.tar.gz
  • Upload date:
  • Size: 6.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.2

File hashes

Hashes for mpwf-0.0.3.tar.gz
Algorithm Hash digest
SHA256 22a4507a065c9da0391e1f687cf0e112e0c741c94e4ce558c130e341d0cc3b33
MD5 12f0e73f9844c6e92f17f67c843a4cb1
BLAKE2b-256 bfb6dd8426eac263fdf46f437bea9a1cf42b44e30ff8d87a4a0d5073d92084cc

See more details on using hashes here.

File details

Details for the file mpwf-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: mpwf-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 4.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.2

File hashes

Hashes for mpwf-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 6b2674dd44e43d5232cd70a6b3ebf6b8c472d1a42a80908d9b115036b8b82291
MD5 a8059d2723b7fb93f724bf84ca17272a
BLAKE2b-256 330f13ba2d517f0793f12d12e1f8bc725365ee67fec3260a65516c86676b0116

See more details on using hashes here.

Supported by

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