Take a look


Python Flask Project Structure

After Completing my PG Diploma and securing a job,  when I started writing services, one thing that amazed me was the code structure. It was then I realized the importance of structuring a project. I’m pretty sure most beginners will find it excruciating when they start writing services for industrial grade software projects.  The main use of structuring a project is for better readability and reusing the code in an effective way. Also when a project grows from its pilot to higher versions to a multi-client one, the structure will play an impact on customization based on clients requirements for enabling client based configurations if structured wisely.  So structuring a python project is the key for better coding and active version controlling. Here I’ll be sharing the basic structure of directories and sub-directories for the flask based project that I have learned and the purpose of it.

Shelves is the main project Directory. The main sub-directory is the bin directory which contains directories commoncore and utils.

Common is the place to store all the frequently used variables or constant values like some indication or warning messages strings and the static jsons like the attributes which are used in multiple places throughout the application and editing from this common directory will have an impact throughout the application.

core is the next Sub-directory where we are going to write and edit most of our code which has three more sub-directories or commonly called layers and the code flows back and forth through these layers. The First layer is the service layer where you will write a flask service which only accepts the input json data either encrypted (in most cases) or non-encrypted from the front-end and decrypts the data as it is and streamline the data to the application layer function which will return the expected output and after encrypting, the service layer returns the final output back to user.  The Application layer will accept the json from the service layer and whatever logic or modifications we have to apply here can be done. The final layer is the persistence layer where all the database related methods can be written. So the flow is from user => service => application => persistence and back  through persistence => Application => Service => user. And a good practice to follow is if your application has a feature called say SampleFit so all the services related to that particular feature Should be in separate files inside the above three layers, for eg we can name it like SamplefitService.py , SamplefitApplication.py, SamplefitPersistence.py  

utils is another important subdirectory where we can put all out common utility files. Suppose if we have some common mongo queries like insert, search etc those can be modularized to form a common mongoUtil.py which could be imported to the persistence layer files for better reusability. Same for the logger functions where we can create a LoggerUtil and use it everywhere throughout all the directories.

conf directory contains all the configurations related to the application such as the database name and port details, the service port configs, the collections configurations, the log configurations, the email configs and whatever configurations an application requires can be configured here.

logs directory can be used to maintain all the log files in different formats like json, text etc in different time based files.

static directory contents will be accessible to all without restriction and most of the images and gif files are put into the static directory.

One more directory is the template directory where you can put the HTML templates that could be used with the flask code.

LICENSE – This plaintext file describes the license you’re using for a project. It’s always a good practise to have one if you’re distributing code as you will be generating license expiry dates based on the various clients.

README.md: A description of your project and its goals.

Requirement.txt: All the required python packages to run the application should be listed in this file and the advantage is we can install all the packages at once.

Server_main.py or app.py is the python file which  makes the application up. Just run this file on any server and the services will be up.  Flask uses the concept of blueprints and that they record operations to execute when registered on an application.This file can be used to register all the services to flask after importing and all the service endpoints can be reached through this file.

The External libraries section contains the particular virtual environment or so called venv. Python applications will always use packages which are not a part of standard library and Some applications may require specific versions of those packages otherwise it won’t run. To maintain those packages required for all applications to run in one place isn’t possible. So  We can create isolated venvs in separate directories based on project dependencies and use those venvs to run projects.

Leave a Reply