Skip to main content

My python web framework built for learning purposes.

Project description

MyFrameWork: Python Web Framework built for learning purposes

purpose PyPI - Version

MyFrameUz is a Python web framework built for learning purposes.

It's a WSGI framework and can be used with any WSGI application server such as Gunicorn.

Installation

pip install myframeuz

How to use it

Basic usage:

from myframeuz.app import MyFrameApp

app = MyFrameApp()

@app.route("/home")
def home(request, response):
    response.text = "Hello from the HOME page"

@app.route("hello/{name}/")
def greeting(request, response, name):
    response.text = f"Hello, {name}"

@app.route('/books')
class Book:
    def get(self, request, response):
        response.text = "Books Page"

    def post(self, request, response):
        response.text = "Endpoint to create a book"

@app.route('/template')
def template_handler(request, response):
    context = {
        "title":"New Project",
        "content":"New FastApi Project"
               }
    response.html = app.template("home.html", context=context)

@app.route("/json")
def json_handler(request, response):
    response_data = {"name": "same name", "type":"json"}
    response.json = response_data

Unit Tests

The recommended way of writing unit tests is with pytest. There are teo built in fixtures that you may want to use when writing unit tests with MyFrameUz. The first one is 'app' which is an instance of the main 'MyFrameUz' class:

import pytest
from conftest import app
from myframeuz.middleware import Middleware

def test_basic_route_adding(app):
    @app.route('/home')
    def home(request, response):
        response.text = "Hello from Home"

def test_duplicate_routes_throws_exception(app):
    @app.route('/home')
    def home(request, response):
        response.text = "Hello from Home"

    with pytest.raises(AssertionError):
        @app.route('/home')
        def home(request, response):
            response.text = "Hello from Home 2"

Templates

The default folder for templates is 'templates'. You can change it when initializing the main MyFrameApp() class :

app = MyFrameApp(templates="templates_dir_name")

The you can use HTML files in that folder like so in a handler:

@app.route('show/template')
def template_handler(request, response):
    context = {
        "title":"New Project",
        "content":"New FastApi Project"
               }
    response.html = app.template("home.html", context=context)

Static Files

Just like templates, the default folder for static files is 'static' nd you can override it:

app = MyFrameApp(static_dir = "static_dir_name")

The you can use the files inside this folder in HTML files:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Assalom aleykum</title>
    <link rel="stylesheet" href="/static/style.css">
</head>
<body>
    <h1>Welcome to my framework</h1>
    <p>{{title}}</p>
    <p>{{content}}</p>
</body>
</html>

Middleware

You can create custom middleware classes by inhereting from the "myframeuz.middleware.Middleware"
class and overriding its two methods that are called before and after each request:
from myframeuz.app import MyFrameApp
from myframeuz.middleware import Middleware

from webob import Request


class Middleware:
    def __init__(self, app):
        self.app = app

    def add(self, middleware_class):
        self.app = middleware_class(self.app)

    def process_request(self, request):
        pass

    def process_response(self, request, response):
        pass

    def handle_request(self, request):
        self.process_request(request)
        response = self.app.handle_request(request)
        self.process_response(request, response)

        return response

    def __call__(self, environ, start_response):
        request = Request(environ)
        response = self.app.handle_request(request)
        return response(environ, start_response)

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

myframeuz-1.0.1.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

myframeuz-1.0.1-py2.py3-none-any.whl (2.7 kB view details)

Uploaded Python 2Python 3

File details

Details for the file myframeuz-1.0.1.tar.gz.

File metadata

  • Download URL: myframeuz-1.0.1.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.19

File hashes

Hashes for myframeuz-1.0.1.tar.gz
Algorithm Hash digest
SHA256 ba2e6cceac55599e9ec016a36b485565c76bd445fbbc4d2da3335968e6db4eb0
MD5 80c7eada2bb962907e3b516b98964dcf
BLAKE2b-256 0669ecbfb17c1aad6d75ed463cae812ad1d9539560b5b66b8b6820ba8645b9aa

See more details on using hashes here.

File details

Details for the file myframeuz-1.0.1-py2.py3-none-any.whl.

File metadata

  • Download URL: myframeuz-1.0.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.1 CPython/3.9.19

File hashes

Hashes for myframeuz-1.0.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 9ce429830d4401ea50ef62cc91e4f30645fb047d9432b95696d0ad5d36998d5e
MD5 e68d62688df35c0763d657b5b358dfd4
BLAKE2b-256 ae684c0e01ae6665b674b9cec0761a2d8b610013c5a85f78c454b4014b73904b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page