Skip to main content

Python Web Framework

Project description

AsadStack: Python Web Framework

purpose license PyPI - Version

Description

AsadStack is a Python Web Framework

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

Installation

pip install AsadStack

How to use it

Basic Usage

from Asad_Stack.app import AsadStackApp
app = AsadStackApp()

@app.route("/home", allowed_methods=["get"])
def home(request, response):
    response.text = "That is home page"


@app.route("/about", allowed_methods=["put"])
def about(request, response):
    response.text = "That is about page"


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


@app.route("/books")
class Books:
    def get(self, request, response):
        response.text = "That is books page"

    def post(self, request, response):
        response.text = "That is books post page"


def new_handler(request, response):
    response.text = "That is new handler"


@app.route("/template")
def template_handler(request, response):
    response.html = app.template(
        "home.html",
        context={"new_title": "Best Title", "new_body": "Best Body Asadbek"}
    )

@app.route("/json")
def json_handler(req, resp):
    response_data = {"name": "Asadbek", "type": "json"}
    resp.json = response_data

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 AsadStack. The first one is app which is an instance of AsadStackApp.

def test_route_overlap_throws_exception(app):
    @app.route("/")
    def home(req, resp):
        resp.text = "That is home page"

    with pytest.raises(AssertionError):
        app.route("/")
        def home2(req, resp):
            resp.text = "That is home2 page"

The other one is test_client that you can use to sen HTTP requests to your handlers.It is based on the famous requests and it should feel very familiar:

def test_parameterized_route(app, test_client):
    @app.route("/hello/{name}")
    def greeting(req, resp, name):
        resp.text = f"Hello {name}"
        
    assert test_client.get("http://testserver/hello/Asadtopchik").text == "Hello Asadtopchik"

Templates

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

app = AsadStackApp(template_dir="template_dir_name")

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

@app.route("/template")
def template_handler(request, response):
    response.html = app.template(
        "home.html",
        context={"new_title": "Best Title", "new_body": "Best Body Asadbek"}
    )

Static Files

You can use static folder in your project to serve static files.

app = AsadStackApp(static_dir="static")

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

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{{new_title}}</title>
    <link rel="stylesheet" href="/static/home.css">
</head>
<body>
<h1>{{new_body}}</h1>
<p>This a paragraph</p>
</body>
</html>

Middleware

You can create custom middleware by extending the Middleware class.

from Asad_Stack.middleware import Middleware
from Asad_Stack.app import AsadStackApp

app = AsadStackApp()

class MyMiddleware(Middleware):
    def process_request(self, req):
        print("request is being called", req.url)
        
    def process_response(self, req, resp):
        print("response has been generated", resp.url)


app.add_middleware(MyMiddleware)

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

AsadStack-0.1.2.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

AsadStack-0.1.2-py2.py3-none-any.whl (5.0 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file AsadStack-0.1.2.tar.gz.

File metadata

  • Download URL: AsadStack-0.1.2.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.11.9

File hashes

Hashes for AsadStack-0.1.2.tar.gz
Algorithm Hash digest
SHA256 9847739431c95085dae265765e776d1e85a180eef8a4773c120931c190944b7a
MD5 d17730e40095f7425651898fa44ffef5
BLAKE2b-256 984a29df6719fc2279d9a2c44b2e2099a20049c5fba85b8189fc13cd185b027e

See more details on using hashes here.

File details

Details for the file AsadStack-0.1.2-py2.py3-none-any.whl.

File metadata

  • Download URL: AsadStack-0.1.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.0 CPython/3.11.9

File hashes

Hashes for AsadStack-0.1.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 8720e7288f45b69456addf1d69b3a1ddd3a2b57b8e06f253b8f8b6dfd7f9cab1
MD5 40faa8cfd0138c264db76d8651ac6dfd
BLAKE2b-256 fe412ef88c721f680af7ea6976db631b6ec78e6f5f941a4059e9c3a599d37f2e

See more details on using hashes here.

Supported by

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