Skip to main content

LumosWeb is web framework, simple and effective usage

Project description

LumosWeb PyPI

  • LumosWeb is web framework written in python
  • It's a WSGI framework and can be used with any WSGI application server such as Gunicorn.
  • PyPI Release

Installation

pip install LumosWeb==<latest_version>
e.g. pip install LumosWeb==0.0.2

Getting Started

Basic usage

@app.route("/home", allowed_methods=["get"])
def home(request, response):
    if request.method == "get":
        response.text = "Hello from the HOME page"
    else:
        raise AttributeError("Method not allowed.")

# Parameterized routes
@app.route("/book/{title}/page/{page:d}", allowed_methods=["get", "post"])
def book(request, response, title, page):
    response.text = f"You are reading the Book: {title}, and you were on Page: {page}"

## Adding a route without a decorator
def handler(req, resp):
    resp.text = "We don't have to use decorators!"

app.add_route("/sample", handler, allowed_methods=["get", "post"])

Unit Test

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 LumosWeb. The first one is app which is an instance of the main API class:

def test_basic_route_adding(api):
    @api.route("/home", allowed_methods=["get", "post"])
    def home(req, resp):
        resp.text = "Lumos is on!"
    with pytest.raises(AssertionError):
        @api.route("/home", allowed_methods=["get", "post"])
        def home2(req, resp):
            resp.text = "Lumos is off!"

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_lumos_test_client_can_send_requests(api, client):
    RESPONSE_TEXT = "Yes it can :)!"

    @api.route("/lumos", allowed_methods=["get", "post"])
    def lumos(req, resp):
        resp.text = RESPONSE_TEXT

    assert client.get("http://testserver/lumos").text == RESPONSE_TEXT

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

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>

  <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 LumosWeb.middleware.Middleware class and overriding its two methods that are called before and after each request:

from LumosWeb.api import API
from LumosWeb.middleware import Middleware

app = API()

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

LumosWeb-0.0.5.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

LumosWeb-0.0.5-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file LumosWeb-0.0.5.tar.gz.

File metadata

  • Download URL: LumosWeb-0.0.5.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.3

File hashes

Hashes for LumosWeb-0.0.5.tar.gz
Algorithm Hash digest
SHA256 4d6333957ca0bff6e80cb1b444043a6442e0c3b3e5c13683ce4e81535c530244
MD5 f4da5cbf8e972919782a1ebc17e96f77
BLAKE2b-256 b2205f25cdb75cc5a27f57c66a56e83f0081cb7c2534ae3422580d131a9b4c0f

See more details on using hashes here.

File details

Details for the file LumosWeb-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: LumosWeb-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 5.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.3

File hashes

Hashes for LumosWeb-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 8d1b87e4c60afde0d23cbcbcd6f901ddcf1d6ae1dc72f4ef9f48500ed6e7c26d
MD5 01a2ab664cfa7972ff1fe808a41c6915
BLAKE2b-256 5b12f9a9de840ab95ac445802e6f96e0e3168240ff89e710e6320dc8c8f76cb6

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page