Skip to main content

Python Web Framework built for learning purpose

Project description

Jala: Python Web Framework built for learning purposes

purpose PyPi - Version

Jala is a Python web framework built for learing purposes

It's a WSGI faramework and can be used with any WSGI application server such as Gunicorn (in windows waitress).

Installation

pip install jala

How to use it

Basic usage:

from jala.app import PyTempFrameApp

app = PyTempFrameApp()

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


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


@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(req, res):
    res.text = "From new handler"

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


@app.route('/template')
def template_handler(req, res):
    res.body = app.template(
        'home.html',
        context = {"new_title":'yangi Title', "new_body":"yap - yangi Body"}
    )

Unit Tests

The recommended way of writing unit test is with pytest. There are two built in fixture that you may want to use when writing unit test with Jala. The first one is app which is an instance of the main API class:

def test_route_overlab_throws_exception(app):
    @app.route("/")
    def home(req, resp):
        resp.text = "Welcome Home."

    with pytest.raises(AssertionError)
    @app.route("/")
        def home2(req, resp):
            resp.text = "Welcome Home2."

The other one is a 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_parameterizing_routing(app, client):
    @app.route("/hello/{name}")
    def greeting(req, res, name):
        res.text = f"Hello {name}"    

        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 the main API() class:

app = API(templates_dir = "template_dir_name")

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

@app.route('/show/template')
def template_handler(req, res):
    res.body = app.template(
        'example.html',
        context = {"new_title":'Awesome Framework', "new_body":"Welcome to the future!"}
    )

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:

<html>

<head>
    <title>{{new_title}}</title>
    <link rel="stylesheet" href="/static/main.css">
</head>

<body>
    <h1> {{ body }} </h1>
    <p>This a paragraph</p>
</body>

</html>v

Middleware

You can write custom middleware classes by inheriting from the jala.middleware.Middleware class overriding its two methods that are called before and after each request:

from jala.app import PyTempFrameApp
from jala.middleware import Middleware

app = PyTempFrameApp()

class SampleCustomMiddleware(Middleware):
    def process_request(self, req):
        print(f"Before dispatch {req.url}")

    def process_response(self, req, res):
        print(f"After dispatch {req.url}")

app.add_middleware(SampleCustomMiddleware)

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

jala-0.1.2.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

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

jala-0.1.2-py2.py3-none-any.whl (2.7 kB view details)

Uploaded Python 2Python 3

File details

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

File metadata

  • Download URL: jala-0.1.2.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.9.6

File hashes

Hashes for jala-0.1.2.tar.gz
Algorithm Hash digest
SHA256 d4b5905b9d4fdedd25f60d63451ace2bacb57257147806188e6b7d594893a99f
MD5 7fa8964a05f86ff435741b2d7cd27555
BLAKE2b-256 a9fcb08236bc391f6203b745bd9989f0c427ead3a8c81efa0796271f86871741

See more details on using hashes here.

File details

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

File metadata

  • Download URL: jala-0.1.2-py2.py3-none-any.whl
  • Upload date:
  • Size: 2.7 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.9.6

File hashes

Hashes for jala-0.1.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 545518db6a6f2cb2b75f89c8119b76cf6949e246883b5e5d0bb78c410f41cf43
MD5 4d672b9c1c3f19bbf0d28e13128c5d7f
BLAKE2b-256 441f54160561f243ca50b9e2ab6a820ac7c199b873cb30c3177cad64a3dbe4a7

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