Skip to main content

Python web-framework building for learning purpose

Project description

PY-WEB-FRAMEWORK is a Python web framework build for purposes

purpose PyPy - Version

PY-WEB-FRAMEWORK is a Python web framework build for purposes

Installation

pip install pywebframework-uz

Basic usage

from app import PyFrameBuilding
from middleware import CustomMiddleware

server = PyFrameBuilding()


@server.route('/home', allowed_methods=['put', 'post'])
def home(request, response):
    response.status = 200
    response.text = "Hello from Home page"


@server.route('/about')
def about(request, response):
    response.status = 200
    response.text = "Hello from About  page"


@server.route('/about2')
def about2(request, response):
    response.status = 200
    response.text = "Hello from About  page"


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


@server.route('/lessons')
class Lessons:

    def get(self, request, response):
        response.text = "Hello from Lessons class, GET method is called"

    @server.route('/post')
    def post(self, request, response):
        response.text = "Lesson is create, POST method is called"


def new_handler(request, response):
    response.text = 'From new handler'


server.add_route('/new-handler', new_handler


def on_exception(req, resp, exc):
    resp.text = str(exc)


server.add_exception_handler(on_exception)


@server.route('/exception')
def exception_handler(request, response):
    raise AssertionError("Something Bad Happened")


class SimpleMiddleware(CustomMiddleware):


    def process_request(self, req):
        print('The process request came')

    def process_response(self, req, resp):
        print("The response has been generated!")


server.add_middleware(SimpleMiddleware)

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 PYWEBFRAMEWORK. The first one is app which is an instance of the main API class:

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


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

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

The other one is test_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_alternative_route(app, test_client):
    def new_handler(request, response):
        response.text = 'From new handler'

    app.add_route('/new-handler', new_handler)

    assert test_client.get('http://testserver/new-handler').text == 'From new handler'

TEMPLATES

The default folder for templates is 'template'. You can change it when initializing the 'PyFrameBuilding' class.

@server.route('/template')
def template_handler(request, response):
    response.body = server.template(
        'home.html', context={
            "new_title": 'New Title',
            "new_body": f"Hello 1243"
        }
    )

Static files Just like templates, the default folder for static files is static and you can override it: server = PyFrameBuilding(static_dir='path/to/static/files/)

<html>
    <header>
        <title>{{new_title}}</title>

        <link rel="stylesheet" href="/static/home.css">
    </header>
    <body>
        {{new_body}}
    </body>

</html>

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

pywebframework_uz-0.1.4.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.

pywebframework_uz-0.1.4-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pywebframework_uz-0.1.4.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.4

File hashes

Hashes for pywebframework_uz-0.1.4.tar.gz
Algorithm Hash digest
SHA256 6da7b65d3635be48605bcea8729324cdec4f5ccedf021a663c1260a953316f62
MD5 c45881e3e15d01a7c3929c596948f866
BLAKE2b-256 8d0fae4c05393d18da23c32ccc26e926db151cf7163824d98da73a53cc86b920

See more details on using hashes here.

File details

Details for the file pywebframework_uz-0.1.4-py3-none-any.whl.

File metadata

File hashes

Hashes for pywebframework_uz-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 fa0a94d6f3a752b06842bc63d6382bad16d4343d8cae6482e97cb393c6c27ce1
MD5 08988fcd8a9080237f54fbc4adcb871d
BLAKE2b-256 1167f3da65a44ec6888994a0c8d1614dfb247772355c022d6ee3ba439b085113

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