Skip to content
Myr

Revise foreword and Becoming Big docs, #484.

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Ron DuPlain · committed by Ron DuPlain · parent ff5ee03 · Browse files · 4 files+118 added−102 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+118 added−102 removed

  • other:+118 added−102 removed

4 files+118 added−102 removed

modified:docs/advanced_foreword.rst+33 added-32 removed
1.. _advanced_foreword:23Foreword for Experienced Programmers4====================================54This chapter is for programmers who have worked with other frameworks in the5past, and who may have more specific or esoteric concerns that the typical6user.6Thread-Locals in Flask7----------------------88Threads in Flask9----------------1011One of the design decisions with Flask was that simple tasks should be simple;9One of the design decisions in Flask was that simple tasks should be simple;10they should not take a lot of code and yet they should not limit you. Because13of that we made a few design choices that some people might find surprising or14unorthodox. For example, Flask uses thread-local objects internally so that15you don’t have to pass objects around from function to function within a16request in order to stay threadsafe. While this is a really easy approach and17saves you a lot of time, it might also cause some troubles for very large18applications because changes on these thread-local objects can happen anywhere19in the same thread. In order to solve these problems we don’t hide the thread20locals for you but instead embrace them and provide you with a lot of tools to21make it as pleasant as possible to work with them.11of that, Flask has few design choices that some people might find surprising or12unorthodox. For example, Flask uses thread-local objects internally so that you13don’t have to pass objects around from function to function within a request in14order to stay threadsafe. This approach is convenient, but requires a valid15request context for dependency injection or when attempting to reuse code which16uses a value pegged to the request. The Flask project is honest about17thread-locals, does not hide them, and calls out in the code and documentation18where they are used.1923Web Development is Dangerous24----------------------------20Develop for the Web with Caution21--------------------------------2223Always keep security in mind when building web applications.2425If you write a web application, you are probably allowing users to register26and leave their data on your server. The users are entrusting you with data.27And even if you are the only user that might leave data in your application,28you still want that data to be stored securely.2930Unfortunately, there are many ways the security of a web application can be31compromised. Flask protects you against one of the most common security33problems of modern web applications: cross-site scripting (XSS). Unless34you deliberately mark insecure HTML as secure, Flask and the underlying35Jinja2 template engine have you covered. But there are many more ways to36cause security problems.32problems of modern web applications: cross-site scripting (XSS). Unless you33deliberately mark insecure HTML as secure, Flask and the underlying Jinja234template engine have you covered. But there are many more ways to cause35security problems.3638The documentation will warn you about aspects of web development that39require attention to security. Some of these security concerns40are far more complex than one might think, and we all sometimes underestimate41the likelihood that a vulnerability will be exploited - until a clever42attacker figures out a way to exploit our applications. And don't think43that your application is not important enough to attract an attacker.44Depending on the kind of attack, chances are that automated bots are45probing for ways to fill your database with spam, links to malicious46software, and the like.37The documentation will warn you about aspects of web development that require38attention to security. Some of these security concerns are far more complex39than one might think, and we all sometimes underestimate the likelihood that a40vulnerability will be exploited - until a clever attacker figures out a way to41exploit our applications. And don't think that your application is not42important enough to attract an attacker. Depending on the kind of attack,43chances are that automated bots are probing for ways to fill your database with44spam, links to malicious software, and the like.4548So always keep security in mind when doing web development.46Flask is no different from any other framework in that you the developer must47build with caution, watching for exploits when building to your requirements.4849The Status of Python 350----------------------51
11 unmodified lines
63using Python 2.6 and 2.7 with activated Python 3 warnings during64development. If you plan on upgrading to Python 3 in the near future we65strongly recommend that you read `How to write forwards compatible66Python code <http://lucumr.pocoo.org/2011/1/22/forwards-compatible-python/>`_.6768Continue to :ref:`installation` or the :ref:`quickstart`.
modified:docs/becomingbig.rst+50 added-38 removed
1 unmodified line
23Becoming Big4============56Your application is becoming more and more complex? If you suddenly7realize that Flask does things in a way that does not work out for your8application there are ways to deal with that.6Here are your options when growing your codebase or scaling your application.710Flask is powered by Werkzeug and Jinja2, two libraries that are in use at11a number of large websites out there and all Flask does is bring those12two together. Being a microframework Flask does not do much more than13combining existing libraries - there is not a lot of code involved.14What that means for large applications is that it's very easy to take the15code from Flask and put it into a new module within the applications and16expand on that.8Read the Source.9----------------1018Flask is designed to be extended and modified in a couple of different19ways:11Flask started in part to demonstrate how to build your own framework on top of12existing well-used tools Werkzeug (WSGI) and Jinja (templating), and as it13developed, it became useful to a wide audience. As you grow your codebase,14don't just use Flask -- understand it. Read the source. Flask's code is15written to be read; it's documentation published so you can use its internal16APIs. Flask sticks to documented APIs in upstream libraries, and documents its17internal utilities so that you can find the hook points needed for your18project.1921- Flask extensions. For a lot of reusable functionality you can create22 extensions. For extensions a number of hooks exist throughout Flask23 with signals and callback functions.20Hook. Extend.21-------------2225- Subclassing. The majority of functionality can be changed by creating26 a new subclass of the :class:`~flask.Flask` class and overriding27 methods provided for this exact purpose.23The :ref:`api` docs are full of available overrides, hook points, and24:ref:`signals`. You can provide custom classes for things like the request and25response objects. Dig deeper on the APIs you use, and look for the26customizations which are available out of the box in a Flask release. Look for27ways in which your project can be refactored into a collection of utilities and28Flask extensions. Explore the many extensions in the community, and look for29patterns to build your own extensions if you do not find the tools you need.3029- Forking. If nothing else works out you can just take the Flask30 codebase at a given point and copy/paste it into your application31 and change it. Flask is designed with that in mind and makes this32 incredible easy. You just have to take the package and copy it33 into your application's code and rename it (for example to34 `framework`). Then you can start modifying the code in there.31Subclass.32---------3336Why consider Forking?34The :class:`~flask.Flask` class has many methods designed for subclassing. You35can quickly add or customize behavior by subclassing :class:`~flask.Flask` (see36the linked method docs) and using that subclass wherever you instantiate an37application class. This works well with :ref:`app-factories`.3839Wrap with middleware.40---------------------4139The majority of code of Flask is within Werkzeug and Jinja2. These40libraries do the majority of the work. Flask is just the paste that glues41those together. For every project there is the point where the underlying42framework gets in the way (due to assumptions the original developers43had). This is natural because if this would not be the case, the44framework would be a very complex system to begin with which causes a42The :ref:`app-dispatch` chapter shows in detail how to apply middleware. You43can introduce WSGI middleware to wrap your Flask instances and introduce fixes44and changes at the layer between your Flask application and your HTTP45server. Werkzeug includes several `middlewares46<http://werkzeug.pocoo.org/docs/middlewares/>`_.4748Fork.49-----5051If none of the above options work, fork Flask. The majority of code of Flask52is within Werkzeug and Jinja2. These libraries do the majority of the work.53Flask is just the paste that glues those together. For every project there is54the point where the underlying framework gets in the way (due to assumptions55the original developers had). This is natural because if this would not be the56case, the framework would be a very complex system to begin with which causes a57steep learning curve and a lot of user frustration.5859This is not unique to Flask. Many people use patched and modified60versions of their framework to counter shortcomings. This idea is also
5 unmodified lines
66Furthermore integrating upstream changes can be a complex process,67depending on the number of changes. Because of that, forking should be68the very last resort.6958Scaling like a Pro59------------------70Scale like a pro.71-----------------7273For many web applications the complexity of the code is less an issue than74the scaling for the number of users or data entries expected. Flask by75itself is only limited in terms of scaling by your application code, the
13 unmodified lines
89majority of servers are using either threads, greenlets or separate90processes to achieve concurrency which are all methods well supported by91the underlying Werkzeug library.9281Dialogue with the Community93Discuss with the community.94---------------------------9584The Flask developers are very interested to keep everybody happy, so as85soon as you find an obstacle in your way, caused by Flask, don't hesitate86to contact the developers on the mailinglist or IRC channel. The best way87for the Flask and Flask-extension developers to improve it for larger96The Flask developers keep the framework accessible to users with codebases big97and small. If you find an obstacle in your way, caused by Flask, don't hesitate98to contact the developers on the mailinglist or IRC channel. The best way for99the Flask and Flask extension developers to improve the tools for larger100applications is getting feedback from users.
modified:docs/contents.rst.inc+1 added
7 unmodified lines
8.. toctree::9 :maxdepth: 21011 foreword12 advanced_foreword13 installation14 quickstart15 tutorial/index16 templating
42 unmodified lines
modified:docs/foreword.rst+34 added-32 removed
6 unmodified lines
78What does "micro" mean?9-----------------------1011“Micro” does not mean that your whole web application has to fit into12a single Python file (although it certainly can). Nor does it mean13that Flask is lacking in functionality. The "micro" in microframework14means Flask aims to keep the core simple but extensible. Flask won't make 15many decisions for you, such as what database to use. Those decisions that 16it does make, such as what templating engine to use, are easy to change. 17Everything else is up to you, so that Flask can be everything you need 18and nothing you don't.11“Micro” does not mean that your whole web application has to fit into a single12Python file, although it certainly can. Nor does it mean that Flask is lacking13in functionality. The "micro" in microframework means Flask aims to keep the14core simple but extensible. Flask won't make many decisions for you, such as15what database to use. Those decisions that it does make, such as what16templating engine to use, are easy to change. Everything else is up to you, so17that Flask can be everything you need and nothing you don't.1819By default, Flask does not include a database abstraction layer, form20validation or anything else where different libraries already exist that can22handle that. Instead, FLask extensions add such functionality to your23application as if it was implemented in Flask itself. Numerous extensions21handle that. Instead, Flask supports extensions to add such functionality to22your application as if it was implemented in Flask itself. Numerous extensions23provide database integration, form validation, upload handling, various open25authentication technologies, and more. Flask may be "micro", but the26possibilities are endless.24authentication technologies, and more. Flask may be "micro", but it's ready for25production use on a variety of needs.2628Convention over Configuration27Configuration and Conventions28-----------------------------2931Flask is based on convention over configuration, which means that many things32are preconfigured. For example, by convention templates and static files are33stored in subdirectories within the application's Python source tree. While34this can be changed you usually don't have to. We want to minimize the time35you need to spend in order to get up and running, without assuming things 36about your needs.30Flask has many configuration values, with sensible defaults, and a few31conventions when getting started. By convention templates and static files are32stored in subdirectories within the application's Python source tree, with the33names `templates` and `static` respectively. While this can be changed you34usually don't have to, especially when getting started.3538Growing Up39----------36Growing with Flask37------------------3841Since Flask is based on a very solid foundation there is not a lot of code in42Flask itself. As such it's easy to adapt even for large applications and we43are making sure that you can either configure it as much as possible by44subclassing things or by forking the entire codebase. If you are interested45in that, check out the :ref:`becomingbig` chapter.39Once you have Flask up and running, you'll find a variety of extensions40available in the community to integrate your project for production. The Flask41core team reviews extensions and ensures approved extensions do not break with42future releases.4347If you are curious about the Flask design principles, head over to the section48about :ref:`design`.44As your codebase grows, you are free to make the design decisions appropriate45for your project. Flask will continue to provide a very simple glue layer to46the best that Python has to offer. You can implement advanced patterns in47SQLAlchemy or another database tool, introduce non-relational data persistence48as appropriate, and take advantage of framework-agnostic tools built for WSGI,49the Python web interface.5050For the Stalwart and Wizened...51-------------------------------51Flask includes many hooks to customize its behavior. Should you need more52customization, the Flask class is built for subclassing. If you are interested53in that, check out the :ref:`becomingbig` chapter. If you are curious about54the Flask design principles, head over to the section about :ref:`design`.5553If you're more curious about the minutiae of Flask's implementation, and54whether its structure is right for your needs, read the56Continue to :ref:`installation`, the :ref:`quickstart`, or the57:ref:`advanced_foreword`.