Skip to main content

Web-PyOctopus is a lightweight Python web framework designed for learning and experimentation, inspired by modern frameworks like Django and FastAPI.

Project description

Web-PyOctopus: A Lightweight Python Web Framework

Purpose PyPI

Web-PyOctopus is a lightweight and easy-to-use Python web framework built for learning purposes. It is a WSGI framework, meaning it can be used with any WSGI application server such as Gunicorn.

🔗 View on PyPI


🚀 Installation

Install Web-PyOctopus using pip:

pip install web-pyoctopus

📌 Basic Usage

Create a simple Web-PyOctopus app:

from pyoctopus.api import OctopusAPI

app = OctopusAPI()

@app.route("/home")
def home(request, response):
    response.text = "Hello from the HOME page"

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

Run it with a WSGI server like Gunicorn:

gunicorn app:app

📚 Routing & Class-Based Views

Define class-based views for better organization:

@app.route("/book")
class BooksResource:
    def get(self, req, resp):
        resp.text = "Books Page"

    def post(self, req, resp):
        resp.text = "Endpoint to create a book"

🎨 Template Rendering

Use templates for dynamic content:

@app.route("/template")
def template_handler(req, resp):
    resp.body = app.template(
        "index.html", context={"name": "Web-PyOctopus", "title": "Best Framework"}
    ).encode()

Change the default template directory:

app = OctopusAPI(templates_dir="custom_templates")

Example index.html:

<!DOCTYPE html>
<html>
  <head>
    <title>{{ title }}</title>
  </head>
  <body>
    <h1>Welcome to {{ name }}</h1>
  </body>
</html>

📂 Static Files

By default, static files are served from the static directory. You can change it:

app = OctopusAPI(static_dir="assets")

Use them in HTML:

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

🛠 Middleware Support

Create custom middleware by inheriting from Middleware:

from pyoctopus.middleware import Middleware

class SimpleLoggerMiddleware(Middleware):
    def process_request(self, req):
        print("Request received:", req.url)

    def process_response(self, req, res):
        print("Response sent:", req.url)

app.add_middleware(SimpleLoggerMiddleware)

🧪 Unit Testing

Use pytest for testing. Fixtures app and client help in writing tests:

def test_home_route(client):
    response = client.get("/home")
    assert response.text == "Hello from the HOME page"

Parameterized route testing:

def test_dynamic_route(client):
    response = client.get("/hello/Alice")
    assert response.text == "Hello, Alice!"

📜 License

Web-PyOctopus is an open-source project for educational purposes.


🚀 Happy Coding with Web-PyOctopus! 🎉

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

web-pyoctopus-1.0.2.tar.gz (7.5 kB view details)

Uploaded Source

Built Distribution

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

web_pyoctopus-1.0.2-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file web-pyoctopus-1.0.2.tar.gz.

File metadata

  • Download URL: web-pyoctopus-1.0.2.tar.gz
  • Upload date:
  • Size: 7.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for web-pyoctopus-1.0.2.tar.gz
Algorithm Hash digest
SHA256 422efa384bd4f61e7ffafe0b670d89636b45cccc539a80dc51c7bcaa11296443
MD5 9f2ea447be6c794b5d61b6e1a4545f34
BLAKE2b-256 9e72c138b7f8d77eb9587b36566e17c5decb806e747715f84187ba0855468c9e

See more details on using hashes here.

File details

Details for the file web_pyoctopus-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: web_pyoctopus-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 7.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for web_pyoctopus-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0bb33ae349368c15b7b25f88264bf95ee6d3ee47affbd38d2566085dab8cf73c
MD5 8afbcc6b300e6e6d2fe739189ab854b5
BLAKE2b-256 4fee09d00ddf8a8439dda91f50fba888448261604d24e7cba45dc80e11d4f34b

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