Skip to main content

A lightweight Python web framework built for learning purposes.

Project description

Nimbus Web Framework

Python Version License Version PyPI

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


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

Install Nimbus Web Framework via pip:

pip install nimbus-web-framework

Quick Start

1. Create a Simple App

from nimbus import Nimbusapp

app = Nimbusapp()

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

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

2. Run the App

Start the development server:

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(request, response):
    response.html = app.template(
        "test.html",
        context={"title": "Nimbus", "message": "Welcome to Nimbus!"}
    )

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)

Testing

The framework includes a comprehensive test suite to ensure functionality. To run the tests:

  1. Clone the repository:

    git clone https://github.com/yourusername/nimbus-web-framework.git
    cd nimbus-web-framework
    
  2. Install the development dependencies:

    pip install -r requirements.txt
    
  3. Run the tests:

    pytest tests/test_app.py
    

Example Test File (test_app.py)

The test_app.py file includes tests for the following features:

1. Basic Routing

  • Tests if a simple route returns the correct response.
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"

2. Dynamic Routes

  • Tests if dynamic routes with URL parameters work correctly.
def test_dynamic_route(app, test_client):
    @app.route("/hello/{name}")
    def greet(req, resp, name):
        resp.text = f"Hello, {name}!"

    response = test_client.get("https://testserver/hello/Abulqosim")
    assert response.text == "Hello, Abulqosim!"

3. Template Rendering

  • Tests if templates are rendered correctly with context variables.
def test_template_handler(app, test_client):
    @app.route("/template")
    def template(req, resp):
        resp.html = app.template(
            "test.html",
            context={"title": "Nimbus", "message": "Welcome to Nimbus!"}
        )
    response = test_client.get("https://testserver/template")
    assert "Welcome to Nimbus!" in response.text

4. Static Files

  • Tests if static files are served correctly.
def test_static_file_serving(test_client):
    response = test_client.get("https://testserver/static/styles.css")
    assert response.status_code == 200
    assert "text/css" in response.headers["Content-Type"]

5. JSON Responses

  • Tests if JSON responses are returned correctly.
def test_json_response(app, test_client):
    @app.route("/json")
    def json_handler(req, resp):
        resp.json = {"status": "success", "message": "Hello, JSON!"}

    response = test_client.get("https://testserver/json")
    assert response.json() == {"status": "success", "message": "Hello, JSON!"}

6. Middleware

  • Tests if middleware processes requests and responses correctly.
def test_middleware(app, test_client):
    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)

    @app.route("/middleware")
    def middleware_test(req, resp):
        resp.text = "Middleware test"

    response = test_client.get("https://testserver/middleware")
    assert response.text == "Middleware test"

Got it! If you want to include information about template rendering in the README.md, here’s how you can add a section specifically for templates:


Template Rendering

Nimbus Web Framework supports Jinja2 templates for rendering dynamic HTML content. You can easily render templates and pass context variables to them.

Example: Rendering a Template

  1. Create a template file (e.g., test.html) in the templates/ directory:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>{{ title }}</title>
    </head>
    <body>
        <h1>{{ title }}</h1>
        <p>{{ message }}</p>
    </body>
    </html>
    
  2. Use the app.template method to render the template in your route:

    @app.route("/template")
    def template_handler(request, response):
        response.html = app.template(
            "test.html",
            context={"title": "Nimbus", "message": "Welcome to Nimbus!"}
        )
    
  3. When you visit http://localhost:8080/template, the rendered HTML will look like this:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Nimbus</title>
    </head>
    <body>
        <h1>Nimbus</h1>
        <p>Welcome to Nimbus!</p>
    </body>
    </html>
    

Got it! Here's how you can add a Static Files section to your README.md to explain how static files (CSS, JS, images, etc.) are handled in your framework:


Static Files

Nimbus Web Framework uses WhiteNoise to serve static files like CSS, JavaScript, and images. Static files are served from the static/ directory.

Example: Serving Static Files

  1. Create a static/ directory in your project root and add your static files. For example:

    static/
    ├── styles.css
    ├── script.js
    └── images/
        └── logo.png
    
  2. Link to the static files in your HTML templates or responses:

    <link rel="stylesheet" href="/static/styles.css">
    <script src="/static/script.js"></script>
    <img src="/static/images/logo.png" alt="Logo">
    
  3. When you run your app, static files will be automatically served from the static/ directory.

Example: Serving a CSS File

  1. Add a CSS file (styles.css) to the static/ directory:

    body {
        background-color: lightblue;
        font-family: Arial, sans-serif;
    }
    
  2. Link to the CSS file in your HTML template:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" href="/static/test.css">
        <title>Static Files Example</title>
    </head>
    <body>
        <h1>Welcome to Nimbus!</h1>
        <p>This page uses a static CSS file.</p>
    </body>
    </html>
    
  3. When you visit the page, the CSS will be applied.

Testing Static File Serving

You can test static file serving using the following test:

def test_static_file_serving(test_client):
    response = test_client.get("https://testserver/static/styles.css")
    assert response.status_code == 200
    assert "text/css" in response.headers["Content-Type"]
    assert "background-color" in response.text

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

nimbus_web_framework-0.1.4.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

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

nimbus_web_framework-0.1.4-py2.py3-none-any.whl (6.9 kB view details)

Uploaded Python 2Python 3

File details

Details for the file nimbus_web_framework-0.1.4.tar.gz.

File metadata

  • Download URL: nimbus_web_framework-0.1.4.tar.gz
  • Upload date:
  • Size: 7.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.8.20

File hashes

Hashes for nimbus_web_framework-0.1.4.tar.gz
Algorithm Hash digest
SHA256 3ad1058531edc195555984844d1b15ad917c5ec7a047c2bd9d96736fa892da4b
MD5 10e58cd9e4500ead81aecd688c14bbf9
BLAKE2b-256 c523afd982929650bcd7099a1ec49353a5bba708900ea9837ee69e2b65bbed25

See more details on using hashes here.

File details

Details for the file nimbus_web_framework-0.1.4-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for nimbus_web_framework-0.1.4-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 b7956f433c1ad7d535ee6a81daa1eefc9654c7e7cd375acfbcae55ea2735372b
MD5 68951032c6c23ead51224361ef22a1a4
BLAKE2b-256 fc7fb6f431781b91ab0559d1079e8410f87fb3a1251c5eb1146a32f22b7e8631

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