Python web framework built for learning purposes.
Project description
Sarva
A lightweight, fast, and minimal Python web framework built from scratch — designed to give developers full control without unnecessary complexity.
Built for learning, performance, and flexibility.
Overview
Sarva is a custom Python web framework that handles HTTP requests, routing, and responses with a clean and minimal design.
It’s created to:
- Understand how frameworks like Django and Flask work internally
- Provide a simple alternative for lightweight projects
- Give developers full control over request/response handling
Features
- Custom Request & Response handling
- URL Routing system
- Lightweight and fast
- Minimal dependencies
- Easy to extend
- Uploaded to PyPi
Installation
pip install sarva
How to use it
Basic Usage
from sarva.api import Sarva
app = Sarva()
@app.route("/home", allowed_methods=["get"])
def home(request, response):
response.text = "Hello from the Home Page"
@app.route("/hello/{name}")
def greeting(request, response, name):
response.text = f"Hello {name}"
@app.route("/books")
class Books:
def get(self, request, response):
response.text = "Books page"
def post(self, request, response):
response.text = "Endpoint to create a book"
@app.route("/template")
def template_handler(request, response):
response.body = app.template(
"home.html",
context = {"new_title": "Best title", "new_body": "New best body"}
)
Unit tests
The recommended way of writing unit tests is with pytest . There are two built in fixtures that you may want to use when writing unit tests with Sarva. The first one is app which is an instance of main API class
def test_basic_route_adding(app):
@app.route("/home")
def home(request, response):
response.text = "Hello from Home!"
The other one is test_client that you can use to send HTTP requests to your handlers.
def test_parameterazied_routing(app, test_client):
@app.route("/hello/{name}")
def greeting(request, response, name):
response.text = f"Hello {name}"
assert test_client.get("http://testserver/hello/Sarvar").text == "Hello Sarvar"
assert test_client.get("http://testserver/hello/John").text == "Hello John"
Templates
The default folder for templates is templates. You can change that when initializing main API() class:
app = API(templates_dir="templates_dir_name")
Then you can use HTML files in that folder like so in the handler:
@app.route("/template")
def template_handler(request, response):
response.body = app.template(
"home.html",
context = {"new_title": "Best title", "new_body": "New best body"}
)
Static files
Just like templates the default folder for static files is 'static' and you can override it:
app = API(static="static_files_dir")
Then you can use file inside this folder in HTML files:
<html>
<head>
<title>{{new_title}}</title>
<link rel="stylesheet" href="/static/home.css">
</head>
<body>
{{new_body}}
</body>
</html>
Contributing
Contributions are welcome!
Feel free to:
- Open issues
- Submit pull requests
- Suggest new features
License
MIT License — feel free to use and modify.
Support
If you like this project, give it a star ⭐ It helps others discover it!
Final Note
This framework is built for learning and experimentation — but can grow into something powerful with your ideas.
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 sarva-0.1.2.tar.gz.
File metadata
- Download URL: sarva-0.1.2.tar.gz
- Upload date:
- Size: 6.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
40db39f509b3beedcdde9278d17b5767e25b7f30a5cd92b9dd7dcf7c84187182
|
|
| MD5 |
262aeb8feb77ab1ed3a9c01ccd02913a
|
|
| BLAKE2b-256 |
a17ee1cfe77293f756bce73a8ad514bfd3ed5bd589c78cce8eacd71270647682
|
File details
Details for the file sarva-0.1.2-py2.py3-none-any.whl.
File metadata
- Download URL: sarva-0.1.2-py2.py3-none-any.whl
- Upload date:
- Size: 5.2 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b4c9b4852193ba85aab9ad2b75f31336074abae18c717bfd632914c6e199372d
|
|
| MD5 |
2b19efe2895c726a00ad1d81e05f2f78
|
|
| BLAKE2b-256 |
61264f87c6398a9260b9dca172d0cef0f4554c0df3c3755ae6977691f221a970
|