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

Uploaded Source

Built Distribution

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

Uploaded Python 2 Python 3

File details

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

File metadata

  • Download URL: monicapf-0.1.3.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.3.tar.gz
Algorithm Hash digest
SHA256 0620a7e7ecea6112d349ad02115e14c6cf2cb915f0324383341d569d13a632a4
MD5 94c65ec5a4506090c9898115b2d08580
BLAKE2b-256 c8226527ea7d59a376586e24bb65c9ae78bff6a5256e2fb1810f9a4b3c6775f0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: monicapf-0.1.3-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.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 12e9da848c7c2435edde80e0801a3c9ee8c3f8c7d74a55d12af517068da61947
MD5 8a63fd0666143aaaa902239b91459a8e
BLAKE2b-256 b2a39194ca8d662fe5dba14af58d62eb4ccc9668985b407283628c94a656c8e0

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