Skip to main content

Python Web Framework built for learning purposes.

Project description

PyBraineUZ: Python Web Framework built for learning purposes

purpose

PyPI - Version

PyBraineUz is a Python web framework built for learning purpose.

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

Installation

pip install pybraineuz

How to use it

Basic usage:

from pybarineuz.app import PyBraineUz

app = PyBraineUz()


@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 Books:
       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": "New body 123",
              }
       )


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

Unit Tests

The recommended way of writing unit tests is with pytest. There are two built in fixture that you may want to use when writing unit tests with PyBraineUz. The first one is app which is an instance of the main API class:

def test_route_overlap_throws_exception(app):
       @route('/')
       def home(req, resp):
              resp.text = "Welcome home."
       
       with pytest.raises(AssertionError):
              @app.route('/')
              def home2(req, resp):
                     resp.text = "Welcome home2."

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

def test_parameterized_route(app, client):
       @app.route("/{name}")
       def hello(req, resp, name):
              resp.text = f"hey {name}"
       
       assert client.get("http://testserver/matthew").text == "hey matthew"

Templates

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

app = API(templates_dir="templates_dor_name")

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

@app.route("/show/template")
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>
</head>

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

Middleware

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

from pybraineuz.api import API
from pybraineuz.middleware import Middleware


app = API()

class SimpleCustomMiddleware(Middleware):

        def proccess_request(self, req):
            print("Before dispatch", req.url)


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


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

pybraineuz-0.1.2.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

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

pybraineuz-0.1.2-py2.py3-none-any.whl (5.2 kB view details)

Uploaded Python 2Python 3

File details

Details for the file pybraineuz-0.1.2.tar.gz.

File metadata

  • Download URL: pybraineuz-0.1.2.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.12

File hashes

Hashes for pybraineuz-0.1.2.tar.gz
Algorithm Hash digest
SHA256 8350ab1cace17aa8c08db35ab88d2be5964a48858fcad725cb7ada721d223f10
MD5 6dbc4d872ee9527a73ad82a6bf0a8b45
BLAKE2b-256 572b14c18148c7f8439ad7bfd8348f52db2094387c2e022d35fc0a946005258a

See more details on using hashes here.

File details

Details for the file pybraineuz-0.1.2-py2.py3-none-any.whl.

File metadata

  • Download URL: pybraineuz-0.1.2-py2.py3-none-any.whl
  • Upload date:
  • Size: 5.2 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.12

File hashes

Hashes for pybraineuz-0.1.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 babe34dfd91090f2eb7f624fb9262c8e9ca82f7c798061878faefe1dfded5b31
MD5 daf3bde6fa2e7166030b8cd4f9c64049
BLAKE2b-256 ce6d4a9a45c21a578327ba14b8e3ecaf8bebdd52f73687f8937a3e168ef1eb0c

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