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.2.tar.gz (5.6 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.2-py2.py3-none-any.whl (5.0 kB view details)

Uploaded Python 2Python 3

File details

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

File metadata

  • Download URL: myframeuz-1.0.2.tar.gz
  • Upload date:
  • Size: 5.6 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.2.tar.gz
Algorithm Hash digest
SHA256 90dde294286ac25ad68c78eea5a8fe94fb486d6506e3a41ca7b08bf07fa89164
MD5 a4fbdb3412c7d96639a2ed9510a4d2ba
BLAKE2b-256 9a1da8240416d460c573d760052576290ef93675bfb1837a4dfe202f081a79b4

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for myframeuz-1.0.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 8866766ee54a23abfd94e707e1211c8875d1a334b42f8c17d9433a730eb4fa6f
MD5 26f820f21426d55888b3b308f5140396
BLAKE2b-256 c7305b7b7639f5667cea0e732d04adeebcfd0ea613735e98021ae9bf3a08d168

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