Python Web Frmaework built for learning purposes.
Project description
MonicaPF: Python Web Framework built for learning purposes
MonicaPF it is python web framework
It is WSGI Framework and can be used with any WSGI application server such as Gunicorn
Installation
pip install monicapf
How To Use It:
Basic Usage:
```python
from monicapf.app import Monica
app = Monica()
Add Allowed Methods:
@app.route('/home', allowed_methods=['get'])
def home(request, response):
if request.method == 'GET':
response.text = 'Hello this is home page'
elif request.method == 'POST':
response.text = 'POST '
Simple Route:
@app.route('/about')
def about(request, response):
response.text = 'Hello this is about page'
Parametrize Route:
@app.route('/hello/{name}')
def hello(request, response, name):
response.text = f"Just say hello. Hello {name}"
Class Based Handlers:
@app.route('/books')
class Book:
def get(self, request, response):
response.text = 'Books GET request'
def post(self, request, response):
response.text = 'Books POST request'
def new_handler(req, res):
res.text = 'New handler'
app.add_route('/new-handler', new_handler)
Json data:
@app.route('/json')
def json(req, res):
res.json = {
'name': 'Request',
'Body': 'Json response',
}
```
Add Template
Create "templates" folder and save inside that folder htmls
```python
@app.route('/template')
def template(req, res):
res.body = app.template(
'home.html',
context={'name': 'Bilol', 'title': 'Template working'}
)
```
Add Static Files
Create "static" folder and save inside that folder static files
```html
<title>{{title}}</title>
{{body}}
This is a paragraph
```MiddleWare
You can create custom middleware classes by inheriting from the monicapf.middleware.Middleware class and overriding its two methods that are called before and after each request:
from monicapf.api import API
from minocapf.middleware import Middleware
app = API()
class SimpleCustomMiddleware(Middleware):
def process_request(self, req):
print("Before dispatch", req.url)
def process_response(self, req, res):
print("After dispatch", req.url)
app.add_middleware(SimpleCustomMiddleware)
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 MonicaPF. The first one is app which is an instance of the main API class:
def test_route_overlap_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 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_route(app, client):
@app.route("/{name}")
def hello(req, resp, name):
resp.text = f"hey {name}"
assert client.get("http://testserver/MonicaPF").text == "hey MonicaPF"
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
File details
Details for the file monicapf-0.1.1.tar.gz
.
File metadata
- Download URL: monicapf-0.1.1.tar.gz
- Upload date:
- Size: 4.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.9.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 88cf155e668fc43f64a37303678c4fe1e1cb9172bd125f52991f2038a827acc6 |
|
MD5 | f912498944b47bba82f0109f922e5b77 |
|
BLAKE2b-256 | 06ff1dca21974f102f8cc3071a21b0181b29370af24efb427f7d4fa51e9cff8d |
File details
Details for the file monicapf-0.1.1-py2.py3-none-any.whl
.
File metadata
- Download URL: monicapf-0.1.1-py2.py3-none-any.whl
- Upload date:
- Size: 2.7 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.9.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8f8ce84f219eab068bc4bb0dde9fb5e61fc76fa137ee57bef58859aeeb600cec |
|
MD5 | e8289977b40d51dab72fd878780ce5cf |
|
BLAKE2b-256 | 11e07e938217cade5632d7869c670c31f137e674ae19b522e36cd59a5b147fee |