trivela


Nametrivela JSON
Version 0.0.2 PyPI version JSON
download
home_pageNone
SummaryTrivela Python Web Framework build for learning purpose.
upload_time2024-04-03 15:39:39
maintainerNone
docs_urlNone
authorSanjaya Rai
requires_python>=3.12.0
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            
# Trivela: Python Web Framework built for learning purposes

![purpose](https://img.shields.io/badge/any_text-you_like-blue)
![PyPI](https://img.shields.io/badge/just%20the%20message-8A2BE2)

Trivela is a Python web framework build for learning purposes.

It's a WSGI framework and can be used with any WSGI application server such as Gunicorn.

## Installation
```shell
pip install trivela

```


## How to use it

### Basic usage:

```python
from trivela.api import API

app = API()

@app.route("/home")
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("/book")
class BooksResource:
    def get(self, req, resp):
        resp.text = "Books Page"

    def post(self, req, resp):
        resp.text = "Endpoint to create a book"


@app.route("/template")
def template_handler(req, resp):
    resp.body = app.template(
        "index.html", context={"name": "Trivela", "title": "Worst Framework"}).encode()

```

### Unit Tests

The recommended way of writing unit tests is with [pytest](https://docs.pytest.org/en/latest/). There are two built in fixtures
that you may want to use when writing unit tests with Bumbo. The first one is `app` which is an instance of the main `API` class:

```python
def test_route_overlap_throws_exception(app):
    @app.route("/")
    def home(req, resp):
        resp.text = "Welcome Home."

    with pytest.raises(AssertionError):
        @app.route("/")
        def home2(req, resp):
            resp.text = "Welcome Home2."
```

The other one is `client` that you can use to send HTTP requests to your handlers. It is based on the famous [requests](https://requests.readthedocs.io/) and it should feel very familiar:

```python
def test_parameterized_route(app, client):
    @app.route("/{name}")
    def hello(req, resp, name):
        resp.text = f"hey {name}"

    assert client.get("http://testserver/matthew").text == "hey matthew"
```

## Templates

The default folder for templates is `templates`. You can change it when initializing the main `API()` class:

```python
app = API(templates_dir="templates_dir_name")
```

Then you can use HTML files in that folder like so in a handler:

```python
@app.route("/show/template")
def handler_with_template(req, resp):
    resp.html = app.template(
        "example.html", context={"title": "Bad Framework", "body": "welcome to the future!"})
```

## Static Files

Just like templates, the default folder for static files is `static` and you can override it:

```python
app = API(static_dir="static_dir_name")
```

Then you can use the files inside this folder in HTML files:

```html
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>{{title}}</title>

  <link href="/static/main.css" rel="stylesheet" type="text/css">
</head>

<body>
    <h1>{{body}}</h1>
    <p>This is a paragraph</p>
</body>
</html>
```

### Middleware

You can create custom middleware classes by inheriting from the `bumbo.middleware.Middleware` class and overriding its two methods
that are called before and after each request:

```python
from bumbo.api import API
from bumbo.middleware import Middleware


app = API()


class SimpleCustomMiddleware(Middleware):
    def process_request(self, req):
        print("Before dispatch", req.url)

    def process_response(self, req, res):
        print("After dispatch", req.url)


app.add_middleware(SimpleCustomMiddleware)
```

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "trivela",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.12.0",
    "maintainer_email": null,
    "keywords": null,
    "author": "Sanjaya Rai",
    "author_email": "rainamite@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/f6/ef/6d8489818a1481b876e8176a8404cd8fa3288274832378f8739471c3e8c5/trivela-0.0.2.tar.gz",
    "platform": null,
    "description": "\n# Trivela: Python Web Framework built for learning purposes\n\n![purpose](https://img.shields.io/badge/any_text-you_like-blue)\n![PyPI](https://img.shields.io/badge/just%20the%20message-8A2BE2)\n\nTrivela is a Python web framework build for learning purposes.\n\nIt's a WSGI framework and can be used with any WSGI application server such as Gunicorn.\n\n## Installation\n```shell\npip install trivela\n\n```\n\n\n## How to use it\n\n### Basic usage:\n\n```python\nfrom trivela.api import API\n\napp = API()\n\n@app.route(\"/home\")\ndef home(request, response):\n    response.text = \"Hello from the HOME page\"\n\n\n@app.route(\"/hello/{name}\")\ndef greeting(request, response, name):\n    response.text = f\"Hello, {name}\"\n\n\n@app.route(\"/book\")\nclass BooksResource:\n    def get(self, req, resp):\n        resp.text = \"Books Page\"\n\n    def post(self, req, resp):\n        resp.text = \"Endpoint to create a book\"\n\n\n@app.route(\"/template\")\ndef template_handler(req, resp):\n    resp.body = app.template(\n        \"index.html\", context={\"name\": \"Trivela\", \"title\": \"Worst Framework\"}).encode()\n\n```\n\n### Unit Tests\n\nThe recommended way of writing unit tests is with [pytest](https://docs.pytest.org/en/latest/). There are two built in fixtures\nthat you may want to use when writing unit tests with Bumbo. The first one is `app` which is an instance of the main `API` class:\n\n```python\ndef test_route_overlap_throws_exception(app):\n    @app.route(\"/\")\n    def home(req, resp):\n        resp.text = \"Welcome Home.\"\n\n    with pytest.raises(AssertionError):\n        @app.route(\"/\")\n        def home2(req, resp):\n            resp.text = \"Welcome Home2.\"\n```\n\nThe other one is `client` that you can use to send HTTP requests to your handlers. It is based on the famous [requests](https://requests.readthedocs.io/) and it should feel very familiar:\n\n```python\ndef test_parameterized_route(app, client):\n    @app.route(\"/{name}\")\n    def hello(req, resp, name):\n        resp.text = f\"hey {name}\"\n\n    assert client.get(\"http://testserver/matthew\").text == \"hey matthew\"\n```\n\n## Templates\n\nThe default folder for templates is `templates`. You can change it when initializing the main `API()` class:\n\n```python\napp = API(templates_dir=\"templates_dir_name\")\n```\n\nThen you can use HTML files in that folder like so in a handler:\n\n```python\n@app.route(\"/show/template\")\ndef handler_with_template(req, resp):\n    resp.html = app.template(\n        \"example.html\", context={\"title\": \"Bad Framework\", \"body\": \"welcome to the future!\"})\n```\n\n## Static Files\n\nJust like templates, the default folder for static files is `static` and you can override it:\n\n```python\napp = API(static_dir=\"static_dir_name\")\n```\n\nThen you can use the files inside this folder in HTML files:\n\n```html\n<!DOCTYPE html>\n<html lang=\"en\">\n\n<head>\n  <meta charset=\"UTF-8\">\n  <title>{{title}}</title>\n\n  <link href=\"/static/main.css\" rel=\"stylesheet\" type=\"text/css\">\n</head>\n\n<body>\n    <h1>{{body}}</h1>\n    <p>This is a paragraph</p>\n</body>\n</html>\n```\n\n### Middleware\n\nYou can create custom middleware classes by inheriting from the `bumbo.middleware.Middleware` class and overriding its two methods\nthat are called before and after each request:\n\n```python\nfrom bumbo.api import API\nfrom bumbo.middleware import Middleware\n\n\napp = API()\n\n\nclass SimpleCustomMiddleware(Middleware):\n    def process_request(self, req):\n        print(\"Before dispatch\", req.url)\n\n    def process_response(self, req, res):\n        print(\"After dispatch\", req.url)\n\n\napp.add_middleware(SimpleCustomMiddleware)\n```\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Trivela Python Web Framework build for learning purpose.",
    "version": "0.0.2",
    "project_urls": null,
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "673e36ab6fd0daebeb7579d44e68b9ddbc7e09217a2146028d3fadacf860216f",
                "md5": "33e4c878b15532ebb7884233c5710ecf",
                "sha256": "af4cd8f1e2196bc42f9fad6f7282e80b438cec7491fdab3d0d48d4fb6a5717ff"
            },
            "downloads": -1,
            "filename": "trivela-0.0.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "33e4c878b15532ebb7884233c5710ecf",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.12.0",
            "size": 5477,
            "upload_time": "2024-04-03T15:39:37",
            "upload_time_iso_8601": "2024-04-03T15:39:37.046248Z",
            "url": "https://files.pythonhosted.org/packages/67/3e/36ab6fd0daebeb7579d44e68b9ddbc7e09217a2146028d3fadacf860216f/trivela-0.0.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "f6ef6d8489818a1481b876e8176a8404cd8fa3288274832378f8739471c3e8c5",
                "md5": "a17b81b68c6e65945cc9e939cbef614d",
                "sha256": "bbdba0a4f383416da6bf1a213157ed79a502098649d99dd462ad958ac6559791"
            },
            "downloads": -1,
            "filename": "trivela-0.0.2.tar.gz",
            "has_sig": false,
            "md5_digest": "a17b81b68c6e65945cc9e939cbef614d",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.12.0",
            "size": 5450,
            "upload_time": "2024-04-03T15:39:39",
            "upload_time_iso_8601": "2024-04-03T15:39:39.853385Z",
            "url": "https://files.pythonhosted.org/packages/f6/ef/6d8489818a1481b876e8176a8404cd8fa3288274832378f8739471c3e8c5/trivela-0.0.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-04-03 15:39:39",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "trivela"
}
        
Elapsed time: 0.18995s