3
3
Becoming Big
4
4
============
5
5
6
- Your application is becoming more and more complex? If you suddenly
7
- realize that Flask does things in a way that does not work out for your
8
- application there are ways to deal with that.
9
-
10
- Flask is powered by Werkzeug and Jinja2, two libraries that are in use at
11
- a number of large websites out there and all Flask does is bring those
12
- two together. Being a microframework Flask does not do much more than
13
- combining existing libraries - there is not a lot of code involved.
14
- What that means for large applications is that it's very easy to take the
15
- code from Flask and put it into a new module within the applications and
16
- expand on that.
17
-
18
- Flask is designed to be extended and modified in a couple of different
19
- ways:
20
-
21
- - Flask extensions. For a lot of reusable functionality you can create
22
- extensions. For extensions a number of hooks exist throughout Flask
23
- with signals and callback functions.
24
-
25
- - Subclassing. The majority of functionality can be changed by creating
26
- a new subclass of the :class: `~flask.Flask ` class and overriding
27
- methods provided for this exact purpose.
28
-
29
- - Forking. If nothing else works out you can just take the Flask
30
- codebase at a given point and copy/paste it into your application
31
- and change it. Flask is designed with that in mind and makes this
32
- incredible easy. You just have to take the package and copy it
33
- into your application's code and rename it (for example to
34
- `framework `). Then you can start modifying the code in there.
35
-
36
- Why consider Forking?
6
+ Here are your options when growing your codebase or scaling your application.
7
+
8
+ Read the Source.
9
+ ----------------
10
+
11
+ Flask started in part to demonstrate how to build your own framework on top of
12
+ existing well-used tools Werkzeug (WSGI) and Jinja (templating), and as it
13
+ developed, it became useful to a wide audience. As you grow your codebase,
14
+ don't just use Flask -- understand it. Read the source. Flask's code is
15
+ written to be read; it's documentation published so you can use its internal
16
+ APIs. Flask sticks to documented APIs in upstream libraries, and documents its
17
+ internal utilities so that you can find the hook points needed for your
18
+ project.
19
+
20
+ Hook. Extend.
21
+ -------------
22
+
23
+ The :ref: `api ` docs are full of available overrides, hook points, and
24
+ :ref: `signals `. You can provide custom classes for things like the request and
25
+ response objects. Dig deeper on the APIs you use, and look for the
26
+ customizations which are available out of the box in a Flask release. Look for
27
+ ways in which your project can be refactored into a collection of utilities and
28
+ Flask extensions. Explore the many extensions in the community, and look for
29
+ patterns to build your own extensions if you do not find the tools you need.
30
+
31
+ Subclass.
32
+ ---------
33
+
34
+ The :class: `~flask.Flask ` class has many methods designed for subclassing. You
35
+ can quickly add or customize behavior by subclassing :class: `~flask.Flask ` (see
36
+ the linked method docs) and using that subclass wherever you instantiate an
37
+ application class. This works well with :ref: `app-factories `.
38
+
39
+ Wrap with middleware.
37
40
---------------------
38
41
39
- The majority of code of Flask is within Werkzeug and Jinja2. These
40
- libraries do the majority of the work. Flask is just the paste that glues
41
- those together. For every project there is the point where the underlying
42
- framework gets in the way (due to assumptions the original developers
43
- had). This is natural because if this would not be the case, the
44
- framework would be a very complex system to begin with which causes a
42
+ The :ref: `app-dispatch ` chapter shows in detail how to apply middleware. You
43
+ can introduce WSGI middleware to wrap your Flask instances and introduce fixes
44
+ and changes at the layer between your Flask application and your HTTP
45
+ server. Werkzeug includes several `middlewares
46
+ <http://werkzeug.pocoo.org/docs/middlewares/> `_.
47
+
48
+ Fork.
49
+ -----
50
+
51
+ If none of the above options work, fork Flask. The majority of code of Flask
52
+ is within Werkzeug and Jinja2. These libraries do the majority of the work.
53
+ Flask is just the paste that glues those together. For every project there is
54
+ the point where the underlying framework gets in the way (due to assumptions
55
+ the original developers had). This is natural because if this would not be the
56
+ case, the framework would be a very complex system to begin with which causes a
45
57
steep learning curve and a lot of user frustration.
46
58
47
59
This is not unique to Flask. Many people use patched and modified
@@ -55,8 +67,8 @@ Furthermore integrating upstream changes can be a complex process,
55
67
depending on the number of changes. Because of that, forking should be
56
68
the very last resort.
57
69
58
- Scaling like a Pro
59
- ------------------
70
+ Scale like a pro.
71
+ -----------------
60
72
61
73
For many web applications the complexity of the code is less an issue than
62
74
the scaling for the number of users or data entries expected. Flask by
@@ -78,11 +90,11 @@ majority of servers are using either threads, greenlets or separate
78
90
processes to achieve concurrency which are all methods well supported by
79
91
the underlying Werkzeug library.
80
92
81
- Dialogue with the Community
93
+ Discuss with the community.
82
94
---------------------------
83
95
84
- The Flask developers are very interested to keep everybody happy, so as
85
- soon as you find an obstacle in your way, caused by Flask, don't hesitate
86
- to contact the developers on the mailinglist or IRC channel. The best way
87
- for the Flask and Flask- extension developers to improve it for larger
96
+ The Flask developers keep the framework accessible to users with codebases big
97
+ and small. If you find an obstacle in your way, caused by Flask, don't hesitate
98
+ to contact the developers on the mailinglist or IRC channel. The best way for
99
+ the Flask and Flask extension developers to improve the tools for larger
88
100
applications is getting feedback from users.
0 commit comments