Is it bad practice to write a whole Flask application in one file?

There is no right or wrong answer to this. One file may be easy to manage if it is a very small project and you probably are the only one working on it. Some of the reasons you split the project into multiple source files however are:

  • You only change and commit what requires change. What I mean here is if you have a large single file with all your code in it, any change in the file will mean saving/updating the entire file. Imagine if you made a mistake, the entire codebase could get screwed up.

  • You have a large team possibly with different set of duties and responsibilities. For example, you could have a designer who only takes care of the design/front end (HTML,CSS etc.). If you have all the code in one file, they are exposed to the other stuff that they don't need to worry about. Also, they can independently work on their portion without needing to worry about anything else. You minimize the risk of mistakes by having multiple source files here.

  • Easier to manage as the codebase gets bigger. Can you imagine looking through 100,000 lines of code in one single file and trying to debug a problem ?


Usually it's not a good practice to keep your app in a single file except that it's trivial or for educational purposes.

I don't want to reinvent the wheel, so here's links for sample flask project structures, skeletons and other info on the subject:

  • Flask: Large App how-to
  • https://github.com/italomaia/flask-empty
  • How to organize a relatively large Flask application?
  • Flask project structure
  • How do I Structure My Flask Applications

And, or course, read the awesome flask mega-tutorial - you'll see how your application will grow and split into logical parts step-by-step.