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.4.tar.gz (6.1 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.4-py2.py3-none-any.whl (5.1 kB view details)

Uploaded Python 2Python 3

File details

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

File metadata

  • Download URL: jala-0.1.4.tar.gz
  • Upload date:
  • Size: 6.1 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.4.tar.gz
Algorithm Hash digest
SHA256 34362739be47ea7f528c461603f4c145e7d4f43b5db3defe53840abcb3baf858
MD5 5111dff257349f8c3a3629e1a6226704
BLAKE2b-256 cf4ba8610eeda7a4de213c632c0339a27bfd5b8fe2a2b7a47308c7c2a36bac58

See more details on using hashes here.

File details

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

File metadata

  • Download URL: jala-0.1.4-py2.py3-none-any.whl
  • Upload date:
  • Size: 5.1 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.4-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 6ce6b88fac10b5cc154b461ba3e145053bcda121f1fa39f4b7380c5ab747353c
MD5 48d2936b061e0cdb94395b8235c72e39
BLAKE2b-256 7378acd02cdad0a4da50fa3adfc44237de0c2cff6962e7cf56e6efb703b4abb1

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