Python Web Framework built for learning purposes.
Project description
PyBraineUZ: Python Web Framework built for learning purposes
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pybraineuz-0.1.1.tar.gz
.
File metadata
- Download URL: pybraineuz-0.1.1.tar.gz
- Upload date:
- Size: 4.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 682f186d6ed8359f926fec577f2035e2fa8797505799738f2f31e8665c992497 |
|
MD5 | 0ad569f08793bba7b300cc8460aebe1a |
|
BLAKE2b-256 | a4049e10df8e01bb09c97868a6f9d774395e15b1ce514b8f3f510d6d67dd7693 |
File details
Details for the file pybraineuz-0.1.1-py2.py3-none-any.whl
.
File metadata
- Download URL: pybraineuz-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/5.1.1 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3214d7533ea576c2edf763c14235aae2a3283e2f371c23e63ec16a32205cd4b8 |
|
MD5 | 4c0b005aa18d1656d4689484327da8cd |
|
BLAKE2b-256 | 41bfe8ff5c812f02e74fd64d4f308c1572f9548427483984c565d2e32c58a19e |