Flask-Classful
==============
Note::
This is a fork of original `Flask-Classy` for continuing its development since the original
project was not updated for a long time. For more information, see:
https://github.com/apiguy/flask-classy/issues/80
Flask-Classful is an extension that adds class-based views to Flask.
But why?
I ❤ Flask. Like a lot. But sometimes projects get a little big
and I need some way of managing and organizing all the different
pieces. I know what you're saying: "But what about Blueprints?"
You're right. Blueprints are pretty awesome. But I found that they
aren't always enough to encapsulate a specific context the way I
need. What I wanted, no what I *needed* was to be able to group
my views into relevant classes each with their own context and
behavior. It's also made testing really nifty too.
"OK, I see your point. But can't I just use the base classes in
``flask.views`` to do that?"
Well, yes and no. While ``flask.views.MethodView`` does
provide some of the functionality of ``flask_classful.FlaskView``
it doesn't quite complete the picture by supporting methods that
aren't part of the typical CRUD operations for a given resource, or
make it easy for me to override the route rules for particular view.
And while ``flask.views.View`` does add some context, it requires
a class for each view instead of letting me group very similar
views for the same resource into a single class.
"But my projects aren't that big. Can Flask-Classful do
anything else for me besides making a big project easier to manage?"
Why yes. It does help a bit with some other things.
For example, `Flask-Classful` will automatically generate routes based on the methods
in your views, and makes it super simple to override those routes
using Flask's familiar decorator syntax.
.. _Flask-Classful: https://github.com/pallets-eco/flask-classful
.. _Flask: https://flask.palletsprojects.com/
Installation
------------
Install the latest extension with::
$ pip install flask-classful
Let's see how it works
----------------------
If you're like me, you probably get a better idea of how to use something
when you see it being used. Let's go ahead and create a little app to
see how Flask-Classful works:
.. code-block:: python
from flask import Flask
from flask_classful import FlaskView
# we'll make a list to hold some quotes for our app
quotes = [
"A noble spirit embiggens the smallest man! ~ Jebediah Springfield",
"If there is a way to do it better... find it. ~ Thomas Edison",
"No one knows what he can do till he tries. ~ Publilius Syrus"
]
app = Flask(__name__)
class QuotesView(FlaskView):
def index(self):
return "<br>".join(quotes)
QuotesView.register(app)
if __name__ == '__main__':
app.run()
Run this app and open your web browser to: http://localhost:5000/quotes/
As you can see, it returns the list of quotes. But what if we just wanted
one quote? What would we do then?
.. code-block:: python
class QuotesView(FlaskView):
def index(self):
...
def get(self, id):
id = int(id)
if id < len(quotes):
return quotes[id]
else:
return "Not Found", 404
Now direct your browser to: http://localhost:5000/quotes/1/ and you should
see the very poignant quote from the esteemed Mr. Edison.
That's cool and all, but what if we just wanted a random quote? What then?
Let's add a random view to our FlaskView:
.. code-block:: python
from random import choice
.. code-block:: python
class QuotesView(FlaskView):
def index(self):
...
def get(self, id):
...
def random(self):
return choice(quotes)
And point your browser to: http://localhost:5000/quotes/random/ and see
that a random quote is returned each time. Voilà!
So by now you must be keenly aware of the fact that you have not defined a
single route, but yet routing is obviously taking place. "Is this voodoo?"
you ask?
Not at all. Flask-Classful will automatically create routes for any method
in a FlaskView that doesn't begin with an underscore character.
You can still define your own routes of course, and we'll look at that next.
Raw data
{
"_id": null,
"home_page": "",
"name": "Flask-Classful",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.7",
"maintainer_email": "Pallets Community Ecosystem <contact@palletsprojects.com>",
"keywords": "",
"author": "",
"author_email": "",
"download_url": "https://files.pythonhosted.org/packages/54/9f/0a2e2144e3e32d1d8e5bd66acb3cb54f76b7072ab832a6be9b7d707d2015/flask_classful-0.16.0.tar.gz",
"platform": null,
"description": "Flask-Classful\n==============\n\nNote::\n\n This is a fork of original `Flask-Classy` for continuing its development since the original\n project was not updated for a long time. For more information, see:\n https://github.com/apiguy/flask-classy/issues/80\n\n\nFlask-Classful is an extension that adds class-based views to Flask.\nBut why?\n\nI \u2764 Flask. Like a lot. But sometimes projects get a little big\nand I need some way of managing and organizing all the different\npieces. I know what you're saying: \"But what about Blueprints?\"\n\nYou're right. Blueprints are pretty awesome. But I found that they\naren't always enough to encapsulate a specific context the way I\nneed. What I wanted, no what I *needed* was to be able to group\nmy views into relevant classes each with their own context and\nbehavior. It's also made testing really nifty too.\n\n\"OK, I see your point. But can't I just use the base classes in\n``flask.views`` to do that?\"\n\nWell, yes and no. While ``flask.views.MethodView`` does\nprovide some of the functionality of ``flask_classful.FlaskView``\nit doesn't quite complete the picture by supporting methods that\naren't part of the typical CRUD operations for a given resource, or\nmake it easy for me to override the route rules for particular view.\nAnd while ``flask.views.View`` does add some context, it requires\na class for each view instead of letting me group very similar\nviews for the same resource into a single class.\n\n\"But my projects aren't that big. Can Flask-Classful do\nanything else for me besides making a big project easier to manage?\"\n\nWhy yes. It does help a bit with some other things.\n\nFor example, `Flask-Classful` will automatically generate routes based on the methods\nin your views, and makes it super simple to override those routes\nusing Flask's familiar decorator syntax.\n\n.. _Flask-Classful: https://github.com/pallets-eco/flask-classful\n.. _Flask: https://flask.palletsprojects.com/\n\nInstallation\n------------\n\nInstall the latest extension with::\n\n $ pip install flask-classful\n\n\nLet's see how it works\n----------------------\n\nIf you're like me, you probably get a better idea of how to use something\nwhen you see it being used. Let's go ahead and create a little app to\nsee how Flask-Classful works:\n\n.. code-block:: python\n\n from flask import Flask\n from flask_classful import FlaskView\n\n # we'll make a list to hold some quotes for our app\n quotes = [\n \"A noble spirit embiggens the smallest man! ~ Jebediah Springfield\",\n \"If there is a way to do it better... find it. ~ Thomas Edison\",\n \"No one knows what he can do till he tries. ~ Publilius Syrus\"\n ]\n\n app = Flask(__name__)\n\n class QuotesView(FlaskView):\n def index(self):\n return \"<br>\".join(quotes)\n\n QuotesView.register(app)\n\n if __name__ == '__main__':\n app.run()\n\nRun this app and open your web browser to: http://localhost:5000/quotes/\n\nAs you can see, it returns the list of quotes. But what if we just wanted\none quote? What would we do then?\n\n.. code-block:: python\n\n class QuotesView(FlaskView):\n def index(self):\n ...\n\n def get(self, id):\n id = int(id)\n if id < len(quotes):\n return quotes[id]\n else:\n return \"Not Found\", 404\n\nNow direct your browser to: http://localhost:5000/quotes/1/ and you should\nsee the very poignant quote from the esteemed Mr. Edison.\n\nThat's cool and all, but what if we just wanted a random quote? What then?\nLet's add a random view to our FlaskView:\n\n.. code-block:: python\n\n from random import choice\n\n.. code-block:: python\n\n class QuotesView(FlaskView):\n def index(self):\n ...\n\n def get(self, id):\n ...\n\n def random(self):\n return choice(quotes)\n\nAnd point your browser to: http://localhost:5000/quotes/random/ and see\nthat a random quote is returned each time. Voil\u00e0!\n\nSo by now you must be keenly aware of the fact that you have not defined a\nsingle route, but yet routing is obviously taking place. \"Is this voodoo?\"\nyou ask?\n\nNot at all. Flask-Classful will automatically create routes for any method\nin a FlaskView that doesn't begin with an underscore character.\nYou can still define your own routes of course, and we'll look at that next.\n\n",
"bugtrack_url": null,
"license": "",
"summary": "Class based views for Flask",
"version": "0.16.0",
"project_urls": {
"Source": "https://github.com/pallets-eco/flask-classful/"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "bcc42d11d7b9190e478107078f158ba69cb0fe5c5632ad486c1115cc6d3a4163",
"md5": "15edcb2e1b48b1d8503b7a467db1a9ee",
"sha256": "4a2f9020588caf62ee0662e98b02503a99b2422ed29e9832c85f143f652e6ed1"
},
"downloads": -1,
"filename": "flask_classful-0.16.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "15edcb2e1b48b1d8503b7a467db1a9ee",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7",
"size": 9482,
"upload_time": "2023-09-07T21:44:48",
"upload_time_iso_8601": "2023-09-07T21:44:48.135854Z",
"url": "https://files.pythonhosted.org/packages/bc/c4/2d11d7b9190e478107078f158ba69cb0fe5c5632ad486c1115cc6d3a4163/flask_classful-0.16.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "549f0a2e2144e3e32d1d8e5bd66acb3cb54f76b7072ab832a6be9b7d707d2015",
"md5": "3d9467ec192d268628d7213ee9841d44",
"sha256": "9073dc705f59e301da84d981f48bdff1901a5170700b685f42548d6f754156d5"
},
"downloads": -1,
"filename": "flask_classful-0.16.0.tar.gz",
"has_sig": false,
"md5_digest": "3d9467ec192d268628d7213ee9841d44",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7",
"size": 8806,
"upload_time": "2023-09-07T21:44:49",
"upload_time_iso_8601": "2023-09-07T21:44:49.908058Z",
"url": "https://files.pythonhosted.org/packages/54/9f/0a2e2144e3e32d1d8e5bd66acb3cb54f76b7072ab832a6be9b7d707d2015/flask_classful-0.16.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-09-07 21:44:49",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "pallets-eco",
"github_project": "flask-classful",
"travis_ci": false,
"coveralls": true,
"github_actions": true,
"requirements": [
{
"name": "pytest",
"specs": []
},
{
"name": "coverage",
"specs": []
},
{
"name": "flake8",
"specs": []
},
{
"name": "pylint",
"specs": []
}
],
"lcname": "flask-classful"
}