Skip to main content

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.

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:

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?

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:

from random import choice
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.

Metadata

Release files for Flask-Classful 0.16.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for Flask-Classful 0.16.0
File Size Uploaded
flask_classful-0.16.0.tar.gz 8.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for Flask-Classful 0.16.0
File Interpreter ABI Platform
flask_classful-0.16.0-py3-none-any.whl Python 3 none any Details

Total release size: 18.3 kB

Release files / flask_classful-0.16.0.tar.gz

Download URL flask_classful-0.16.0.tar.gz
Size 8.8 kB
Tags Source
SHA-256 checksum
How to use checksums
9073dc705f59e301da84d981f48bdff1901a5170700b685f42548d6f754156d5
BLAKE2b-256 checksum
How to use checksums
549f0a2e2144e3e32d1d8e5bd66acb3cb54f76b7072ab832a6be9b7d707d2015
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/4.0.2 CPython/3.11.5

Release files / flask_classful-0.16.0-py3-none-any.whl

Download URL flask_classful-0.16.0-py3-none-any.whl
Size 9.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4a2f9020588caf62ee0662e98b02503a99b2422ed29e9832c85f143f652e6ed1
BLAKE2b-256 checksum
How to use checksums
bcc42d11d7b9190e478107078f158ba69cb0fe5c5632ad486c1115cc6d3a4163
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/4.0.2 CPython/3.11.5

Release history Release notifications | RSS feed

This release

0.16.0 This release

2 release files

0.14.2

2 release files

0.14.1

2 release files

0.14.0

2 release files

0.11.0

2 release files

0.10.0

2 release files

0.9.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page