Skip to main content

The artless and minimalistic web library for creating small applications or APIs.

Project description

artless-core

PyPI Version Development Status PyPI - Python Version Downloads PyPI - License

The artless and minimalistic web library for creating small applications or APIs.

Motivation

An extremely minimalistic framework was needed to create the same minimalistic applications. Those "micro" frameworks like Flask, Pyramid, CherryPie, etc - turned out to be not micro at all). Even a single-module Bottle turned out to be a "monster" of 4 thousand LOC and supporting compatibility with version 2.7.

Therefore, it was decided to sketch out our own simple, minimally necessary implementation of the WSGI library for creating small/simple web app.

Main principles

  1. Artless, fast and small (less then 400 LOC) single-file module.
  2. No third party dependencies (standart library only).
  3. Support only modern versions of Python (>=3.10).
  4. Mostly pure functions without side effects.
  5. Interfaces with type annotations.
  6. Comprehensive documentation with examples of use.
  7. Full test coverage.

Limitations

  • No Async/ASGI support.
  • No WebSocket support.
  • No Cookies support.
  • No multipart/form-data support.
  • No built-in protections, such as: CSRF, XSS, clickjacking and other.

Installation

$ pip install artless-core

Getting Started

from http import HTTPStatus
from os import getenv

from artless import App, Request, Response, ResponseFactory


def get_template(username: str) -> str:
    return f"""
    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="utf-8">
        <title>Say hello</title>
      </head>
      <body>
        <h1>Hello, {username}!</h1>
      </body>
    </html>
    """


def say_hello(request: Request, username: str) -> Response:
    available_formats = {
        "json": ResponseFactory.json({"hello": username}),
        "plain": ResponseFactory.plain(f"Hello, {username}!"),
        "html": ResponseFactory.html(get_template(username)),
    }

    format = request.query.get("format", ["plain"])[0]

    if format not in available_formats:
        return ResponseFactory.create(status=HTTPStatus.BAD_REQUEST)

    return available_formats[format]


def create_application() -> App:
    app = App()
    app.set_routes([("GET", r"^/hello/(?P<username>\w+)$", say_hello)])
    return app


application = create_application()

if __name__ == "__main__":
    from wsgiref.simple_server import make_server

    host = getenv("HOST", "127.0.0.1")
    port = int(getenv("PORT", 8000))

    with make_server(host, port, application) as httpd:
        print(f"Started WSGI server on {host}:{port}")
        httpd.serve_forever()

Run it:

$ python3 app.py
Started WSGI server on 127.0.0.1:8000

Check it:

$ curl http://127.0.0.1:8000/hello/Peter
Hello, Peter!

$ curl http://127.0.0.1:8000/hello/Peter?format=html
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Say hello</title>
  </head>
  <body>
    <h1>Hello, Peter!</h1>
  </body>
</html>

$ curl http://127.0.0.1:8000/hello/Peter?format=json
{"hello": "Peter"}

Need more? See documentation and examples.

Roadmap

  • Add Async/ASGI support.
  • Add plugin support.
  • Add cookies support.
  • Add multipart/form-data support.
  • Add test client.
  • Add benchmarks.
  • Add more examples.
  • Add Sphinx doc.

Related projects

  • artless-template - the artless and small template library for server-side rendering.

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

artless_core-0.2.1.tar.gz (11.2 kB view details)

Uploaded Source

Built Distribution

artless_core-0.2.1-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: artless_core-0.2.1.tar.gz
  • Upload date:
  • Size: 11.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.3

File hashes

Hashes for artless_core-0.2.1.tar.gz
Algorithm Hash digest
SHA256 a8d3dd62da6a5cb6c4a76b23e553eb5c4fce68a43ca4e04045c27d6fabcb7d85
MD5 a8959da2c89ec5d73ff5c8b6323f1d66
BLAKE2b-256 c7841f386c12e1a9aacdbe96971ff68c54269c59d6c89415858418143af1ef81

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for artless_core-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 771d9e812a2aa03b6b37f2dfdac01e5672d8e9a6376cff3c91c6c91b32c5bf17
MD5 4f597e464abeab4a72479f121f559a32
BLAKE2b-256 da52f2ab9e49792acb1550b31107c21fa249146521a5865d71759d67a3ce3e19

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