Skip to main content

A simple and elegant Python web framework – forge your site in minutes.

Project description

🔥 WebForge

A simple, elegant Python web framework. Forge your site in minutes.

pip install WebForge
webforge new mysite
cd mysite && python backend/server/forge.py

Project Structure

mysite/
├── backend/
│   ├── requirements.txt
│   └── server/
│       └── forge.py          ← your app code
└── public/
    ├── pages/
    │   └── index.html        ← HTML templates
    ├── script/
    │   └── app.js            ← JavaScript
    └── service/
        └── style.css         ← CSS & static assets

Quick Start

# backend/server/forge.py
from WebForge import env as web

password = web.env("PASSWORD")   # read from environment (like os.getenv)
ip = "192.168.1.123"

@web.app("/")
def index():
    web.route("style", "/public/service/style.css")
    web.route("script", "/public/script/app.js")
    return web.render("index.html")

@web.app("/login")
def login():
    return web.render("login.html")

@web.security("/")
def protect_index():
    web.passwordlock(password)

if __name__ == "__main__":
    web.runapp(debug=True, port=5000, host="0.0.0.0")

API Reference

web.env(key, default=None)

Read an environment variable.

db_url = web.env("DATABASE_URL", "sqlite:///app.db")

@web.app(path, methods=None)

Register a route handler.

@web.app("/users", methods=["GET", "POST"])
def users():
    return web.json({"users": []})

web.route(name, filepath)

Inject a static asset (CSS/JS) into the rendered template.
Call this inside a route handler before web.render().

@web.app("/")
def index():
    web.route("style", "/public/service/style.css")
    return web.render("index.html")

web.render(template, context={})

Render an HTML file from public/pages/.
Supports {{ variable }} substitution.

return web.render("profile.html", {"username": "Alice"})

web.json(data, status=200)

Return a JSON response.

return web.json({"ok": True})

web.redirect(location, status=302)

Return a redirect response.

return web.redirect("/login")

@web.security(path)

Attach security rules to a route.

@web.security("/admin")
def protect_admin():
    web.passwordlock(web.env("ADMIN_PASSWORD"))

web.runapp(host, port, debug)

Start the development server.

web.runapp(debug=True, port=5000, host="0.0.0.0")

@web.before_request / @web.after_request

Request lifecycle hooks.

@web.before_request
def log_request(request):
    print(f"{request.method} {request.path}")

@web.after_request
def add_cors(request, response):
    response.set_header("Access-Control-Allow-Origin", "*")

@web.errorhandler(code)

Custom error pages.

@web.errorhandler(404)
def not_found(request):
    return web.render("404.html")

Security Utilities

# Hash a password
hashed = web.hash_password("secret", salt="mysalt")

# Verify a password
ok = web.verify_password("secret", hashed, salt="mysalt")

# Generate a random token
token = web.generate_token(32)

CLI

webforge new <project-name>   # scaffold a new project
webforge run                   # run backend/server/forge.py
webforge version               # print version

License

MIT © WebForge Contributors

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

webforge-2.0.0.tar.gz (10.8 kB view details)

Uploaded Source

Built Distribution

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

webforge-2.0.0-py3-none-any.whl (9.4 kB view details)

Uploaded Python 3

File details

Details for the file webforge-2.0.0.tar.gz.

File metadata

  • Download URL: webforge-2.0.0.tar.gz
  • Upload date:
  • Size: 10.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.12

File hashes

Hashes for webforge-2.0.0.tar.gz
Algorithm Hash digest
SHA256 545f0c5c2b54a1debe298eddd5c295f93284d406b9ad5ce9b45122cbd495e0ac
MD5 b1db55b67fb0cd8cba68c72360609bbf
BLAKE2b-256 fc0aeb491aa0cdfbba8ab90d8ccaf963eaa56fd3a75a4e8141f0ad729d69389f

See more details on using hashes here.

File details

Details for the file webforge-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: webforge-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 9.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.12

File hashes

Hashes for webforge-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0bb725a1b68df736aa7a59888fb9070b9b7966b5af24e3d8ae4390e2012512ba
MD5 d395502212d9e1c39f66fb956880b6ba
BLAKE2b-256 cdfa33519d13e359b76b3cf2c4e6023722206ff746befb9f83c29fbb01bcba8f

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