5

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?

Grant Palin
1,7112 gold badges14 silver badges29 bronze badges
asked Sep 20, 2011 at 21:13

1 Answer 1

6

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.

answered Sep 21, 2011 at 1:05

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.