Skip to main content

LumosWeb is web framework, simple and effective usage

Project description

LumosWeb PyPI

  • To ensure compatibility and access the latest features and improvements, it is highly recommended to use version 1.0.0 or higher of the package.
  • 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
  • Sample App

Installation

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

Getting Started

Basic usage

Define App

from LumosWeb.api import API()
app = API()  # We created our api instance
@app.route("/home", allowed_methods=["get", "post", "put", "delete"])
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"])

Run Server

Navigate to the directory in the Terminal where the file of your API instance is located

Lumosweb --app <module_name> run

And lights are on!

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 or Markdown 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!"})

@app.route("/md-files", allowed_methods=["get"])
def index(req, resp):
    resp.html = app.template("index.md")

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)

Database

You can create custom middleware classes by inheriting from the LumosWeb.orm.Database class First create models file and create a class for each table in the database

# models.py

from LumosWeb.orm import Table, Column

class Book(Table):
   author = Column(str)
   name = Column(str)

Then create a storage file and import the models

# storage.py

from models import Book

class BookStorage:
    _id = 0

    def __init__(self):
        self._books = []

    def all(self):
        return [book._asdict() for book in self._books]

    def get(self, id: int):
        for book in self._books:
            if book.id == id:
                return book

        return None

    def create(self, **kwargs):
        self._id += 1
        kwargs["id"] = self._id
        book = Book(**kwargs)
        self._books.append(book)
        return book

    def delete(self, id):
        for ind, book in enumerate(self._books):
            if book.id == id:
                del self._books[ind]

Now you can use them

# app.py

from LumosWeb.orm import Database

db = Database("./lumos.db")  # lumos.db is the name of the database file
# which will be created in the current directory (if it doesn't exist already)
db.create(Book)

@app.route("/", allowed_methods=["get"])
def index(req, resp):
   books = db.all(Book)
   resp.html = app.template("index.html", context={"books": books})

@app.route("/books", allowed_methods=["post"])
def create_book(req, resp):
   book = Book(**req.POST)  # Creates a Book instance with the given data in the request.
   db.save(book)

   resp.status_code = 201  # Created
   resp.json = {"name": book.name, "author": book.author}

@app.route("/books/{id:d}", allowed_methods=["delete"])
def delete_book(req, resp, id):
   db.delete(Book, id=id)
   resp.status_code = 204  # No content (resource has successfully been deleted.)

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-1.2.0.tar.gz (9.4 kB view details)

Uploaded Source

Built Distribution

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

LumosWeb-1.2.0-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for LumosWeb-1.2.0.tar.gz
Algorithm Hash digest
SHA256 1ac181fba138b8558d7b089294871a12defb9de53bf562ff2ee3c23471b7438b
MD5 97d1a88edf863d0ce925c0718fa17d3b
BLAKE2b-256 917997ca825293b49bfcb42512e0682229d3173de6706ac2b28ed295a074fc96

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for LumosWeb-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 495f6cf872ea531f78ec9948ad5fbe4eef60f6a1b5829f9fbd7a01a7fa03d8f0
MD5 3432a3d0b7f31837d0069472cc69389f
BLAKE2b-256 4d9704429dc101359f7f5dbeaacc98b8ef39a326dcb4c9666c65f62a8a77ff02

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