Flask blueprint injection12/15/2023 ![]() We've considered tips to overcome a common architectural problem of Flask apps related to cyclic imports. We suggest to look at Flask project using approaches described above, through the example of the "Bulls and cows" game, designed in the form of the web app. The measures mentioned in this article allow to get rid of cyclic imports, as well as to improve the quality of a code. Import dependency_injector.providers as di_prvĬlass DIServices(di_cnt.DeclarativeContainer): The example of using a pattern in the code with the dependency-injector library: app.py For this purpose, it is good to use the dependency injection pattern implemented in the dependency-injector library. Next, you need to realise the logic to get an access to global objects in the application. ![]() The implementation of the classy approach and the rejection of flask-sqlalchemy are just first steps to solve the problem of cyclic import. We try not to use flask-sqlalchemy for these reasons. As a result, these models cannot be used in subprojects or in a supporting script. There is a need to describe models using your own classes, which lead to a tight binding of models to the Flask project.The extension promotes the use of a global object for working with the database, including models’ creation, which again leads to the problem of cyclic imports.The flask-sqlalchemy extension is designed to improve the integration between sqlalchemy and flask, but in practice it often brings more problems than benefits: One of the best examples is flask-sqlalchemy. This problem in classic Flask projects occurs due to both view declaration and some extensions. The code presented above shows how flask-classful helps to cope with cyclic imports. An app is divided into sub projects that are configured by blueprint and then are registered in app object by only one line of code. When examining the code, you should pay attention to the fact that initialization now takes place in app.py, which is located at the root. The example of the project structure while working with the flask-classful library:įrom flask_classy import FlaskView, route As a result, the problem with cyclic import is solved. This approach allows to improve the structure of a code, as well as to create view without app object. With this approach, you don't have to manually write routing for view: it will be configured automatically based on names of your classes and methods. Instead of using the standard routing method described in the documentation, you can use the classy approach. We prefer original versions of libraries, without "wrapping".Subsequently, it will be difficult to elaborate such project as changing the code in one place will lead to changes in a dozen of other places.Īs a rule, we solve the problem as follows: IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.Obviously, this architecture is not so good since all the components are strongly connected. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: ![]() Gunicorn is configured, but check the Dockerfile as the workers and threads are set to 1 each for easier debugging. ![]() Dockerfileĭockerfile sets up a non-root user and gives that user ownership of the directory where the API is located. Config injection is configured if needed. Python Flask API scaffold using blueprints for routing. ![]()
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |