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.1.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

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

Uploaded Python 2Python 3

File details

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

File metadata

  • Download URL: AsadStack-0.1.1.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.1.tar.gz
Algorithm Hash digest
SHA256 b08c7a2dab4b63c75e6534341348f40de088c61845c69802bcb63f7c826c253d
MD5 52f5f4b01180e190ceb45c252818144a
BLAKE2b-256 06284898a7161019877761039b61ad9cf7f8cb93235767f62d2f78456ce2b922

See more details on using hashes here.

File details

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

File metadata

  • Download URL: AsadStack-0.1.1-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.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 3b3622822cb4f2fb5b5e0a54eaca0fc9c213a1a39521ce3f75ea0d809d0b4cb7
MD5 64bc3ae96d60ca38bca79c1dc2c5f6a4
BLAKE2b-256 c013657290965626ea2e92e104d399ac1a0f90187fc74e6c2e0c4580eb598b40

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