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.1.tar.gz (4.6 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.1-py2.py3-none-any.whl (2.7 kB view details)

Uploaded Python 2Python 3

File details

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

File metadata

  • Download URL: peakpy-0.1.1.tar.gz
  • Upload date:
  • Size: 4.6 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.1.tar.gz
Algorithm Hash digest
SHA256 3bd35c0a9695a3d25c85a17a874eb68a048f86ec9f4c55664627b303ce5cd4c3
MD5 c1ac0d2d7db326801300a9ea77251def
BLAKE2b-256 f04a371637f34909a2d8c8e6729737519a7650edccb0aafb88250bf8b1ae8462

See more details on using hashes here.

File details

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

File metadata

  • Download URL: peakpy-0.1.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 2.7 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.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 65aa2e20447810cdb3fc97efd134530ebeb2faef005dd2693fed056c2d0074ed
MD5 b958f356947f2907a103bb0d5c8140c1
BLAKE2b-256 18790df4aed37d106ea3fe1ad7c5cd28dc1c88ab40cef4f309d4cdadc4f37b37

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