# PyBraineUZ: Python Web Framework built for learning purposes
![purpose](https://img.shields.io/badge/purpose-learning-green)
![PyPI - Version](https://img.shields.io/pypi/v/pybraineuz)
PyBraineUz is a Python web framework built for learning purpose.
It's a WSGI framework and can be used with any WSGI application server such as Gunicorn.
## Installation
```shel
pip install pybraineuz
```
## How to use it
### Basic usage:
```python
from pybarineuz.app import PyBraineUz
app = PyBraineUz()
@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(req, resp):
resp.html = app.template(
'home.html',
context={
"new_title": "New title",
"new_body": "New body 123",
}
)
@app.route('/json')
def json_handler(req, resp):
response_data = {'name': 'some name', 'type': 'json'}
resp.json = response_data
```
### Unit Tests
The recommended way of writing unit tests is with [pytest](http://docs.pytest.org/en/latest/). There are two built in fixture that you may want to use when writing unit tests with PyBraineUz. The first one is `app` which is an instance of the main `API` class:
```python
def test_route_overlap_throws_exception(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](http://requests.readthedics.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_dor_name")
```
When 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": "Awesome 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>
</head>
<body>
<h1>{{body}}</h1>
<p>This is a paragraph</p>
</body>
</html>
```
### Middleware
You can create custom middleware classes by inheriting from the `pybraineuz.middleware. Middleware` class and overriding its two methods that are called before and after each requests:
```python
from pybraineuz.api import API
from pybraineuz.middleware import Middleware
app = API()
class SimpleCustomMiddleware(Middleware):
def proccess_request(self, req):
print("Before dispatch", req.url)
def proccess_response(self, req, resp):
print("After dispatch", req.url)
app.add_middleware(SimpleCustomMiddleware)
```
Raw data
{
"_id": null,
"home_page": "https://github.com/me/myproject",
"name": "pybraineuz",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.10.12",
"maintainer_email": null,
"keywords": null,
"author": "Fazliddin Gadoyev",
"author_email": "fazliddinn.gadoyev@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/57/2b/14c18148c7f8439ad7bfd8348f52db2094387c2e022d35fc0a946005258a/pybraineuz-0.1.2.tar.gz",
"platform": null,
"description": "\n# PyBraineUZ: Python Web Framework built for learning purposes\n\n![purpose](https://img.shields.io/badge/purpose-learning-green)\n\n![PyPI - Version](https://img.shields.io/pypi/v/pybraineuz)\n\nPyBraineUz is a Python web framework built for learning purpose.\n\nIt's a WSGI framework and can be used with any WSGI application server such as Gunicorn.\n\n## Installation\n\n```shel\npip install pybraineuz\n```\n\n## How to use it\n\n### Basic usage:\n\n```python\nfrom pybarineuz.app import PyBraineUz\n\napp = PyBraineUz()\n\n\n@app.route('/home', allowed_methods=['get'])\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('/books')\nclass Books:\n def get(self, request, response):\n response.text = \"Books page\"\n\n def post(self, request, response):\n response.text = \"Endpoint to create a book\"\n\n\n@app.route('/template')\ndef template_handler(req, resp):\n resp.html = app.template(\n 'home.html',\n context={\n \"new_title\": \"New title\",\n \"new_body\": \"New body 123\",\n }\n )\n\n\n@app.route('/json')\ndef json_handler(req, resp):\n response_data = {'name': 'some name', 'type': 'json'}\n resp.json = response_data\n```\n\n\n### Unit Tests\n\nThe recommended way of writing unit tests is with [pytest](http://docs.pytest.org/en/latest/). There are two built in fixture that you may want to use when writing unit tests with PyBraineUz. The first one is `app` which is an instance of the main `API` class:\n\n\n```python\ndef test_route_overlap_throws_exception(app):\n @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\n\nThe other one is `client` that you can use to send HTTP requests to your handlers. It is based on the famous [requests](http://requests.readthedics.io/) and it should feel very familiar:\n\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\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_dor_name\")\n```\n\nWhen 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={\n \"title\": \"Awesome Framework\",\n \"body\": \"welcome to the future!\"\n }\n )\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</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 `pybraineuz.middleware. Middleware` class and overriding its two methods that are called before and after each requests:\n\n```python\nfrom pybraineuz.api import API\nfrom pybraineuz.middleware import Middleware\n\n\napp = API()\n\nclass SimpleCustomMiddleware(Middleware):\n\n def proccess_request(self, req):\n print(\"Before dispatch\", req.url)\n\n\n def proccess_response(self, req, resp):\n print(\"After dispatch\", req.url)\n\n\napp.add_middleware(SimpleCustomMiddleware)\n```\n\n\n\n\n\n\n\n\n\n\n\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Python Web Framework built for learning purposes.",
"version": "0.1.2",
"project_urls": {
"Homepage": "https://github.com/me/myproject"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "ce6d4a9a45c21a578327ba14b8e3ecaf8bebdd52f73687f8937a3e168ef1eb0c",
"md5": "daf3bde6fa2e7166030b8cd4f9c64049",
"sha256": "babe34dfd91090f2eb7f624fb9262c8e9ca82f7c798061878faefe1dfded5b31"
},
"downloads": -1,
"filename": "pybraineuz-0.1.2-py2.py3-none-any.whl",
"has_sig": false,
"md5_digest": "daf3bde6fa2e7166030b8cd4f9c64049",
"packagetype": "bdist_wheel",
"python_version": "py2.py3",
"requires_python": ">=3.10.12",
"size": 5159,
"upload_time": "2024-07-27T08:09:43",
"upload_time_iso_8601": "2024-07-27T08:09:43.585539Z",
"url": "https://files.pythonhosted.org/packages/ce/6d/4a9a45c21a578327ba14b8e3ecaf8bebdd52f73687f8937a3e168ef1eb0c/pybraineuz-0.1.2-py2.py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "572b14c18148c7f8439ad7bfd8348f52db2094387c2e022d35fc0a946005258a",
"md5": "6dbc4d872ee9527a73ad82a6bf0a8b45",
"sha256": "8350ab1cace17aa8c08db35ab88d2be5964a48858fcad725cb7ada721d223f10"
},
"downloads": -1,
"filename": "pybraineuz-0.1.2.tar.gz",
"has_sig": false,
"md5_digest": "6dbc4d872ee9527a73ad82a6bf0a8b45",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.10.12",
"size": 5893,
"upload_time": "2024-07-27T08:09:45",
"upload_time_iso_8601": "2024-07-27T08:09:45.635127Z",
"url": "https://files.pythonhosted.org/packages/57/2b/14c18148c7f8439ad7bfd8348f52db2094387c2e022d35fc0a946005258a/pybraineuz-0.1.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-07-27 08:09:45",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "me",
"github_project": "myproject",
"github_not_found": true,
"lcname": "pybraineuz"
}