Question

I'm currently writing a web application in Python using the Flask web framework. I'm really getting used to just putting everything in the one file, unlike many other projects I see where they have different directories for classes, views, and stuff. However, the Flask example just stuff everything into the one file, which is what I seem to be going with.

Is there any risks or problems in writing the whole web app in the one single file, or is it better to spread out my functions and classes across separate files?

Was it helpful?

Solution

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:

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.

OTHER TIPS

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 ?

As it is a micro framework, you should not rely on it to build full blown applications since it's not designed for it.

As long a you keep your project small (a few forms, a few tables and mostlys static contents) you will be fine. But if you want to have a bigger application, you might "out program" the capacities of the framework in terms of modularity and reuse of code. In that case you might want to migrate toward a full blown framework where everything is separated in their own module.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top