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.2.1.tar.gz (7.7 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.2.1-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for pylord-0.2.1.tar.gz
Algorithm Hash digest
SHA256 7410362c48962d20b6f5250ee479519bfcf533718647e55e469a63dd3e34e322
MD5 55af8cf563095a8afd96116c84563888
BLAKE2b-256 899818706f4af1276869b2de38a0d3fcf0daf3b4a3e5fd11886d8e216842aec4

See more details on using hashes here.

File details

Details for the file pylord-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: pylord-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 6.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.10.11

File hashes

Hashes for pylord-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5f015d07257807b48b7c08e553f749c004a69a48a2b16def993d6f8c5d332019
MD5 8b84bfd2bbc5a1482c6735eb5d6b7a58
BLAKE2b-256 129f3c997b464fad4343a74b68453c2a2790c5b661c793577f11f83000df6eb0

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