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:

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
@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.4.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

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

Uploaded Python 2 Python 3

File details

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

File metadata

  • Download URL: monicapf-0.1.4.tar.gz
  • Upload date:
  • Size: 5.6 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.4.tar.gz
Algorithm Hash digest
SHA256 76010e6330cfb5322bb1660a8faf8e55606dd6e462d9b00f6fe91a9f43559ee5
MD5 976219144f85589a268d97d882d628e7
BLAKE2b-256 1f4a856852966e81fff9281ff75023f606f9680339883ec5da20b9333ca3cafe

See more details on using hashes here.

File details

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

File metadata

  • Download URL: monicapf-0.1.4-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.4-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 d86e778fe9fadc796d2572480deb3b8f6f7c278487fb9e4caef5140b1062d53a
MD5 12d7e8ca19a7e0d12b54febed527f94e
BLAKE2b-256 12c73be1a9a3210eef69f6ff53b0ae423e52dc613cd0386675b4f4d5ee580520

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