A simple Python WSGI framework built for learning purposes
Project description
Oberoon: Python Web Framework
Oberoon is a WSGI framework for Python, written for mainly learning purposes. Can be used with any WSGI server such as Gunicorn.
Soon, the author is planning to release his own WSGI server - Obicorn, then you can easily integrate the framework with it.
Source code: https://github.com/Samandar-Komilov/oberoon
Installation
pip install oberoon
How to use it
Basic Usage
from oberoon.app import Oberoon
app = Oberoon()
# Simple text response handlers
@app.route("/home", allowed_methods=["get"])
def home(request, response):
response.text = "Hello from the Home Page"
# Parametrized handlers
@app.route("/hello/{name}")
def greeting(request, response, name):
response.text = f"Hello {name}"
# Class-based handlers
@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\n"
# Explicit addition of handlers
def new_handler(request, response):
response.text = "From new handler"
app.add_route("/new-handler", new_handler)
# Templates support
@app.route("/template")
def template(req, resp):
resp.body = app.template(
"home.html",
context = {"new_title": "Best Title", "new_body": "Best Body"}
).encode()
# JSON response handlers
@app.route("/json")
def json_handler(request, response):
response_data = {"name": "some name", "type": "json"}
response.json = response_data
Unit Tests
The recommended way of writing unit tests is with pytest. Here is an example:
def test_basic_route_adding(app):
@app.route('/home')
def home(req, resp):
resp.text = "Hello from Home"
def test_parametrized_routes(app, test_client):
@app.route("/hello/{name}")
def greeting(request, response, name):
response.text = f"Hello {name}"
assert test_client.get("http://testserver/hello/Sam").text == "Hello Sam"
def test_class_based_get(app, test_client):
@app.route("/books")
class Books:
def get(self, request, response):
response.text = "Books page"
response = test_client.get("http://testserver/books")
assert response.text == "Books page"
assert response.status_code == 200
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
oberoon-0.1.2.tar.gz
(7.2 kB
view details)
Built Distribution
File details
Details for the file oberoon-0.1.2.tar.gz
.
File metadata
- Download URL: oberoon-0.1.2.tar.gz
- Upload date:
- Size: 7.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 91829c3cc6afd6a59ed8e7cc2d3e37d23e520ee2df6089b4b2a43484baba1397 |
|
MD5 | a086b9f705af8e4d5490c8ea5f2e6e5d |
|
BLAKE2b-256 | 9098754a63f56241070ba7a0683967c073a6f4c2640a260ab7a646ee59596ffe |
File details
Details for the file oberoon-0.1.2-py2.py3-none-any.whl
.
File metadata
- Download URL: oberoon-0.1.2-py2.py3-none-any.whl
- Upload date:
- Size: 6.5 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 843d8479c34d0c2d891d0b3b40272b75ef1322c1f1ed6e488bd431dabf70298b |
|
MD5 | d43b09ee23974a4831e35803560a5235 |
|
BLAKE2b-256 | d22f6b4ce581429f9559269d26646164d7ea4e5687dfbc9b39e39d5d2c4ac0af |