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

Step 1: Set Up a Virtual Environment

  1. Install virtualenv (if not already installed):

    pip install virtualenv
    
  2. Create a Virtual Environment:

    python -m venv venv
    
  3. Activate the Virtual Environment:

    • On Windows:
      venv\Scripts\activate
      
    • On macOS/Linux:
      source venv/bin/activate
      

Step 2: Install Web-PyOctopus

Install Web-PyOctopus using pip:

pip install web-pyoctopus

Step 3: Install Gunicorn

Install Gunicorn to run the application:

pip install gunicorn

📌 Basic Usage

Step 1: Create app.py

Create a file named app.py in your project directory and add the following code:

from web_pyoctopus.api import OctopusAPI

# Create an instance of OctopusAPI
app = OctopusAPI()

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

# Define a route with a dynamic parameter
@app.route("/hello/{name}")
def greeting(request, response, name):
    response.text = f"Hello, {name}!"

# Define a class-based view
@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"

# Define a route for template rendering
@app.route("/template")
def template_handler(req, resp):
    resp.body = app.template(
        "index.html", context={"name": "Web-PyOctopus", "title": "Simple & Awesome Framework"}
    ).encode()

Step 2: Create a Template (Optional)

If you want to use templates, create a directory named templates in the same folder as app.py. Inside the templates directory, create a file named index.html:

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

Step 3: Run the Application

Run the application using Gunicorn:

gunicorn app:app

Visit the following URLs in your browser:

  • http://127.0.0.1:8000/home
  • http://127.0.0.1:8000/hello/Octopus
  • http://127.0.0.1:8000/book
  • http://127.0.0.1:8000/template

📚 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": "Simple & Awesome Framework"}
    ).encode()

Change the default template directory:

app = OctopusAPI(templates_dir="custom_templates")

📂 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 web_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/Octopus")
    assert response.text == "Hello, Octopus!"

📜 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.8.tar.gz (8.3 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.8-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: web-pyoctopus-1.0.8.tar.gz
  • Upload date:
  • Size: 8.3 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.8.tar.gz
Algorithm Hash digest
SHA256 37abf1f2e9ad795a5f0e72d94379495c1a6672b93a90012159c10707b31da4d3
MD5 9eea65c9840394b06441fc9489a0c016
BLAKE2b-256 c8baad07826656a11b684af20bd95f6eba82d308f50d403a900a2b14726383a4

See more details on using hashes here.

File details

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

File metadata

  • Download URL: web_pyoctopus-1.0.8-py3-none-any.whl
  • Upload date:
  • Size: 9.8 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.8-py3-none-any.whl
Algorithm Hash digest
SHA256 8a66d1e7e1ae21bbf8c1350ba0580bf6306442adbd077a91aae4cb07da52a505
MD5 434051f2ac01a0a50e87e146717b7e83
BLAKE2b-256 8c39eb0db2a835840248728b5436c1744aeb6e53115f58b0dd49d8059ccdcf7d

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