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.2.tar.gz (5.8 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.2-py2.py3-none-any.whl (5.2 kB view details)

Uploaded Python 2Python 3

File details

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

File metadata

  • Download URL: oboframework-0.1.2.tar.gz
  • Upload date:
  • Size: 5.8 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.2.tar.gz
Algorithm Hash digest
SHA256 b52faebc6f914a4059978f0e743b0fc1a5521985001775ca917fe34f48f9b65a
MD5 6f7df0cd7ab1de1646b9786730c5ceb0
BLAKE2b-256 12f1f249269e2f8faf7f0e056d09f6a9dc7209e632b2abcdd65594d14f817aa2

See more details on using hashes here.

File details

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

File metadata

  • Download URL: oboframework-0.1.2-py2.py3-none-any.whl
  • Upload date:
  • Size: 5.2 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.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 77c564398941023e49f6b0180db10aef8e10450bd6fd6426af503a296e448d4a
MD5 f7c9650bcffbc9b6902608f6c4a9bdaa
BLAKE2b-256 b1577b8a2bb1a08068918475e77d641de012b93843ccae49d43dc06ff1827335

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