Skip to main content

Python Web Framework built for learning purpose

Project description

OBOFramework: Python Web Framework built for learning purpose

purpose PyPI - Version

OBOFramework: Python Web Framework built for learning purpose It's a WSGI framework and can be used with any WSGI application server such as Gunicorn

Installation

pip install oboframework

How to use oboframework

Basic usage

from oboframework import OBOFramework

app = OBOFramework()
@app.route('/home', allowed_methods=['get'])
def home(request, response):
    response.text = "Hello from the HOME page"


@app.route('/hello/{name}')
def hello(request, response, name):
    response.text = f"Hello, {name}!"


@app.route("/books")
class Books:
    def get(self, request, response):
        response.text = "Books Page"

    def post(self, request, response):
        response.text = "Endpoint to create a book"


def new_handler(request, response):
    response.text = "New handler"

app.add_route("/new-handler", new_handler)


@app.route("/template")
def template_handler(request, response):
    response.html = app.template(
        "index.html", 
        context={"title": "Awesome Framework", "name": "OBOFramework"}
        )

Unit Tests

The recommended way to writing unit tests is with pytest. There are two built in fixtures that you may want to use when unit tests with OBOFramework. 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(request, response):
        response.text = "Hello Home Page"

    with pytest.raises(AssertionError):
        @app.route('/')
        def home2(request, response):
            response.text = "Hello Home Page"

The other one is client that you can use to send HTTP requests to your handlers. It is based on the famous request and it should feel very familiar:

def test_parameterized_route(app, client):
    @app.route("/{name}")
    def hello(request, response, name):
        response.text = f"Hello {name}"

    assert client.get("http://testserver/otajon").text == "Hello otajon"

Templates

The default template for templates is templates. You can change it when initializing your 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("/showe/templates")
def handler_with_templates(request, response):
    response.text = app.template(
        "example.html", content={"title": "Awesome Framework", "body": "Welcome to the awesome framework"}
    )

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 middlewares classes by inheriting from the middleware.Middleware classes and overriding its two methods that are called before and after each request:

from middleware import Middleware

app = API()

class SimpleCustomMiddleware(Middleware):
    def process_request(self, request):
        print("Request received", request.url)
    
    def process_response(self, request, response):
        print("Response received", response.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

oboframework-0.1.1.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

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

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

Uploaded Python 2Python 3

File details

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

File metadata

  • Download URL: oboframework-0.1.1.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.3

File hashes

Hashes for oboframework-0.1.1.tar.gz
Algorithm Hash digest
SHA256 16582a582bac745bd3444a87438f94556ab810729d1c15e4b542233ebd36800a
MD5 a6c88be97172354611e04f9df4bea6fe
BLAKE2b-256 895d762c0e48051edbf232a8759e46bcf63a0cb8912572606767b6fa9b636c41

See more details on using hashes here.

File details

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

File metadata

  • Download URL: oboframework-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.1.0 CPython/3.11.3

File hashes

Hashes for oboframework-0.1.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 6df2489deebb2412c51c4d02af246b29152b42b08f9394ac8fa8cc430d1b5fb1
MD5 96a7b0095d521cc2911d607625f88f1d
BLAKE2b-256 872197c12d6ae1f50c1724884503c338661d1fa9d366a571376397a97a5e7ff2

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