Skip to main content

Python Web Framework

Project description


INX Web Framework

Python Version PyPI

INX is a lightweight Python web framework designed for simplicity and performance. Built for learning purposes, it provides essential features like routing, middleware support, template rendering, and static file serving.

There should be init.py file inside of inxlib file please don't forget


Features

  • Easy Routing: Define routes with support for dynamic URL parameters.
  • Middleware Support: Add custom middleware for request/response processing.
  • Template Rendering: Use Jinja2 templates for dynamic HTML content.
  • Static File Serving: Serve static files (CSS, JS, images) with WhiteNoise.
  • JSON Responses: Easily return JSON data from your routes.
  • Exception Handling: Custom exception handlers for better error management.

Installation

You can install INX Web Framework via pip:

pip install inxlib

## Installation

You can install **INX Web Framework** via pip:

```bash
pip install inxlib

Quick Start

1. Create a Simple App

from inxlib.app import PyInxApp

app = PyInxApp()

@app.route("/")
def home(request, response):
    response.text = "Hello, World!"

if __name__ == "__main__":
    app.run()

Run the App with Waitress

You can run your app directly using Waitress:

waitress-serve --call 'app:app'

This command assumes that your app object is defined in a file named app.py.


Development vs Production

  • Development: Use the built-in development server for testing and debugging.
  • Production: Use Waitress or another production-ready server like Gunicorn or uWSGI.

Start the Development Server

For development, you can start the server using:

python app.py

Visit http://localhost:8080 in your browser to see "Hello, World!".

Basic Usage

Routing

Define routes with the @app.route decorator:

@app.route("/about")
def about(request, response):
    response.text = "About Us"

Dynamic Routes

Capture URL parameters:

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

Template Rendering

Use Jinja2 templates to render HTML:

@app.route("/template")
def template_handler(req, resp):
    resp.body = app.template(
        'home.html',
        context={"new_title": "Best Title", "new_body": "Best body"}
    )

Static Files

Serve static files (CSS, JS, images) from the static/ directory:

<link rel="stylesheet" href="/static/test.css">

JSON Responses

Return JSON data:

@app.route("/json")
def json_handler(request, response):
    response.json = {"status": "success", "message": "Hello, JSON!"}

Middleware

Add custom middleware:

class LoggingMiddleware:
    def __init__(self, app):
        self.app = app

    def process_request(self, req):
        print(f"Request: {req.url}")

    def process_response(self, req, resp):
        print(f"Response: {resp.status_code}")

app.add_middleware(LoggingMiddleware)

Example Test File (test_app.py)

import pytest
from inxlib import PyInxApp

@pytest.fixture
def app():
    return PyInxApp()

@pytest.fixture
def test_client(app):
    return app.test_session()

def test_basic_route_adding(app, test_client):
    @app.route("/home")
    def home(req, resp):
        resp.text = "Hello from home"

    response = test_client.get("https://testserver/home")
    assert response.text == "Hello from home"

def test_template_handler(app, test_client):
    @app.route("/test-template")
    def template_handler(req, resp):
        resp.body = app.template(
            'test.html',
            context={"new_title": "Best Title", "new_body": "Best body"}
        )
    
    response = test_client.get("http://testserver/test-template")

    assert "Best Title" in response.text
    assert "Best body" in response.text
    assert "text/html" in response.headers["Content-Type"]

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

inxlib-0.1.2.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

inxlib-0.1.2-py2.py3-none-any.whl (5.1 kB view details)

Uploaded Python 2Python 3

File details

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

File metadata

  • Download URL: inxlib-0.1.2.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for inxlib-0.1.2.tar.gz
Algorithm Hash digest
SHA256 98823ec2c3b6df965d60beec4985c0b820126ae9e6a7b5788f49bff80392a776
MD5 4dc6c8f2ff2234cec72fb94161fd2ec8
BLAKE2b-256 77c8687e80b13347ede9273fe5c762fe11d472ecbcf18f08cc0f6664a741c277

See more details on using hashes here.

File details

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

File metadata

  • Download URL: inxlib-0.1.2-py2.py3-none-any.whl
  • Upload date:
  • Size: 5.1 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for inxlib-0.1.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 654671c3544f1bcc1067497e9dc011cb7e75993554a8eb689d54b3c9ebdbc35d
MD5 a63b9d76d8500da1327a177051bd232d
BLAKE2b-256 4b78629300bc8c71119f347387e0a1ff8a3bec32a8049e76a032dde17d00bd46

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