Skip to main content

Python Web Framework built for learning purposes

Project description

PyServeUz : Python Web Framework built for learning purposes

build

PyPI version

PyServeUz is a lightweight Python web framework created for learning and experimentation.

It follows the WSGI standard and works seamlessly with any WSGI application server such as Gunicorn or uWSGI.

Installation

pip install pyserveuz

How to use it

Basic usage:

from pyserveuz.api import PyServeApp

app = PyServeApp()

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


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


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

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




@app.route("/template")
def template_handler(req,resp):
    resp.html = app.template(
        "home.html",
        context = {
            "new_title":"new title",
            "new_body":"hi bro"
        }
    )


@app.route("/json")
def json_handler(req,resp):
    response_data = {"name":"john","type":"json"}
    resp.json = response_data

Unit tests

The recommended way to write unit tests in PyServeUz is by using pytest.
PyServeUz provides two built-in fixtures that simplify testing your applications.
The first one is app, which represents an instance of the main API class.

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, which you can use to send HTTP requests to your handlers.
It is built on top of the popular requests library,
so the API should feel very familiar if you have used requests before.
With the client fixture, you can easily test endpoints by sending GET, POST,
or other HTTP requests and then asserting the responses in your unit tests.

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

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

Templates

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

'''python app = API(templates_dir = "templates_dir_name") '''

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

@app.route("/show/template")
def template_handler(req,resp):
    resp.html = app.template(
        "home.html",
        context = {
            "new_title":"new title",
            "new_body":"hi bro"
        }
    )

Static Files

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

'''python app = API(static_dir="static_dir_name") '''

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

<!DOCTYPE html>
<html>
    <head>
    <title>{{new_title}}</title>
    <link rel="stylesheet" href="static/home.css">
    </head>

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

</html>

Middleware

PyServeUz provides a simple middleware system that lets you run custom logic before and after each request.

from webob import Request

class CustomMiddleware:
    def __init__(self,app):
        self.app = app


    def add(self,middleware_class):
        self.app = middleware_class(self.app)

    def process_request(self,req):
        pass

    def process_response(self,req,resp):
        pass

    def handle_request(self,request):
        self.process_request(request)
        response = self.app.handle_request(request)
        self.process_response(request,response)

        return response

    def __call__(self,environ,start_response):
        request = Request(environ)
        response = self.app.handle_request(request)
        return response(environ,start_response) 

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

pyserveuz-0.1.1.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

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

pyserveuz-0.1.1-py2.py3-none-any.whl (2.9 kB view details)

Uploaded Python 2Python 3

File details

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

File metadata

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

File hashes

Hashes for pyserveuz-0.1.1.tar.gz
Algorithm Hash digest
SHA256 1c07f57a99679e9b383b76d7b942171ed1aff5f611a0576b55610d0d7e356929
MD5 f5f1e22c839d118260556f4ab0e3059e
BLAKE2b-256 50ca05ff6d29bfc1774981739fe4a7ddc7983e260d1e1c4dc1ae0d9b193c277d

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for pyserveuz-0.1.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 15a2479077015f30b5e07140a41f0b9548b0a3d69b08c6d2e18c863b6851f3ff
MD5 3bae37e08f3e876c3a873664ff774634
BLAKE2b-256 af1669f785c1618b3db4315ab61438e02cc1096dc865df7461bfd4ec73755231

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