Skip to content

Commit 7c79ce6

Browse files
committed
Revise foreword and Becoming Big docs, pallets#484.
1 parent ff5ee03 commit 7c79ce6

File tree

4 files changed

+129
-113
lines changed

4 files changed

+129
-113
lines changed

docs/advanced_foreword.rst

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
1+
.. _advanced_foreword:
2+
13
Foreword for Experienced Programmers
24
====================================
35

4-
This chapter is for programmers who have worked with other frameworks in the
5-
past, and who may have more specific or esoteric concerns that the typical
6-
user.
7-
8-
Threads in Flask
9-
----------------
6+
Thread-Locals in Flask
7+
----------------------
108

11-
One of the design decisions with Flask was that simple tasks should be simple;
9+
One of the design decisions in Flask was that simple tasks should be simple;
1210
they should not take a lot of code and yet they should not limit you. Because
13-
of that we made a few design choices that some people might find surprising or
14-
unorthodox. For example, Flask uses thread-local objects internally so that
15-
you don’t have to pass objects around from function to function within a
16-
request in order to stay threadsafe. While this is a really easy approach and
17-
saves you a lot of time, it might also cause some troubles for very large
18-
applications because changes on these thread-local objects can happen anywhere
19-
in the same thread. In order to solve these problems we don’t hide the thread
20-
locals for you but instead embrace them and provide you with a lot of tools to
21-
make it as pleasant as possible to work with them.
11+
of that, Flask has few design choices that some people might find surprising or
12+
unorthodox. For example, Flask uses thread-local objects internally so that you
13+
don’t have to pass objects around from function to function within a request in
14+
order to stay threadsafe. This approach is convenient, but requires a valid
15+
request context for dependency injection or when attempting to reuse code which
16+
uses a value pegged to the request. The Flask project is honest about
17+
thread-locals, does not hide them, and calls out in the code and documentation
18+
where they are used.
2219

23-
Web Development is Dangerous
24-
----------------------------
20+
Develop for the Web with Caution
21+
--------------------------------
22+
23+
Always keep security in mind when building web applications.
2524

2625
If you write a web application, you are probably allowing users to register
2726
and leave their data on your server. The users are entrusting you with data.
@@ -30,22 +29,22 @@ you still want that data to be stored securely.
3029

3130
Unfortunately, there are many ways the security of a web application can be
3231
compromised. Flask protects you against one of the most common security
33-
problems of modern web applications: cross-site scripting (XSS). Unless
34-
you deliberately mark insecure HTML as secure, Flask and the underlying
35-
Jinja2 template engine have you covered. But there are many more ways to
36-
cause security problems.
32+
problems of modern web applications: cross-site scripting (XSS). Unless you
33+
deliberately mark insecure HTML as secure, Flask and the underlying Jinja2
34+
template engine have you covered. But there are many more ways to cause
35+
security problems.
3736

38-
The documentation will warn you about aspects of web development that
39-
require attention to security. Some of these security concerns
40-
are far more complex than one might think, and we all sometimes underestimate
41-
the likelihood that a vulnerability will be exploited - until a clever
42-
attacker figures out a way to exploit our applications. And don't think
43-
that your application is not important enough to attract an attacker.
44-
Depending on the kind of attack, chances are that automated bots are
45-
probing for ways to fill your database with spam, links to malicious
46-
software, and the like.
37+
The documentation will warn you about aspects of web development that require
38+
attention to security. Some of these security concerns are far more complex
39+
than one might think, and we all sometimes underestimate the likelihood that a
40+
vulnerability will be exploited - until a clever attacker figures out a way to
41+
exploit our applications. And don't think that your application is not
42+
important enough to attract an attacker. Depending on the kind of attack,
43+
chances are that automated bots are probing for ways to fill your database with
44+
spam, links to malicious software, and the like.
4745

48-
So always keep security in mind when doing web development.
46+
Flask is no different from any other framework in that you the developer must
47+
build with caution, watching for exploits when building to your requirements.
4948

5049
The Status of Python 3
5150
----------------------
@@ -65,3 +64,5 @@ using Python 2.6 and 2.7 with activated Python 3 warnings during
6564
development. If you plan on upgrading to Python 3 in the near future we
6665
strongly recommend that you read `How to write forwards compatible
6766
Python code <http://lucumr.pocoo.org/2011/1/22/forwards-compatible-python/>`_.
67+
68+
Continue to :ref:`installation` or the :ref:`quickstart`.

docs/becomingbig.rst

Lines changed: 56 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,45 +3,57 @@
33
Becoming Big
44
============
55

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.
3740
---------------------
3841

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
4557
steep learning curve and a lot of user frustration.
4658

4759
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,
5567
depending on the number of changes. Because of that, forking should be
5668
the very last resort.
5769

