A lightweight and efficient web framework
Project description
Graceful Web Framework
Graceful is a lightweight, asynchronous web framework designed for Python, allowing you to build and manage web applications with ease. It handles HTTP requests and responses, supports various HTTP methods, and includes middleware support.
Features
- Asynchronous Processing: Utilizes
asynciofor handling requests and responses asynchronously. - Flexible Routing: Define routes for different HTTP methods (GET, POST, PUT, DELETE, etc.).
- Automatic Data Casting: Uses Python built-in types for automatic data casting in request handlers.
- Template Handling: Convert data to and from Python types with a simple template casting mechanism.
- Exception Handling: Customizable exception handling with status codes.
Installation
To use Graceful, you need Python 3.7 or higher. You can clone the repository and install the dependencies:
git clone https://github.com/nolanM123/graceful
cd graceful
pip install -r requirements.txt
Basic Usage
Creating a Simple Application
Here's a basic example of how to set up a simple web application using Graceful:
from graceful import Graceful
app = Graceful()
@app.get("/")
def index(request):
return "Hello, world!"
if __name__ == "__main__":
app.run()
Routing
Graceful supports various HTTP methods:
app.get("/url")- Handles GET requests.app.post("/url")- Handles POST requests.app.put("/url")- Handles PUT requests.app.delete("/url")- Handles DELETE requests.app.head("/url")- Handles HEAD requests.app.connect("/url")- Handles CONNECT requests.app.options("/url")- Handles OPTIONS requests.app.trace("/url")- Handles TRACE requests.app.patch("/url")- Handles PATCH requests.
Parameters and Data Casting
In Graceful, the parameters of a handler function determine how data from the request is accessed and passed to the handler. The framework uses Python's built-in types to automatically cast incoming data to the appropriate types based on the annotations of the handler function parameters.
Example
Suppose you have a route that expects certain parameters in the request:
from graceful import Graceful
from typing import Optional
app = Graceful()
@app.get("/greet/{name}")
def greet(name: str, age: Optional[int] = None):
if age:
return f"Hello, {name}. You are {age} years old."
return f"Hello, {name}."
if __name__ == "__main__":
app.run()
In this example:
nameis extracted from the URL path and is automatically cast to astr.ageis an optional query parameter and is cast to anintif provided.
The casting mechanism ensures that:
- Data is automatically converted to the specified types (
str,int, etc.). - If the data cannot be converted, an appropriate error or default value is used.
Note: While asynchronous processing is supported, it is optional for handler functions. You can define handlers as either synchronous or asynchronous functions. The framework will handle them accordingly. For example, the greet function above is synchronous but can be defined as asynchronous if needed:
@app.get("/greet/{name}")
async def greet(name: str, age: Optional[int] = None):
# Asynchronous code here
...
Middleware
You can define custom middleware to process requests and responses:
async def custom_middleware(request, fetch):
# Modify request or perform actions before handling
response = await fetch(request)
# Modify response or perform actions after handling
return response
app = graceful.Graceful(middleware=custom_middleware)
Exception Handling
Handle exceptions and customize responses for specific HTTP status codes:
@app.exception(404)
def not_found():
# Handle response for 404 Not Found errors.
...
@app.exception(500)
def internal_error():
# Handle response for 500 Internal Server Error errors.
...
Contributing
Contributions are welcome! Please follow these steps to contribute:
- Fork the repository.
- Create a new branch (
git checkout -b feature-branch). - Commit your changes (
git commit -am 'Add new feature'). - Push to the branch (
git push origin feature-branch). - Create a new Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file graceful-web-1.0.0.tar.gz.
File metadata
- Download URL: graceful-web-1.0.0.tar.gz
- Upload date:
- Size: 13.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.11.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ce1f12bdcb30855dbcb59c93a654fcfe8ee8e8a2ef4657e34d9f77b2e58f1550
|
|
| MD5 |
67cfccf26416321d0ace7e9657837df0
|
|
| BLAKE2b-256 |
9dbc756912760781575511f37e1b51103a9bc31bcd3946f4afa2e4a16d79e446
|
File details
Details for the file graceful_web-1.0.0-py3-none-any.whl.
File metadata
- Download URL: graceful_web-1.0.0-py3-none-any.whl
- Upload date:
- Size: 11.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.11.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dcf9be4f82bd15925f75be697f2690e15447f5491471f63a2a2d2328f7b3a283
|
|
| MD5 |
d938879a8c38e46cb5705764f7d7f536
|
|
| BLAKE2b-256 |
75e112be7d85dc7aa9a231caf7a8c273767b34fb5ee3385a01155a68d5d04d57
|