Skip to main content

Python web framework built for learning purposes.

Project description

alt text

PyPI version Python Versions License

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sarva-0.1.1.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

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

sarva-0.1.1-py2.py3-none-any.whl (2.9 kB view details)

Uploaded Python 2Python 3

File details

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

File metadata

  • Download URL: sarva-0.1.1.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for sarva-0.1.1.tar.gz
Algorithm Hash digest
SHA256 1a1901b0660af56d73d276364ea019f345c999b7ca451f6d301f64083be3ca41
MD5 f4a3cefd6f1e93a10cc312c1a8c983fb
BLAKE2b-256 1fb19a2c037b63638e5c52c9c9c9031956dba2b3bcc3445824908b90e1290f04

See more details on using hashes here.

File details

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

File metadata

  • Download URL: sarva-0.1.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 2.9 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for sarva-0.1.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 2bb19f25d0f4c5e8cae2977b234e85bef9c3333deb576754ba906f56faa57611
MD5 4da56406e4fcd82ce1bd18c00c136a99
BLAKE2b-256 83b2ef9bcf6c14b64f14b4569d3943063debff2e8755ad3bc7026df036e5405d

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