Python Web Framework built for learning purpose
Project description
Jala: Python Web Framework built for learning purposes
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file jala-0.1.3.tar.gz.
File metadata
- Download URL: jala-0.1.3.tar.gz
- Upload date:
- Size: 4.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1034ddf559b9bce3cb9341490a57b08e9b145782c80890c80a1e4a033cb4cdbd
|
|
| MD5 |
0949f57303d1e18f98695c6bafbd775a
|
|
| BLAKE2b-256 |
d3e7e8be0fe1b7f3b9905a16f7241f317744c72a621856acdbbf941d5436982a
|
File details
Details for the file jala-0.1.3-py2.py3-none-any.whl.
File metadata
- Download URL: jala-0.1.3-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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0177b16c6b0fd47394f103a08f915f39b2a2a62bf7f9709da802acca92b604ad
|
|
| MD5 |
f814e79ce528af0cc8a2ea66e26cc1f1
|
|
| BLAKE2b-256 |
cb5d27c65d395d4d8efe9964eda3f642688fe7ef9cbec469735b3d306b2ee683
|