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

pywebframework_uz-0.1.1-py2.py3-none-any.whl (2.7 kB view details)

Uploaded Python 2Python 3

File details

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

File metadata

  • Download URL: pywebframework_uz-0.1.1.tar.gz
  • Upload date:
  • Size: 4.1 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.1.tar.gz
Algorithm Hash digest
SHA256 bbb9d62490f43ffe9771e0eb0fe1b21ecdd1c011523577ad9b40c7b9b2c3554f
MD5 78da7e7fce1d070042a36ed45e11064f
BLAKE2b-256 946798c727200d8c5c939099d742c5964bb926059c982f4b486ce9aafddf5e1a

See more details on using hashes here.

File details

Details for the file pywebframework_uz-0.1.1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for pywebframework_uz-0.1.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 0eaeafa0e4be5a6c8aafe42342cb421170daaa225aff997b0d0e1100fd577507
MD5 b7f20f7e6a2b10052170199d385ecd26
BLAKE2b-256 6bd608b990b242b59ca8587023abd2248282acce0f7b9f8705ee4f4ba30368e6

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