In this Flask article we are going to learn How to Create Routes with Flask-Classy, Flask-Classy is an extension that adds class-based views to Flask. so Flask-Classy 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. if your familiar with Flask, iam sure you have used the default routing in Flask. in that way you can create routes using decorators.
Also you can read more articles on Flask.
1: Flask Building REST API with Marshmallow
2: Flask CRUD Application with SQLAlchemy
3: Flask Create News Web Application
So first of all we need to install Flask-Classy, because it is a third party library, and you can install that using pip command like this.
1 |
pip install Flask-Classy |
OK first let’s just create routes using the default way, and after that we are going to use Flask-Classy for creating of routes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
from flask import Flask #create the object of Flask app = Flask(__name__) #create the route and view function @app.route('/home') def home(): return "<h1>This is our home</h1>" #run flask app if __name__ == "__main__": app.run(debug=True) |
So it is just a simple flask app with one url route.
if you run this and go to http://localhost:5000/home, this will be the result.
OK now let’s create our route using Flask-Classy.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
from flask import Flask from flask_classy import FlaskView #create the object of Flask app = Flask(__name__) #created home view class HomeView(FlaskView): def index(self): return "<h1>Codeloop.org - Flask Classy Tutorial<h1>" HomeView.register(app) #run flask app if __name__ == "__main__": app.run(debug=True) |
You can see that in the above code, we have just created a Class, and my class extends from FlaskView.
If you run the code and go to http://localhost:5000/home/, this will be the result.
OK, now let’s create another example, and in this example the routes creation will be automatically.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
from flask import Flask from flask_classy import FlaskView #create the object of Flask app = Flask(__name__) #created home view class HomeView(FlaskView): def index(self): return "<h1>Codeloop.org - Flask Classy Tutorial<h1>" def get(self, id): return "<h1>codeloop.org, ID is {} </h1>".format(id) HomeView.register(app) #run flask app if __name__ == "__main__": app.run(debug=True) |
If you run the code and go to http://localhost:5000/home/5, this will be the result.
You can see that Flask-Classy 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.
Using Custom Routes
So now let’s just use custom routes with Flask-Classy.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
from flask import Flask from flask_classy import FlaskView, route #create the object of Flask app = Flask(__name__) #created home view class HomeView(FlaskView): def index(self): return "<h1>Codeloop.org - Flask Classy Tutorial<h1>" def get(self, id): return "<h1>codeloop.org, ID is {} </h1>".format(id) @route('/contact/') def contact(self): return "<h1>codeloop.org, this is contact route</h1>" HomeView.register(app) #run flask app if __name__ == "__main__": app.run(debug=True) |
If you run the code and go to http://localhost:5000/home/contact/, this will be the result.
Subscribe and Get Free Video Courses & Articles in your Email