{ "info": { "author": "Jahongir Rahmonov", "author_email": "jrahmonov2@gmail.com", "bugtrack_url": null, "classifiers": [ "License :: OSI Approved :: MIT License", "Programming Language :: Python", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.6", "Programming Language :: Python :: Implementation :: CPython", "Programming Language :: Python :: Implementation :: PyPy" ], "description": "\n

\n \n

\n\n---\n\n![purpose](https://img.shields.io/badge/purpose-learning-green.svg)\n[![travis](https://travis-ci.org/rahmonov/alcazar.svg?branch=master)](https://travis-ci.org/rahmonov/alcazar)\n\n# Alcazar\n\nAlcazar is a Python Web Framework built for learning purposes. The plan is to learn how frameworks are built by implementing their features,\nwriting blog posts about them and keeping the codebase as simple as possible.\n\nIt is a WSGI framework and can be used with any WSGI application server such as Gunicorn.\n\n## Inspiration\n\nI was inspired to make a web framework after reading [Florimond Monca](https://twitter.com/FlorimondManca)'s [blog post](https://blog.florimondmanca.com/how-i-built-a-web-framework-and-became-an-open-source-maintainer)\nabout how he built a web framework and became an open source maintainer. He wrote about how thrilling the experience has been for him so I decided I would give it a try as well.\nThank you, [Florimond](https://github.com/florimondmanca) and of course [Kenneth Reitz](https://twitter.com/kennethreitz) who in turn inspired Florimond to write a framework with\nhis own framework [Responder](https://github.com/kennethreitz/responder). Go check out both [Bocadillo by Florimond Monca](https://github.com/bocadilloproject/bocadillo) and [Responder by Kenneth Reitz](https://github.com/kennethreitz/responder).\nIf you like them, show some love by staring their repos.\n\n## Blog posts\n\n- [Part I: Intro, API, request handlers, routing (both simple and parameterized)](http://rahmonov.me/posts/write-python-framework-part-one/)\n- [Part II: class based handlers, route overlap check, unit tests](http://rahmonov.me/posts/write-python-framework-part-two/)\n- [Part III: templates support, test client, django way of adding routes](http://rahmonov.me/posts/write-python-framework-part-three/)\n- [Part IV: custom exception handler, support for static files, middleware](http://rahmonov.me/posts/write-python-framework-part-four/)\n\n## Quick Start\n\nInstall it:\n\n```bash\npip install alcazar-web-framework\n```\n\nBasic Usage:\n\n```python\n# app.py\nfrom alcazar import Alcazar\n\napp = Alcazar()\n\n\n@app.route(\"/\")\ndef home(req, resp):\n resp.text = \"Hello, this is a home page.\"\n\n\n@app.route(\"/about\")\ndef about_page(req, resp):\n resp.text = \"Hello, this is an about page.\"\n\n\n@app.route(\"/{age:d}\")\ndef tell_age(req, resp, age):\n resp.text = f\"Your age is {age}\"\n\n\n@app.route(\"/{name:l}\")\nclass GreetingHandler:\n def get(self, req, resp, name):\n resp.text = f\"Hello, {name}\"\n\n\n@app.route(\"/show/template\")\ndef handler_with_template(req, resp):\n resp.html = app.template(\"example.html\", context={\"title\": \"Awesome Framework\", \"body\": \"welcome to the future!\"})\n\n\n@app.route(\"/json\")\ndef json_handler(req, resp):\n resp.json = {\"this\": \"is JSON\"}\n\n\n@app.route(\"/custom\")\ndef custom_response(req, resp):\n resp.body = b'any other body'\n resp.content_type = \"text/plain\"\n```\n\nStart:\n\n```bash\ngunicorn app:app\n```\n\n## Handlers\n\nIf you use class based handlers, only the methods that you implement will be allowed:\n\n```python\n@app.route(\"/{name:l}\")\nclass GreetingHandler:\n def get(self, req, resp, name):\n resp.text = f\"Hello, {name}\"\n```\n\nThis handler will only allow `GET` requests. That is, `POST` and others will be rejected. The same thing can be done with\nfunction based handlers in the following way:\n\n```python\n@app.route(\"/\", methods=[\"get\"])\ndef home(req, resp):\n resp.text = \"Hello, this is a home page.\"\n```\n\nNote that if you specify `methods` for class based handlers, they will be ignored.\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 Alcazar. The first one is `app` which is an instance of the main `Alcazar` 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](http://docs.python-requests.org/en/master/) 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(url(\"/matthew\")).text == \"hey matthew\"\n```\n\nNote that there is a `url()` function used. It is used to generate the absolute url of the request given a relative url. Import it before usage:\n\n```python\nfrom alcazar.utils.tests import url\n```\n\n## Templates\n\nThe default folder for templates is `templates`. You can change it when initializing the main `Alcazar()` class:\n\n```python\napp = Alcazar(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(\"example.html\", context={\"title\": \"Awesome 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 = Alcazar(static_dir=\"static_dir_name\")\n```\n\nThen you can use the files inside this folder in HTML files:\n\n```html\n\n\n\n\n \n {{title}}\n\n \n\n\n\n

{{body}}

\n

This is a paragraph

\n\n\n```\n\n## Custom Exception Handler\n\nSometimes, depending on the exception raised, you may want to do a certain action. For such cases, you can register an exception handler:\n\n```python\ndef on_exception(req, resp, exception):\n if isinstance(exception, HTTPError):\n if exception.status == 404:\n resp.text = \"Unfortunately the thing you were looking for was not found\"\n else:\n resp.text = str(exception)\n else:\n # unexpected exceptions\n if app.debug:\n debug_exception_handler(req, resp, exception)\n else:\n print(\"These unexpected exceptions should be logged.\")\n\napp = Alcazar(debug=False)\napp.add_exception_handler(on_exception)\n```\n\nThis exception handler will catch 404 HTTPErrors and change the text to `\"Unfortunately the thing you were looking for was not found\"`. For other HTTPErrors, it will simply\nshow the exception message. If the raised exception is not an HTTPError and if `debug` is set to True, it will show the exception and its traceback. Otherwise, it will log it.\n\n## Middleware\n\nYou can create custom middleware classes by inheriting from the `alcazar.middleware.Middleware` class and override its two methods\nthat are called before and after each request:\n\n```python\nfrom alcazar import Alcazar\nfrom alcazar.middleware import Middleware\n\napp = Alcazar()\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\n## Features\n\n- WSGI compatible\n- Basic and parameterized routing\n- Class based handlers\n- Test Client\n- Support for templates\n- Support for static files\n- Custom exception handler\n- Middleware\n\n## Note\n\nIt is extremely raw and will hopefully keep improving. If you are interested in knowing how a particular feature is implemented in other\nframeworks, please open an issue and we will hopefully implement and explain it in a blog post.\n\n\n", "description_content_type": "text/markdown", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/rahmonov/alcazar", "keywords": "", "license": "MIT", "maintainer": "", "maintainer_email": "", "name": "alcazar-web-framework", "package_url": "https://pypi.org/project/alcazar-web-framework/", "platform": "", "project_url": "https://pypi.org/project/alcazar-web-framework/", "project_urls": { "Homepage": "https://github.com/rahmonov/alcazar" }, "release_url": "https://pypi.org/project/alcazar-web-framework/0.0.2/", "requires_dist": [ "Jinja2 (==2.10)", "requests-wsgi-adapter (==0.4.0)", "parse (==1.11.1)", "requests (==2.21.0)", "WebOb (==1.8.5)", "whitenoise (==4.1.2)" ], "requires_python": ">=3.6.0", "summary": "Python Web Framework built for learning purposes.", "version": "0.0.2" }, "last_serial": 5117251, "releases": { "0.0.2": [ { "comment_text": "", "digests": { "md5": "100b8d298982cf5d10d088cd44762dc9", "sha256": "bdab8928063d1421c6bd41077e9eb54f62a610a870e2211164c735afe05252e6" }, "downloads": -1, "filename": "alcazar_web_framework-0.0.2-py3-none-any.whl", "has_sig": false, "md5_digest": "100b8d298982cf5d10d088cd44762dc9", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": ">=3.6.0", "size": 9571, "upload_time": "2019-04-09T07:03:39", "url": "https://files.pythonhosted.org/packages/ce/ae/25f103ebad24d637ef0af8c9510c4711a3e918e675bc810d550437230a29/alcazar_web_framework-0.0.2-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "9699aeed803da998af468ff3bf29308f", "sha256": "327775c040fdc674f776ced0466d05a38a7f741dd47c2650fc25176e93c897e1" }, "downloads": -1, "filename": "alcazar-web-framework-0.0.2.tar.gz", "has_sig": false, "md5_digest": "9699aeed803da998af468ff3bf29308f", "packagetype": "sdist", "python_version": "source", "requires_python": ">=3.6.0", "size": 9556, "upload_time": "2019-04-09T07:03:41", "url": "https://files.pythonhosted.org/packages/8f/7a/ff3696446a1ba8144368e5186c9b323ba027be4c3a29d9ecd20a8efef629/alcazar-web-framework-0.0.2.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "100b8d298982cf5d10d088cd44762dc9", "sha256": "bdab8928063d1421c6bd41077e9eb54f62a610a870e2211164c735afe05252e6" }, "downloads": -1, "filename": "alcazar_web_framework-0.0.2-py3-none-any.whl", "has_sig": false, "md5_digest": "100b8d298982cf5d10d088cd44762dc9", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": ">=3.6.0", "size": 9571, "upload_time": "2019-04-09T07:03:39", "url": "https://files.pythonhosted.org/packages/ce/ae/25f103ebad24d637ef0af8c9510c4711a3e918e675bc810d550437230a29/alcazar_web_framework-0.0.2-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "9699aeed803da998af468ff3bf29308f", "sha256": "327775c040fdc674f776ced0466d05a38a7f741dd47c2650fc25176e93c897e1" }, "downloads": -1, "filename": "alcazar-web-framework-0.0.2.tar.gz", "has_sig": false, "md5_digest": "9699aeed803da998af468ff3bf29308f", "packagetype": "sdist", "python_version": "source", "requires_python": ">=3.6.0", "size": 9556, "upload_time": "2019-04-09T07:03:41", "url": "https://files.pythonhosted.org/packages/8f/7a/ff3696446a1ba8144368e5186c9b323ba027be4c3a29d9ecd20a8efef629/alcazar-web-framework-0.0.2.tar.gz" } ] }