Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I have a relatively simple web application that is written in Python using the Flask microframework. I've really enjoyed Flask's ease of use, however, as the app has grown larger it has started to become unwieldy having all of my actions (and utility functions) in a single file. My views.py is about 700 lines of code and I'd really like to break things out into more discrete units. How should I restructure my code?

share|improve this question

1 Answer 1

up vote 5 down vote accepted

There are multiple ways to structure your application:

  1. The easiest is just to stick to the functions and move them to different files. For as long as you make sure they are imported when the application starts that's perfectly okay.
  2. Use Blueprints to assign the views to “categories”. For instance backend, auth, profile, etc. Blueprints have the advantage that they can in theory be attached to multiple applications and are also a great way to implement application factories.
  3. Use the underlying Werkzeug URL map and register functions on there on a central URL map.

For all these topics there are entries in the pattern section of the Flask documentation.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.