Skip to main content

Python web framework built for learning purposes

Project description

PyWebhive: Python Web Framework built for learning purposes

purposes PyPI - Version

PyWebhive is a Python web framework built for learning purposes

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

Installation

pip install pywebhive

How to use it

Basic Usage:

from pywebhive.api import PyWebHiveApp

    
app = PyWebHiveApp()


# Simple text response handlers
@app.route("/home", allowed_methods=["get"])
def home(request, response):
    response.text = "Hello from the Home Page"


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


# Class-based handlers
@app.route("/books")
class Books:
    def get(self, request, response):
        response.text = "Books page"
    
    def post(self, request, response):
        response.text = "Endpoint to create a book\n"


# Explicit addition of handlers
def new_handler(request, response):
    response.text = "From new handler"

app.add_route("/new-handler", new_handler)


# Templates support
@app.route("/template")
def template(req, resp):
    resp.body = app.template(
        "home.html",
        context = {"new_title": "Best Title", "new_body": "Best Body"}).encode()
        


# JSON response handlers
@app.route("/json")
def json_handler(request, response):
    response_data = {"name": "some name", "type": "json"}
    response.json = response_data

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 PyWebhive. The first one is "app" which is an instance of the main 'API' class.

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


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

    assert test_client.get("http://testserver/hello/Sam").text == "Hello Sam"


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

    response = test_client.get("http://testserver/books")
    assert response.text == "Books page"
    assert response.status_code == 200  


### How we can use Templates?
### Using Templates
### We have to make dir name called temps
    @app.route("/temp")
    def template_handler(req, resp):
        resp.html = app.template(
            "home.html",
            context = {"new_title": "New title", "new_body": "New body"}
        )

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 lnside this folder in HTML files:

<!DOCTYPE html>
<html lang="en">

 <html>
 <header>
     <meta charset="UTF-8">
     <title>{{new_title}}</title>

     <link href="/static/home.css" rel="stylesheet" href="/static/home.css">
 </header>

 <body>
     {{new_body}}
 </body>
</html>

Middleware

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

from bumbo.api import API
from bumbo.middleware import Middleware

app = API()

class SimpleCustomMiddleware(Middleware):
    def __init__(self, app):
        self.app = app

    def process_request(self, req):
        print(f"Request: {req.url}")

    def process_response(self, req, resp):
        print(f"Response: {resp.status_code}")

app.add_middleware(SimpleCustomMiddleware)

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

pywebhive-0.1.1.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

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

pywebhive-0.1.1-py2.py3-none-any.whl (9.8 kB view details)

Uploaded Python 2Python 3

File details

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

File metadata

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

File hashes

Hashes for pywebhive-0.1.1.tar.gz
Algorithm Hash digest
SHA256 6e65f8d51846bfc1d5b0cb2e75be2654fd29ece7f5e182102d9e4329fbb9f027
MD5 d8a185ed31037f29220618ca12a192d3
BLAKE2b-256 71f36386a6064c1ed1c507631e5e9660aff6acb0aa3468a4c4b0ab3aa01c6095

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pywebhive-0.1.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 9.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 pywebhive-0.1.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 4ce949a866b76f624bd95b216c45496b780c28f301e37f2879f3b934a4dbbf04
MD5 38b54545f59157788670b99440b9f8b6
BLAKE2b-256 722407036cb2a37268da75785682762b143c1d6c6c709aebbdd676b95f806893

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