Python Web Framework built for learning purposes
Project description
PyServeUz : Python Web Framework built for learning purposes
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
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pyserveuz-0.1.2.tar.gz.
File metadata
- Download URL: pyserveuz-0.1.2.tar.gz
- Upload date:
- Size: 6.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b2e6197684016abc1088c7047a7757e7f68b88fb9d64da34bbf5addc8ee9c734
|
|
| MD5 |
b3f22291736d17f8012cbba7a973b9c8
|
|
| BLAKE2b-256 |
4b0fd24d9cf03cb9c80206ba90b5a87e5639295b3694c1f17f08d8bf50564023
|
File details
Details for the file pyserveuz-0.1.2-py2.py3-none-any.whl.
File metadata
- Download URL: pyserveuz-0.1.2-py2.py3-none-any.whl
- Upload date:
- Size: 5.3 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4be0598a3a9123a8bd17d42d1c52e8f5eeb9a7b228457b6594f24b3686634737
|
|
| MD5 |
fa4383f3476fd6e7381a5b476821e8e7
|
|
| BLAKE2b-256 |
a94ac04c5fc33061d8fab91f36be158d284874e17c3c2b7d4d9e1ecc4d6bb788
|