Skip to main content

Python Web framework built for learning purposes

Project description

PeakPy: Python web framework built for learning purposes

Purpose PyPI - Version

PeakPy is a Python Web framework built for learning purposes.

It's a WSGI framework and can be used with any WSGI application server such as GUNICORN.

Installation

pip install peakpy

How to use it

Dynamic Routes:

Handle dynamic URLs with parameters:

from peakpy import PeakPy

app = PeakPy()

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

Templates:

Render HTML templates with Jinja2:

from peakpy import PeakPy

app = PeakPy(templates_dir="templates")

@app.route("/hello/{name}")
def greet(req, resp, name):
    resp.html = app.template("index.html", context={"name": name})

Create templates/index.html::

<!DOCTYPE html>
<html>
<head>
    <title>Hello</title>
</head>
<body>
    <h1>Hello, {{ name }}!</h1>
</body>
</html>

Static Files:

Place static files (e.g., style.css) in the static directory. They are served at /static/:

/* static/style.css */
body {
    background-color: lightblue;
}

Link it in your template:

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

Access http://localhost:8000/static/style.css directly.

Templates and Static Files

PeakPy supports dynamic HTML rendering with Jinja2 templates and static file serving with WhiteNoise.

  • Templates: Store Jinja2 template files (e.g., .html) in a directory. By default, PeakPy looks for templates in the templates directory. To use a custom directory, specify template_dir when creating the PeakPy instance:
    app = PeakPy(template_dir="my_templates")
    
    If template_dir is not provided, it defaults to templates.
  • Static Files: Store static assets (e.g., .css, .js, images) in a directory. By default, PeakPy serves files from the static directory at /static/. To use a custom directory, specify static_dir:
    app = PeakPy(static_dir="my_static")
    
    If static_dir is not provided, it defaults to static.
  • Example with custom directories:
    app = PeakPy(template_dir="custom_templates", static_dir="custom_static")
    
    Ensure the specified directories exist in your project root.

Middleware:

Add middleware to process requests or responses:

from peakpy import PeakPy, Middleware

app = PeakPy()

class LoggingMiddleware(Middleware):
    def process_request(self, req):
        print(f"Received request: {req.path}")
    
    def process_response(self, req, resp):
        print("Response sent")

app.add_middleware(LoggingMiddleware)

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

JSON Responses:

Return JSON data:

from peakpy import PeakPy

app = PeakPy()

@app.route("/api")
def api(req, resp):
    resp.json = {"message": "Welcome to the API"}

Access http://localhost:8000/api to get a JSON response.

Custom Error Handling:

Handle exceptions gracefully:

from peakpy import PeakPy

app = PeakPy()

def on_exception(req, resp, exc):
    resp.status_code = 500
    resp.text = "Something went wrong!"

app.add_exception_handler(on_exception)

@app.route("/error")
def error(req, resp):
    raise ValueError("Test error")

Visit http://localhost:8000/error to see the custom error message.

Contributing

Contributions are welcome! Fork the repository, create a branch, and submit a pull request on GitHub.

Contact

For support or feedback, open an issue on the GitHub repository or email blogasadbek@gmail.com.

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

peakpy-0.1.2.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

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

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

Uploaded Python 2Python 3

File details

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

File metadata

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

File hashes

Hashes for peakpy-0.1.2.tar.gz
Algorithm Hash digest
SHA256 625fb834bdf9d11ef134775402195ce60978da83c36718bc5dbbaadb80ebd5a0
MD5 bfd9a1c43c05db8dd81d5ee57084659c
BLAKE2b-256 9f9fbe6323bd1421527cb6008f9e28f8020f715c123f1175929e141527c69b22

See more details on using hashes here.

File details

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

File metadata

  • Download URL: peakpy-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.12.3

File hashes

Hashes for peakpy-0.1.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 59cd31279f28e4d303d3e5e5dae9ef0bb5d9dfeeeb5bb2bce3edd65ec3852671
MD5 69e4c8a34fc9eb3bc8ce38d766cc40c9
BLAKE2b-256 942a67fdf426c1a3b72a972e2f47fc79d58bca2e562e435230f4b9af855107eb

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