Python web framework building for APIs
Project description
#PyLord: Python Web Framework built for creating APIs
PyLord 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/Waitress
pip install pylord
How to use it
Basic Usage
from pylord.app import PyLordApp
from pylord.middleware import Middleware
app = PyLordApp()
# you can add allowed methods in you function_handler
@app.route("/home", allowed_methods=["get"])
def home(request, response):
response.text = "hi this is home page"
@app.route("/about")
def about(request, response):
response.text = 'hi this is about page'
@app.route("/hello/{name}")
def generating(request, response, name):
response.text = f"hello {name}"
# working with class
@app.route("/book")
class Books:
def get(self, request, response):
response.text = "this is get method"
def post(self, request, response):
response.text = "endpoint to create a book"
# route your class here
def new_handler(req, resp):
resp.text = "New Handler"
app.add_route("/new_handler", new_handler)
# for creating any exception
def on_exception(req, resp, exc):
resp.text = str(exc)
app.add_exception_handler(on_exception)
@app.route("/exception")
def exception_throwing_handler(req, resp):
raise AttributeError("some exception")
# working with json
@app.route("/json")
def json_handler(req, resp):
response_data = {"name": "asasa"}
resp.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 PyLord. The first one is app which is an instance of the main API class:
def test_route_overlap_throws_exception(app):
@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"hi {name}"
assert client.get("http://testserver/sauron").text == "hi sauron"
Templates
The default folder for templates is templates. You can change it when initializing the main API() class:
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 handler_with_template(req, resp):
resp.html = app.template(
"example.html", context={"title": "Awesome Framework", "body": "welcome to the future!"})
Static Files
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>
<link href="/static/main.css" rel="stylesheet" type="text/css">
</head>
<body>
<h1>{{body}}</h1>
<p>This is a paragraph</p>
</body>
</html>
Middleware
You can create 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 pylord.app import PyLordApp
from pylord.middleware import Middleware
app = PyLordApp()
class SimpleCustomMiddleware(Middleware):
def process_request(self, req):
print("Before dispatch", req.url)
def process_response(self, req, res):
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
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 pylord-0.1.2.tar.gz.
File metadata
- Download URL: pylord-0.1.2.tar.gz
- Upload date:
- Size: 5.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.10.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d52582f5e952a04e06bfe96815a4e5820141c76105f4839dede9ca09ad198ba9
|
|
| MD5 |
71cdacb1776e27f4e14718080d1b6180
|
|
| BLAKE2b-256 |
afe7a194739b404dc438f371fee60093811b08eaebe5de483a8f69746b467ca6
|
File details
Details for the file pylord-0.1.2-py2.py3-none-any.whl.
File metadata
- Download URL: pylord-0.1.2-py2.py3-none-any.whl
- Upload date:
- Size: 5.1 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.10.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8ef39472c2f830e0bc26ee830498934b4c961445d8e0b50ee6dda6347b57267c
|
|
| MD5 |
9246e29a50c0ded1f5a90e51c6b7c2a5
|
|
| BLAKE2b-256 |
166cf9e250f8c017a28fc6e8107b3eeedf4c07a5aa36a6b323334eb21f50b4b2
|