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 hashes)

Uploaded Source

Built Distribution

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

Uploaded Python 2 Python 3

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