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.
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
- Custom Request & Response handling
- URL Routing system
- Lightweight and fast
- Minimal dependencies
- Easy to extend
- Uploaded to PyPi
pip install sarvafrom 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"}
)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"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"}
)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>Contributions are welcome!
Feel free to:
- Open issues
- Submit pull requests
- Suggest new features
MIT License — feel free to use and modify.
If you like this project, give it a star ⭐ It helps others discover it!
This framework is built for learning and experimentation — but can grow into something powerful with your ideas.