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
Provenance
- Commit
7c79ce6- Tree
39fac4f- Author
Ron DuPlain <ron.duplain@gmail.com>- Committer
Ron DuPlain <ron.duplain@gmail.com>- Parent
ff5ee03
4 files changed+118 added−102 removed
modified:docs/advanced_foreword.rst+33 added-32 removed
1+.. _advanced_foreword:2+3 Foreword for Experienced Programmers4 ====================================5 4-This chapter is for programmers who have worked with other frameworks in the5-past, and who may have more specific or esoteric concerns that the typical6-user.6+Thread-Locals in Flask7+----------------------8 8-Threads in Flask9-----------------10-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;10 they should not take a lot of code and yet they should not limit you. Because13-of that we made a few design choices that some people might find surprising or14-unorthodox. For example, Flask uses thread-local objects internally so that15-you don’t have to pass objects around from function to function within a16-request in order to stay threadsafe. While this is a really easy approach and17-saves you a lot of time, it might also cause some troubles for very large18-applications because changes on these thread-local objects can happen anywhere19-in the same thread. In order to solve these problems we don’t hide the thread20-locals for you but instead embrace them and provide you with a lot of tools to21-make it as pleasant as possible to work with them.11+of that, Flask has few design choices that some people might find surprising or12+unorthodox. For example, Flask uses thread-local objects internally so that you13+don’t have to pass objects around from function to function within a request in14+order to stay threadsafe. This approach is convenient, but requires a valid15+request context for dependency injection or when attempting to reuse code which16+uses a value pegged to the request. The Flask project is honest about17+thread-locals, does not hide them, and calls out in the code and documentation18+where they are used.19 23-Web Development is Dangerous24-----------------------------20+Develop for the Web with Caution21+--------------------------------22 23+Always keep security in mind when building web applications.24+25 If you write a web application, you are probably allowing users to register26 and leave their data on your server. The users are entrusting you with data.27 And even if you are the only user that might leave data in your application,28 you still want that data to be stored securely.29 30 Unfortunately, there are many ways the security of a web application can be31 compromised. Flask protects you against one of the most common security33-problems of modern web applications: cross-site scripting (XSS). Unless34-you deliberately mark insecure HTML as secure, Flask and the underlying35-Jinja2 template engine have you covered. But there are many more ways to36-cause security problems.32+problems of modern web applications: cross-site scripting (XSS). Unless you33+deliberately mark insecure HTML as secure, Flask and the underlying Jinja234+template engine have you covered. But there are many more ways to cause35+security problems.36 38-The documentation will warn you about aspects of web development that39-require attention to security. Some of these security concerns40-are far more complex than one might think, and we all sometimes underestimate41-the likelihood that a vulnerability will be exploited - until a clever42-attacker figures out a way to exploit our applications. And don't think43-that your application is not important enough to attract an attacker.44-Depending on the kind of attack, chances are that automated bots are45-probing for ways to fill your database with spam, links to malicious46-software, and the like.37+The documentation will warn you about aspects of web development that require38+attention to security. Some of these security concerns are far more complex39+than one might think, and we all sometimes underestimate the likelihood that a40+vulnerability will be exploited - until a clever attacker figures out a way to41+exploit our applications. And don't think that your application is not42+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 with44+spam, links to malicious software, and the like.45 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 must47+build with caution, watching for exploits when building to your requirements.48 49 The Status of Python 350 ----------------------51 11 unmodified lines63 using Python 2.6 and 2.7 with activated Python 3 warnings during64 development. If you plan on upgrading to Python 3 in the near future we65 strongly recommend that you read `How to write forwards compatible66 Python code <http://lucumr.pocoo.org/2011/1/22/forwards-compatible-python/>`_.67+68+Continue to :ref:`installation` or the :ref:`quickstart`.modified:docs/becomingbig.rst+50 added-38 removed
1 unmodified line2 3 Becoming Big4 ============5 6-Your application is becoming more and more complex? If you suddenly7-realize that Flask does things in a way that does not work out for your8-application there are ways to deal with that.6+Here are your options when growing your codebase or scaling your application.7 10-Flask is powered by Werkzeug and Jinja2, two libraries that are in use at11-a number of large websites out there and all Flask does is bring those12-two together. Being a microframework Flask does not do much more than13-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 the15-code from Flask and put it into a new module within the applications and16-expand on that.8+Read the Source.9+----------------10 18-Flask is designed to be extended and modified in a couple of different19-ways:11+Flask started in part to demonstrate how to build your own framework on top of12+existing well-used tools Werkzeug (WSGI) and Jinja (templating), and as it13+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 is15+written to be read; it's documentation published so you can use its internal16+APIs. Flask sticks to documented APIs in upstream libraries, and documents its17+internal utilities so that you can find the hook points needed for your18+project.19 21-- 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.20+Hook. Extend.21+-------------22 25-- 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.23+The :ref:`api` docs are full of available overrides, hook points, and24+:ref:`signals`. You can provide custom classes for things like the request and25+response objects. Dig deeper on the APIs you use, and look for the26+customizations which are available out of the box in a Flask release. Look for27+ways in which your project can be refactored into a collection of utilities and28+Flask extensions. Explore the many extensions in the community, and look for29+patterns to build your own extensions if you do not find the tools you need.30 29-- 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.31+Subclass.32+---------33 36-Why consider Forking?34+The :class:`~flask.Flask` class has many methods designed for subclassing. You35+can quickly add or customize behavior by subclassing :class:`~flask.Flask` (see36+the linked method docs) and using that subclass wherever you instantiate an37+application class. This works well with :ref:`app-factories`.38+39+Wrap with middleware.40 ---------------------41 39-The majority of code of Flask is within Werkzeug and Jinja2. These40-libraries do the majority of the work. Flask is just the paste that glues41-those together. For every project there is the point where the underlying42-framework gets in the way (due to assumptions the original developers43-had). This is natural because if this would not be the case, the44-framework would be a very complex system to begin with which causes a42+The :ref:`app-dispatch` chapter shows in detail how to apply middleware. You43+can introduce WSGI middleware to wrap your Flask instances and introduce fixes44+and changes at the layer between your Flask application and your HTTP45+server. Werkzeug includes several `middlewares46+<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 Flask52+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 is54+the point where the underlying framework gets in the way (due to assumptions55+the original developers had). This is natural because if this would not be the56+case, the framework would be a very complex system to begin with which causes a57 steep learning curve and a lot of user frustration.58 59 This is not unique to Flask. Many people use patched and modified60 versions of their framework to counter shortcomings. This idea is also5 unmodified lines66 Furthermore integrating upstream changes can be a complex process,67 depending on the number of changes. Because of that, forking should be68 the very last resort.69 58-Scaling like a Pro59-------------------70+Scale like a pro.71+-----------------72 73 For many web applications the complexity of the code is less an issue than74 the scaling for the number of users or data entries expected. Flask by75 itself is only limited in terms of scaling by your application code, the13 unmodified lines89 majority of servers are using either threads, greenlets or separate90 processes to achieve concurrency which are all methods well supported by91 the underlying Werkzeug library.92 81-Dialogue with the Community93+Discuss with the community.94 ---------------------------95 84-The Flask developers are very interested to keep everybody happy, so as85-soon as you find an obstacle in your way, caused by Flask, don't hesitate86-to contact the developers on the mailinglist or IRC channel. The best way87-for the Flask and Flask-extension developers to improve it for larger96+The Flask developers keep the framework accessible to users with codebases big97+and small. If you find an obstacle in your way, caused by Flask, don't hesitate98+to contact the developers on the mailinglist or IRC channel. The best way for99+the Flask and Flask extension developers to improve the tools for larger100 applications is getting feedback from users.modified:docs/contents.rst.inc+1 added
7 unmodified lines8 .. toctree::9 :maxdepth: 210 11 foreword12+ advanced_foreword13 installation14 quickstart15 tutorial/index16 templating42 unmodified linesmodified:docs/foreword.rst+34 added-32 removed
6 unmodified lines7 8 What does "micro" mean?9 -----------------------10 11-“Micro” does not mean that your whole web application has to fit into12-a single Python file (although it certainly can). Nor does it mean13-that Flask is lacking in functionality. The "micro" in microframework14-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 single12+Python file, although it certainly can. Nor does it mean that Flask is lacking13+in functionality. The "micro" in microframework means Flask aims to keep the14+core simple but extensible. Flask won't make many decisions for you, such as15+what database to use. Those decisions that it does make, such as what16+templating engine to use, are easy to change. Everything else is up to you, so17+that Flask can be everything you need and nothing you don't.18 19 By default, Flask does not include a database abstraction layer, form20 validation or anything else where different libraries already exist that can22-handle that. Instead, FLask extensions add such functionality to your23-application as if it was implemented in Flask itself. Numerous extensions21+handle that. Instead, Flask supports extensions to add such functionality to22+your application as if it was implemented in Flask itself. Numerous extensions23 provide database integration, form validation, upload handling, various open25-authentication technologies, and more. Flask may be "micro", but the26-possibilities are endless.24+authentication technologies, and more. Flask may be "micro", but it's ready for25+production use on a variety of needs.26 28-Convention over Configuration27+Configuration and Conventions28 -----------------------------29 31-Flask is based on convention over configuration, which means that many things32-are preconfigured. For example, by convention templates and static files are33-stored in subdirectories within the application's Python source tree. While34-this can be changed you usually don't have to. We want to minimize the time35-you need to spend in order to get up and running, without assuming things 36-about your needs.30+Flask has many configuration values, with sensible defaults, and a few31+conventions when getting started. By convention templates and static files are32+stored in subdirectories within the application's Python source tree, with the33+names `templates` and `static` respectively. While this can be changed you34+usually don't have to, especially when getting started.35 38-Growing Up39-----------36+Growing with Flask37+------------------38 41-Since Flask is based on a very solid foundation there is not a lot of code in42-Flask itself. As such it's easy to adapt even for large applications and we43-are making sure that you can either configure it as much as possible by44-subclassing things or by forking the entire codebase. If you are interested45-in that, check out the :ref:`becomingbig` chapter.39+Once you have Flask up and running, you'll find a variety of extensions40+available in the community to integrate your project for production. The Flask41+core team reviews extensions and ensures approved extensions do not break with42+future releases.43 47-If you are curious about the Flask design principles, head over to the section48-about :ref:`design`.44+As your codebase grows, you are free to make the design decisions appropriate45+for your project. Flask will continue to provide a very simple glue layer to46+the best that Python has to offer. You can implement advanced patterns in47+SQLAlchemy or another database tool, introduce non-relational data persistence48+as appropriate, and take advantage of framework-agnostic tools built for WSGI,49+the Python web interface.50 50-For the Stalwart and Wizened...51--------------------------------51+Flask includes many hooks to customize its behavior. Should you need more52+customization, the Flask class is built for subclassing. If you are interested53+in that, check out the :ref:`becomingbig` chapter. If you are curious about54+the Flask design principles, head over to the section about :ref:`design`.55 53-If you're more curious about the minutiae of Flask's implementation, and54-whether its structure is right for your needs, read the56+Continue to :ref:`installation`, the :ref:`quickstart`, or the57 :ref:`advanced_foreword`.