58-
Scaling like a Pro
59-
------------------
70+
Scale like a pro.
71+
-----------------
6072

6173
For many web applications the complexity of the code is less an issue than
6274
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
7890
processes to achieve concurrency which are all methods well supported by
7991
the underlying Werkzeug library.
8092

81-
Dialogue with the Community
93+
Discuss with the community.
8294
---------------------------
8395

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
88100
applications is getting feedback from users.

docs/contents.rst.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ instructions for web development with Flask.
99
:maxdepth: 2
1010
1111
foreword
12+
advanced_foreword
1213
installation
1314
quickstart
1415
tutorial/index

docs/foreword.rst

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,48 +8,50 @@ should or should not be using it.
88
What does "micro" mean?
99
-----------------------
1010

11-
“Micro” does not mean that your whole web application has to fit into
12-
a single Python file (although it certainly can). Nor does it mean
13-
that Flask is lacking in functionality. The "micro" in microframework
14-
means Flask aims to keep the core simple but extensible. Flask won't make
15-
many decisions for you, such as what database to use. Those decisions that
16-
it does make, such as what templating engine to use, are easy to change.
17-
Everything else is up to you, so that Flask can be everything you need
18-
and nothing you don't.
11+
“Micro” does not mean that your whole web application has to fit into a single
12+
Python file, although it certainly can. Nor does it mean that Flask is lacking
13+
in functionality. The "micro" in microframework means Flask aims to keep the
14+
core simple but extensible. Flask won't make many decisions for you, such as
15+
what database to use. Those decisions that it does make, such as what
16+
templating engine to use, are easy to change. Everything else is up to you, so
17+
that Flask can be everything you need and nothing you don't.
1918

2019
By default, Flask does not include a database abstraction layer, form
2120
validation or anything else where different libraries already exist that can
22-
handle that. Instead, FLask extensions add such functionality to your
23-
application as if it was implemented in Flask itself. Numerous extensions
21+
handle that. Instead, Flask supports extensions to add such functionality to
22+
your application as if it was implemented in Flask itself. Numerous extensions
2423
provide database integration, form validation, upload handling, various open
25-
authentication technologies, and more. Flask may be "micro", but the
26-
possibilities are endless.
24+
authentication technologies, and more. Flask may be "micro", but it's ready for
25+
production use on a variety of needs.
2726

28-
Convention over Configuration
27+
Configuration and Conventions
2928
-----------------------------
3029

31-
Flask is based on convention over configuration, which means that many things
32-
are preconfigured. For example, by convention templates and static files are
33-
stored in subdirectories within the application's Python source tree. While
34-
this can be changed you usually don't have to. We want to minimize the time
35-
you need to spend in order to get up and running, without assuming things
36-
about your needs.
37-
38-
Growing Up
39-
----------
40-
41-
Since Flask is based on a very solid foundation there is not a lot of code in
42-
Flask itself. As such it's easy to adapt even for large applications and we
43-
are making sure that you can either configure it as much as possible by
44-
subclassing things or by forking the entire codebase. If you are interested
45-
in that, check out the :ref:`becomingbig` chapter.
46-
47-
If you are curious about the Flask design principles, head over to the section
48-
about :ref:`design`.
49-
50-
For the Stalwart and Wizened...
51-
-------------------------------
52-
53-
If you're more curious about the minutiae of Flask's implementation, and
54-
whether its structure is right for your needs, read the
30+
Flask has many configuration values, with sensible defaults, and a few
31+
conventions when getting started. By convention templates and static files are
32+
stored in subdirectories within the application's Python source tree, with the
33+
names `templates` and `static` respectively. While this can be changed you
34+
usually don't have to, especially when getting started.
35+
36+
Growing with Flask
37+
------------------
38+
39+
Once you have Flask up and running, you'll find a variety of extensions
40+
available in the community to integrate your project for production. The Flask
41+
core team reviews extensions and ensures approved extensions do not break with
42+
future releases.
43+
44+
As your codebase grows, you are free to make the design decisions appropriate
45+
for your project. Flask will continue to provide a very simple glue layer to
46+
the best that Python has to offer. You can implement advanced patterns in
47+
SQLAlchemy or another database tool, introduce non-relational data persistence
48+
as appropriate, and take advantage of framework-agnostic tools built for WSGI,
49+
the Python web interface.
50+
51+
Flask includes many hooks to customize its behavior. Should you need more
52+
customization, the Flask class is built for subclassing. If you are interested
53+
in that, check out the :ref:`becomingbig` chapter. If you are curious about
54+
the Flask design principles, head over to the section about :ref:`design`.
55+
56+
Continue to :ref:`installation`, the :ref:`quickstart`, or the
5557
:ref:`advanced_foreword`.

0 commit comments

Comments
 (0)