pythora


Namepythora JSON
Version 0.1.3 PyPI version JSON
download
home_pagehttps://github.com/johnibek/pythora
SummaryPython Web Framework built for learning purposes
upload_time2024-08-17 16:22:30
maintainerNone
docs_urlNone
authorJonibek Hamroqulov
requires_python>=3.12.0
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            
# Pythora: Python Web Framework built for learning purposes

![purpose](https://img.shields.io/badge/purpose-learning-green)
![PyPI - Version](https://img.shields.io/pypi/v/pythora)

Pythora is a web framework built for learning purposes.
It is a WSGI Framework and can be used with any WSGI application server such as Gunicorn.

## Installation
```shell
pip install pythora
```

## How to use it

### Basic Usage:

```python
from pythora.app import Pythora

app = Pythora()

@app.route("/home", allowed_methods=['get'])
def home(request, response):
    response.text = "Hello from Home Page"

@app.route("/hello/{name}")
def hello(request, response, name):
    response.text = f"Hello from {name}"

@app.route("/template")
def template(req, resp):
    resp.body = app.template("test.html", context={"new_title": "Best Title", "new_body": "Best Body"})
```

## Unit Tests

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

```python
def test_basic_route_adding(app):
    @app.route("/home")
    def home(req, resp):
        resp.text = "Hello from home"
```

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

```python
def test_parameterized_routing(app, test_client):
    @app.route("/hello/{name}")
    def hello(req, resp, name):
        resp.text = f"Hello {name}"

    assert test_client.get("http://testserver/hello/John").text == "Hello John"
    assert test_client.get("http://testserver/hello/Matthew").text == "Hello Matthew"
```

## Templates

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

```python
from pythora.app import Pythora
app = Pythora(template_dir='templates_dir_name')
```

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

```python
from pythora.app import Pythora

app = Pythora()

@app.route("/test-template")
def template(req, resp):
    resp.html = app.template('test.html', context={"new_title": "Best Title", "new_body": "Best body "})
```

## Static files

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

```python
from pythora.app import Pythora
app = Pythora(static_dir="static_dir_name")
```

Then you can use static files inside this directory in HTML file.

```html
<!DOCTYPE html>
<html lang="en">
<head>
    <title>{{new_title}}</title>
    <link rel="stylesheet" href="static/home.css">
</head>
<body>
    <p>{{new_body}}</p>
</body>
</html>
```


## Middleware

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

```python
from pythora.middleware import Middleware
from pythora.app import Pythora
app = Pythora()

class LoggingMiddleware(Middleware):
    def process_request(self, req):
        print("Request is being processed", req.url)

    def process_response(self, req, resp):
        print("Response has been generated", req.url)

app.add_middleware(LoggingMiddleware)
```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/johnibek/pythora",
    "name": "pythora",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.12.0",
    "maintainer_email": null,
    "keywords": null,
    "author": "Jonibek Hamroqulov",
    "author_email": "jonibekhamroqulov2004@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/bf/4f/b37b48539f2714c3f05a222badb2b0086950acded29bef2b539c56688b02/pythora-0.1.3.tar.gz",
    "platform": null,
    "description": "\n# Pythora: Python Web Framework built for learning purposes\n\n![purpose](https://img.shields.io/badge/purpose-learning-green)\n![PyPI - Version](https://img.shields.io/pypi/v/pythora)\n\nPythora is a web framework built for learning purposes.\nIt is a WSGI Framework and can be used with any WSGI application server such as Gunicorn.\n\n## Installation\n```shell\npip install pythora\n```\n\n## How to use it\n\n### Basic Usage:\n\n```python\nfrom pythora.app import Pythora\n\napp = Pythora()\n\n@app.route(\"/home\", allowed_methods=['get'])\ndef home(request, response):\n    response.text = \"Hello from Home Page\"\n\n@app.route(\"/hello/{name}\")\ndef hello(request, response, name):\n    response.text = f\"Hello from {name}\"\n\n@app.route(\"/template\")\ndef template(req, resp):\n    resp.body = app.template(\"test.html\", context={\"new_title\": \"Best Title\", \"new_body\": \"Best Body\"})\n```\n\n## Unit Tests\n\nThe recommended way of writing unit tests is with [pytest](https://docs.pytest.org/en/stable/)\nThere are two built-in fixtures that you may want to use when writing unit tests with `Pythora`.\nThe first one is `app` which is an instance of the main `Pythora` class.\n\n```python\ndef test_basic_route_adding(app):\n    @app.route(\"/home\")\n    def home(req, resp):\n        resp.text = \"Hello from home\"\n```\n\nThe other one is `test_client` that you can use to send HTTP requests to your handlers. \nIt is based on the famous [requests](https://requests.readthedocs.io/en/latest/) and it should feel very familiar:\n\n```python\ndef test_parameterized_routing(app, test_client):\n    @app.route(\"/hello/{name}\")\n    def hello(req, resp, name):\n        resp.text = f\"Hello {name}\"\n\n    assert test_client.get(\"http://testserver/hello/John\").text == \"Hello John\"\n    assert test_client.get(\"http://testserver/hello/Matthew\").text == \"Hello Matthew\"\n```\n\n## Templates\n\nThe default folder for templates is `templates`. You can change it when initializing main `Pythora()` class.\n\n```python\nfrom pythora.app import Pythora\napp = Pythora(template_dir='templates_dir_name')\n```\n\nThen you can use HTML files in that folder like so in a handler:\n\n```python\nfrom pythora.app import Pythora\n\napp = Pythora()\n\n@app.route(\"/test-template\")\ndef template(req, resp):\n    resp.html = app.template('test.html', context={\"new_title\": \"Best Title\", \"new_body\": \"Best body \"})\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\nfrom pythora.app import Pythora\napp = Pythora(static_dir=\"static_dir_name\")\n```\n\nThen you can use static files inside this directory in HTML file.\n\n```html\n<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n    <title>{{new_title}}</title>\n    <link rel=\"stylesheet\" href=\"static/home.css\">\n</head>\n<body>\n    <p>{{new_body}}</p>\n</body>\n</html>\n```\n\n\n## Middleware\n\nYou can create custom middleware classes by inheriting from `pythora.middleware.Middleware` class\nand overriding its two methods that are called before and after each request.\n\n```python\nfrom pythora.middleware import Middleware\nfrom pythora.app import Pythora\napp = Pythora()\n\nclass LoggingMiddleware(Middleware):\n    def process_request(self, req):\n        print(\"Request is being processed\", req.url)\n\n    def process_response(self, req, resp):\n        print(\"Response has been generated\", req.url)\n\napp.add_middleware(LoggingMiddleware)\n```\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Python Web Framework built for learning purposes",
    "version": "0.1.3",
    "project_urls": {
        "Homepage": "https://github.com/johnibek/pythora"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "8ee85cb4c8f60af275e06f093c45edbfb4eda3838eb436f0f594309f77d1c50e",
                "md5": "a4536635b8638fdd27df7e3c26e7e2fc",
                "sha256": "2c4c9ee5087d90b870b51b87131373900b59115adf6f7df933a5a9c06918802e"
            },
            "downloads": -1,
            "filename": "pythora-0.1.3-py2.py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "a4536635b8638fdd27df7e3c26e7e2fc",
            "packagetype": "bdist_wheel",
            "python_version": "py2.py3",
            "requires_python": ">=3.12.0",
            "size": 4853,
            "upload_time": "2024-08-17T16:22:28",
            "upload_time_iso_8601": "2024-08-17T16:22:28.907618Z",
            "url": "https://files.pythonhosted.org/packages/8e/e8/5cb4c8f60af275e06f093c45edbfb4eda3838eb436f0f594309f77d1c50e/pythora-0.1.3-py2.py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "bf4fb37b48539f2714c3f05a222badb2b0086950acded29bef2b539c56688b02",
                "md5": "3af01d1b029f2f6dbcce6da94e57ce40",
                "sha256": "e526f6fec85a55dcca9b310ea846bda9d97007b2b1fde7810ee471bc616bdb3e"
            },
            "downloads": -1,
            "filename": "pythora-0.1.3.tar.gz",
            "has_sig": false,
            "md5_digest": "3af01d1b029f2f6dbcce6da94e57ce40",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.12.0",
            "size": 5507,
            "upload_time": "2024-08-17T16:22:30",
            "upload_time_iso_8601": "2024-08-17T16:22:30.545385Z",
            "url": "https://files.pythonhosted.org/packages/bf/4f/b37b48539f2714c3f05a222badb2b0086950acded29bef2b539c56688b02/pythora-0.1.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-08-17 16:22:30",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "johnibek",
    "github_project": "pythora",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "pythora"
}
        
Elapsed time: 0.58688s