Skip to main content

Python Web Frmaework built for learning purposes.

Project description

MonicaPF: Python Web Framework built for learning purposes

Purpose PyPI - Version

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
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>{{title}}</title>

<link href="/static/main.css" rel="stylesheet" type="text/css">
</head>

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

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

monicapf-0.1.2.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

monicapf-0.1.2-py2.py3-none-any.whl (5.0 kB view details)

Uploaded Python 2 Python 3

File details

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

File metadata

  • Download URL: monicapf-0.1.2.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.9.8

File hashes

Hashes for monicapf-0.1.2.tar.gz
Algorithm Hash digest
SHA256 21cbf3494a06fd6509e5000809320c9ffccb5609a74dbe45da2b86b03eb98355
MD5 fae4d16869b114b0d26ba47648077c1a
BLAKE2b-256 b0b208722d83a410ad61ce69499046de8650b6b92914d6d8a8f9bfacdec35ab7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: monicapf-0.1.2-py2.py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.9.8

File hashes

Hashes for monicapf-0.1.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 bb1b151b83a523a3777d4c287c8f9ec9224968b86fdc0c67ed57f0afdfdc4387
MD5 bef49ac5589cf4491c446fca41bea808
BLAKE2b-256 e45f89d00836e31395e089e6d1e0fb4d4e2646e0d5483d33074de869d3bc46d4

See more details on using hashes here.

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