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.3.tar.gz (4.0 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.3-py2.py3-none-any.whl (2.7 kB view details)

Uploaded Python 2Python 3

File details

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

File metadata

  • Download URL: pywebframework_uz-0.1.3.tar.gz
  • Upload date:
  • Size: 4.0 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.3.tar.gz
Algorithm Hash digest
SHA256 462771ac7c42ca9bdd7093d2d21ab030f32aed886f74c3477c94d53ccfdf937e
MD5 5ab9870f82ce63e956f08f7f15f4e841
BLAKE2b-256 c2dd2eccd67d1dfdefb9146a1f4a97649bb4472b93c830abdb123b0e930fee19

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pywebframework_uz-0.1.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 fbebfbf383f41271ef9482a86b2a836604d2cbb663317d49e282fce60cd7c52d
MD5 526c00ad43b8726c00101e7ac993c7bb
BLAKE2b-256 d18d4549d160e5ba3261a8954828381e3d7efab376185ede25912a62451c9cc7

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