Skip to main content

Python web framework building for APIs

Project description

#PyLord: Python Web Framework built for creating APIs

purpose

PyPI - Version

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pylord-0.1.1.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

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

pylord-0.1.1-py2.py3-none-any.whl (2.8 kB view details)

Uploaded Python 2Python 3

File details

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

File metadata

  • Download URL: pylord-0.1.1.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.10.4

File hashes

Hashes for pylord-0.1.1.tar.gz
Algorithm Hash digest
SHA256 2b0eca9af3a170c444b4607e46ae9f23fbebcd29d25aef9facf98773c31232e0
MD5 f032f0e23c316f4e65cedeadcb9e7322
BLAKE2b-256 3a26e17dc13385dcfa180a42fcde4594cc45ab0839c5cf91722c5901058252e3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pylord-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/6.0.1 CPython/3.10.4

File hashes

Hashes for pylord-0.1.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 41083e627a3711935f789c88f6cabd6da1b926a3cd2cc80032756e8399ab0cd9
MD5 7376bd3c4f4771e18031f1115dd3c076
BLAKE2b-256 49a6b51711f41c0970449646733acc4d55be26f4da4522d0ddfa2c0aab1e6b1e

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