Skip to main content

Python Web Framework built for learning purposes

Project description

Pythora: Python Web Framework built for learning purposes

purpose PyPI - Version

Pythora is a web framework built for learning purposes. It is a WSGI Framework and can be used with any WSGI application server such as Gunicorn.

Installation

pip install pythora

How to use it

Basic Usage:

from pythora.app import Pythora

app = Pythora()

@app.route("/home", allowed_methods=['get'])
def home(request, response):
    response.text = "Hello from Home Page"

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

@app.route("/template")
def template(req, resp):
    resp.body = app.template("test.html", context={"new_title": "Best Title", "new_body": "Best Body"})

Unit Tests

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 Pythora. The first one is app which is an instance of the main Pythora class.

def test_basic_route_adding(app):
    @app.route("/home")
    def home(req, resp):
        resp.text = "Hello from home"

The other one is test_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_parameterized_routing(app, test_client):
    @app.route("/hello/{name}")
    def hello(req, resp, name):
        resp.text = f"Hello {name}"

    assert test_client.get("http://testserver/hello/John").text == "Hello John"
    assert test_client.get("http://testserver/hello/Matthew").text == "Hello Matthew"

Templates

The default folder for templates is templates. You can change it when initializing main Pythora() class.

from pythora.app import Pythora
app = Pythora(template_dir='templates_dir_name')

Then you can use HTML files in that folder like so in a handler:

from pythora.app import Pythora

app = Pythora()

@app.route("/test-template")
def template(req, resp):
    resp.html = app.template('test.html', context={"new_title": "Best Title", "new_body": "Best body "})

Static files

Just like templates, the default folder for static files is static, and you can override it.

from pythora.app import Pythora
app = Pythora(static_dir="static_dir_name")

Then you can use static files inside this directory in HTML file.

<!DOCTYPE html>
<html lang="en">
<head>
    <title>{{new_title}}</title>
    <link rel="stylesheet" href="static/home.css">
</head>
<body>
    <p>{{new_body}}</p>
</body>
</html>

Middleware

You can create custom middleware classes by inheriting from pythora.middleware.Middleware class and overriding its two methods that are called before and after each request.

from pythora.middleware import Middleware
from pythora.app import Pythora
app = Pythora()

class LoggingMiddleware(Middleware):
    def process_request(self, req):
        print("Request is being processed", req.url)

    def process_response(self, req, resp):
        print("Response has been generated", req.url)

app.add_middleware(LoggingMiddleware)

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

pythora-0.1.3.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

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

pythora-0.1.3-py2.py3-none-any.whl (4.9 kB view details)

Uploaded Python 2Python 3

File details

Details for the file pythora-0.1.3.tar.gz.

File metadata

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

File hashes

Hashes for pythora-0.1.3.tar.gz
Algorithm Hash digest
SHA256 e526f6fec85a55dcca9b310ea846bda9d97007b2b1fde7810ee471bc616bdb3e
MD5 3af01d1b029f2f6dbcce6da94e57ce40
BLAKE2b-256 bf4fb37b48539f2714c3f05a222badb2b0086950acded29bef2b539c56688b02

See more details on using hashes here.

File details

Details for the file pythora-0.1.3-py2.py3-none-any.whl.

File metadata

  • Download URL: pythora-0.1.3-py2.py3-none-any.whl
  • Upload date:
  • Size: 4.9 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.3

File hashes

Hashes for pythora-0.1.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 2c4c9ee5087d90b870b51b87131373900b59115adf6f7df933a5a9c06918802e
MD5 a4536635b8638fdd27df7e3c26e7e2fc
BLAKE2b-256 8ee85cb4c8f60af275e06f093c45edbfb4eda3838eb436f0f594309f77d1c50e

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