![]() They help you to add extra routes and functions to your application without chunking the code into a single file. That’s all you need to know about blueprints, they’re really helpful when you want to refactor your flask application and split it into smaller parts. ![]() Reload the webpage and the application should still function like it did before. Let’s register the blueprint in votr.py from flask import Flask, render_template, request, flash, session, redirect, url_for from curity import generate_password_hash, check_password_hash from flask_migrate import Migrate from models import db, Users, Polls, Topics, Options, UserPolls from flask_admin import Admin from admin import AdminView, TopicView # Blueprintsįrom api.api import api votr = Flask ( _name_ ) votr. With this we were able to remove the redundancy of prefixing all our api urls with /api. The third argument is the url prefix of the blueprint. (This ensures that methods like render_template and send_static_files work properly and give us the actual files that we want) This name has to be set to the name of our package (which is also api) as Flask uses the import_name for some internal operations such as locating the template folder of the blueprint and locating various files and objects of the main application from the blueprint. The second argument is very important it’s the import_name. The first argument is the blueprints name The Blueprint class takes three basic arguments: items (): if not value : return jsonify () get_json () # simple validation to check if all values are properly secretįor key, value in poll. method = 'POST' : # get the poll and save it in the database route ( '/polls', methods = ) # retrieves/adds polls from/to the databaseĭef api_polls (): if request. Open api.py and include the following from models import db, Users, Polls, Topics, Options, UserPolls from flask import Blueprint, request, jsonify, session api = Blueprint ( 'api', 'api', url_prefix = '/api' ) api. ![]() py ├── migrations │ ├── versions │ ├── alembic. Your application structure should look like this. Lets create a new package and call it api That way, if something goes wrong with our api for example, you already know where to start looking. There are several ways of creating a blueprint, but personally i prefer to treat blueprints as separate python packages with everything they provide in their own folder. ![]() Well, the choice is really up to you, since flask is very liberal and doesn’t enforce much conventions on you. This makes the code more maintainable and easier to debug. Like i said earlier in this series it would make perfect sense to move all our api calls into a blueprint. The main reason why you should use blueprints is to de-couple your application into smaller re-usable components. It should be noted that a blueprint is not a ‘plug and play’ app, it cannot run on it’s own every blueprint must be registered on a real Flask application before it can be used. Just like a normal flask application, a blueprint defines a collection of views, templates and static assets. Basically, a flask blueprint is a way for you to organize your flask application into smaller and re-usable application
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |