Proofreading the documentation
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Chris Edgemon · committed by Chris Edgemon · parent 4ff9493 · Browse files · 8 files·+58 added−58 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
8 files+58 added−58 removed
- other:+58 added−58 removed
Provenance
- Commit
a7ff9db- Tree
308f6a6- Author
Chris Edgemon <chrisedgemon@pegasusnews.com>- Committer
Chris Edgemon <chrisedgemon@pegasusnews.com>- Parent
4ff9493
8 files changed+58 added−58 removed
modified:docs/deploying/mod_wsgi.rst+3 added-3 removed
9 unmodified lines10 11 If you don't have `mod_wsgi` installed yet you have to either install it using12 a package manager or compile it yourself.13 14-The mod_wsgi `installation instructions`_ cover installation instructions for15-source installations on UNIX systems.14+The mod_wsgi `installation instructions`_ cover source installations on UNIX 15+systems.16 17 If you are using ubuntu / debian you can apt-get it and activate it as follows::18 19 # apt-get install libapache2-mod-wsgi23 unmodified lines43 44 If you don't have a factory function for application creation but a singleton45 instance you can directly import that one as `application`.46 47-Store that file somewhere where you will find it again (eg:47+Store that file somewhere that you will find it again (e.g.:48 `/var/www/yourapplication`) and make sure that `yourapplication` and all49 the libraries that are in use are on the python load path. If you don't50 want to install it system wide consider using a `virtual python`_ instance.51 29 unmodified linesmodified:docs/patterns/packages.rst+3 added-3 removed
46 unmodified lines47 What did we gain from this? Now we can restructure the application a bit48 into multiple modules. The only thing you have to remember is the49 following quick checklist:50 51-1. the `Flask` application object creation have to be in the51+1. the `Flask` application object creation has to be in the52 `__init__.py` file. That way each module can import it safely and the53- `__name__` variable will resole to the correct package.53+ `__name__` variable will resolve to the correct package.54 2. all the view functions (the ones with a :meth:`~flask.Flask.route`55 decorator on top) have to be imported when in the `__init__.py` file.56- Not the objects itself, but the module it is in. Do the importing at56+ Not the object itself, but the module it is in. Do the importing at57 the *bottom* of the file.58 59 Here an example `__init__.py`::60 27 unmodified linesmodified:docs/patterns/sqlalchemy.rst+9 added-9 removed
4 unmodified lines5 6 Many people prefer `SQLAlchemy`_ for database access. In this case it's7 encouraged to use a package instead of a module for your flask application8 and drop the models into a separate module (:ref:`larger-applications`).9-Although that is not necessary but makes a lot of sense.9+While that is not necessary, it makes a lot of sense.10 11 There are three very common ways to use SQLAlchemy. I will outline each12 of them here:13 37 unmodified lines51 def shutdown_session(response):52 db_session.remove()53 return response54 55-Here an example model (put that into `models.py` for instance)::55+Here is an example model (put this into `models.py`, e.g.)::56 57 from sqlalchemy import Column, Integer, String58 from yourapplication.database import Base59 9 unmodified lines69 70 def __repr__(self):71 return '<User %r>' % (self.name, self.email)72 73-You can insert entries into the database like this then:73+You can insert entries into the database like this:74 75 >>> from yourapplication.database import db_session76 >>> from yourapplication.models import User77 >>> u = User('admin', 'admin@localhost')16 unmodified lines94 95 Manual object relational mapping has a few upsides and a few downsides96 versus the declarative approach from above. The main difference is that97 you define tables and classes separately and map them together. It's more98-flexible but a little more to type. In general it works similar to the98+flexible but a little more to type. In general it works like the99 declarative approach, so make sure to also split up your application into100 multiple modules in a package.101 102-Here the example `database.py` module for your application::102+Here is an example `database.py` module for your application::103 104 from sqlalchemy import create_engine, MetaData105 from sqlalchemy.orm import scoped_session, sessionmaker106 4 unmodified lines111 bind=engine)) 112 def init_db():113 metadata.create_all(bind=engine)114 115-As for the declarative approach you need to close down the session after115+As for the declarative approach you need to close the session after116 each request. Put this into your application module::117 118 from yourapplication.database import db_session119 1 unmodified line121 def shutdown_session(response):122 db_session.remove()123 return response124 125-Here an example table and model (put that into `models.py` for instance)::125+Here is an example table and model (put this into `models.py`)::126 127 from sqlalchemy import Table, Column, Integer, String128 from sqlalchemy.orm import mapper129 from yourapplication.database import metadata, db_session41 unmodified lines171 >>> con.execute(users.insert(name='admin', email='admin@localhost'))172 173 SQLAlchemy will automatically commit for us.174 175-To query your database, yu use the engine directly or use a connection:175+To query your database, you use the engine directly or use a connection:176 177 >>> users.select(users.c.id == 1).execute().first()178 (1, u'admin', u'admin@localhost')179 2 unmodified lines182 >>> r = users.select(users.c.id == 1).execute().first()183 >>> r['name']184 u'admin'185 186-You can also pass string of SQL statements to the186+You can also pass strings of SQL statements to the187 :meth:`~sqlalchemy.engine.base.Connection.execute` method:188 189 >>> engine.execute('select * from users where id = :1', [1]).first()190 (1, u'admin', u'admin@localhost')3 unmodified linesmodified:docs/patterns/sqlite3.rst+3 added-3 removed
1 unmodified line2 3 Using SQLite 3 with Flask4 =========================5 6-In Flask you can implement opening of dabase connections at the beginning6+In Flask you can implement opening of database connections at the beginning7 of the request and closing at the end with the8 :meth:`~flask.Flask.before_request` and :meth:`~flask.Flask.after_request`9 decorators in combination with the special :class:`~flask.g` object.10 11-So here a simple example how you can use SQLite 3 with Flask::11+So here a simple example of how you can use SQLite 3 with Flask::12 13 import sqlite314 from flask import g15 53 unmodified lines69 ---------------70 71 Relational databases need schemas, so applications often ship a72 `schema.sql` file that creates the database. It's a good idea to provide73-a function that creates the database bases on that schema. This function73+a function that creates the database based on that schema. This function74 can do that for you::75 76 from contextlib import closing77 10 unmodified linesmodified:docs/becomingbig.rst+9 added-9 removed
6 unmodified lines7 designed for large scale applications and does not attempt to do so, but8 that does not mean you picked the wrong tool in the first place.9 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 bringing those11+a number of large websites out there and all Flask does is bring those12 two together. Being a microframework, Flask is literally a single file.13 What that means for large applications is that it's probably a good idea14 to take the code from Flask and put it into a new module within the15-applications and expanding on that.15+applications and expand on that.16 17 What Could Be Improved?18 -----------------------19 20 For instance it makes a lot of sense to change the way endpoints (the21 names of the functions / URL rules) are handled to also take the module22 name into account. Right now the function name is the URL name, but23-imagine you have a large applications consisting of multiple components.23+imagine you have a large application consisting of multiple components.24 In that case, it makes a lot of sense to use dotted names for the URL25 endpoints.26 27-Here some suggestions how Flask can be modified to better accomodate large28-scale applications:27+Here are some suggestions for how Flask can be modified to better 28+accomodate large-scale applications:29 30 - implement dotted names for URL endpoints31 - get rid of the decorator function registering which causes a lot32 of troubles for applications that have circular dependencies. It1 unmodified line34 initializes or certain URLs will not be available right away. A35 better solution would be to have one module with all URLs in there and36 specifing the target functions explicitly or by name and importing37 them when needed.38-- switch to explicit request object passing. This makes it more to type38+- switch to explicit request object passing. This requires more typing39 (because you now have something to pass around) but it makes it a40 whole lot easier to debug hairy situations and to test the code.41 - integrate the `Babel`_ i18n package or `SQLAlchemy`_ directly into the42 core framework.43 44 .. _Babel: http://babel.edgewall.org/45 .. _SQLAlchemy: http://www.sqlalchemy.org/46 47-Why does not Flask do all that by Default?47+Why does Flask not do all that by Default?48 ------------------------------------------49 50 There is a huge difference between a small application that only has to51 handle a couple of requests per second and with an overall code complexity52-of less than 4000 lines of code or something of larger scale. At one52+of less than 4000 lines of code and something of larger scale. At some53 point it becomes important to integrate external systems, different54 storage backends and more.55 56 If Flask was designed with all these contingencies in mind, it would be a57-much more complex framework and less easy to get started with.57+much more complex framework and harder to get started with.modified:docs/design.rst+11 added-11 removed
1 Design Decisions in Flask2 =========================3 4 If you are curious why Flask does certain things the way it does and not5-different, this section is for you. This should give you an idea about5+differently, this section is for you. This should give you an idea about6 some of the design decisions that may appear arbitrary and surprising at7 first, especially in direct comparison with other frameworks.8 9 33 unmodified lines43 something it can be very helpful to create a minimal application to test44 specific behavior. When the application object is deleted everything it45 allocated will be freed again.46 47-Another thing that becomes possible with having an explicit object laying47+Another thing that becomes possible when you have an explicit object laying48 around in your code is that you can subclass the base class49 (:class:`~flask.Flask`) to alter specific behaviour. This would not be50-possible without hacks if the object was created ahead of time for you50+possible without hacks if the object were created ahead of time for you51 based on a class that is not exposed to you.52 53 But there is another very important reason why Flask depends on an54 explicit instanciation of that class: the package name. Whenever you27 unmodified lines82 that limitation that Jinja2 is *always* configured will probably go away,83 the decision to bundle one template engine and use that will not.84 85 Template engines are like programming languages and each of those engines86-has a certain understandment about how things work. On the surface they86+has a certain understanding about how things work. On the surface they87 all work the same: you tell the engine to evaluate a template with a set88 of variables and take the return value as string.89 90 But that's about where similarities end. Jinja2 for example has an91 extensive filter system, a certain way to do template inheritance, support92 for reusable blocks (macros) that can be used from inside templates and93-also from Python code, is using unicode for all operations, supports93+also from Python code, uses unicode for all operations, supports94 iterative template rendering, configurable syntax and more. On the other95 hand an engine like Genshi is based on XML stream evaluation, template96 inheritance by taking the availability of XPath into account and more.97 Mako on the other hand treats templates similar to Python modules.98 99-When it comes to bridge a template engine with an application or framework100-there is more than just rendering templates. Flask uses Jinja2's101-extensive autoescaping support for instance. Also it provides ways to102-access macros from Jinja2 templates.99+When it comes to connecting a template engine with an application or 100+framework there is more than just rendering templates. For instance,101+Flask uses Jinja2's extensive autoescaping support. Also it provides 102+ways to access macros from Jinja2 templates.103 104 A template abstraction layer that would not take the unique features of105 the template engines away is a science on its own and a too large106 undertaking for a microframework like Flask.7 unmodified lines114 over to the Ruby side of web development there we have a protocol very115 similar to WSGI. Just that it's called Rack there, but besides that it116 looks very much like a WSGI rendition for Ruby. But nearly all117 applications in Ruby land do not work with Rack directly, but on top of a118-lirbary with the same name. This Rack library has two equivalents in118+library with the same name. This Rack library has two equivalents in119 Python: WebOb (formerly Paste) and Werkzeug. Paste is still around but120-from my understanding it's sortof deprecated in favour of WebOb. The120+from my understanding it's sort of deprecated in favour of WebOb. The121 development of WebOb and Werkzeug started side by side with similar ideas122 in mind: be a good implementation of WSGI for other applications to take123 advantage.124 23 unmodified linesmodified:docs/installation.rst+9 added-9 removed
7 unmodified lines8 way and why there are multiple ways.9 10 Flask depends on two external libraries: `Werkzeug11 <http://werkzeug.pocoo.org/>`_ and `Jinja2 <http://jinja.pocoo.org/2/>`_.12-The first on is responsible for interfacing WSGI the latter to render12+The first one is responsible for interfacing WSGI the latter for rendering13 templates. Now you are maybe asking, what is WSGI? WSGI is a standard14 in Python that is basically responsible for ensuring that your application15-is behaving in a specific way that you can run it on different15+is behaving in a specific way so that you can run it on different16 environments (for example on a local development server, on an Apache2, on17 lighttpd, on Google's App Engine or whatever you have in mind).18 19 So how do you get all that on your computer in no time? The most kick-ass5 unmodified lines25 Virtualenv is what you want to use during development and in production if26 you have shell access. So first: what does virtualenv do? If you are27 like me and you like Python, chances are you want to use it for another28 project as well. Now the more projects you have, the more likely it is29-that you will be working with different versions of Python itself or a30-library involved. Because let's face it: quite often libraries break31-backwards compatibility and it's unlikely that your application will32-not have any dependencies, that just won't happen. So virtualenv for the29+that you will be working with different versions of Python itself or at30+least an individual library. Because let's face it: quite often libraries 31+break backwards compatibility and it's unlikely that your application will32+not have any dependencies, that just won't happen. So virtualenv to the33 rescue!34 35 It basically makes it possible to have multiple side-by-side36 "installations" of Python, each for your own project. It's not actually9 unmodified lines46 or even better::47 48 $ sudo pip install virtualenv49 50-Changes are you have virtualenv installed on your system then. Maybe it's50+Chances are you have virtualenv installed on your system then. Maybe it's51 even in your package manager (on ubuntu try ``sudo apt-get install52 python-virtualenv``).53 54 If you are on Windows and missing the `easy_install` command you have to96 unmodified lines151 Once you have done that it's important to add the `easy_install` command152 and other Python scripts to the path. To do that you have to add the153 Python installation's Script folder to the `PATH` variable.154 155-To do that, click right on your "Computer" desktop icon and click155+To do that, right-click on your "Computer" desktop icon and click156 "Properties". On Windows Vista and Windows 7 then click on "Advanced System157 settings", on Windows XP click on the "Advanced" tab instead. Then click158 on the "Environment variables" button and double click on the "Path"159 variable in the "System variables" section.4 unmodified lines164 the following value::165 166 ;C:\Python26\Scripts167 168-Then you are done. To check if it worked, open the cmd and execute168+Then you are done. To check that it worked, open the cmd and execute169 "easy_install". If you have UAC enabled it should prompt you for admin170 privileges.171 172 1 unmodified linemodified:docs/testing.rst+11 added-11 removed
6 unmodified lines7 8 Not sure where that is coming from, and it's not entirely correct, but9 also not that far from the truth. Untested applications make it hard to10 improve existing code and developers of untested applications tend to11-become pretty paranoid. If an application however has automated tests you12-can savely change things and you will instantly know if your change broke11+become pretty paranoid. If an application however has automated tests, you12+can safely change things and you will instantly know if your change broke13 something.14 15 Flask gives you a couple of ways to test applications. It mainly does16 that by exposing the Werkzeug test :class:`~werkzeug.Client` class to your17 code and handling the context locals for you. You can then use that with18-your favourite testing solution. In this documentation we will us the18+your favourite testing solution. In this documentation we will use the19 :mod:`unittest` package that comes preinstalled with each Python20 installation.21 22 The Application26 unmodified lines49 50 if __name__ == '__main__':51 unittest.main()52 53-The code in the `setUp` function creates a new test client and initialize53+The code in the `setUp` function creates a new test client and initializes54 a new database. That function is called before each individual test function.55-What the test client does for us is giving us a simple interface to the55+What the test client does is give us a simple interface to the56 application. We can trigger test requests to the application and the57 client will also keep track of cookies for us.58 59-Because SQLite3 is filesystem based we can easily use the tempfile module59+Because SQLite3 is filesystem-based we can easily use the tempfile module60 to create a temporary database and initialize it. Just make sure that you61 keep a reference to the :class:`~tempfile.NamedTemporaryFile` around (we62 store it as `self.db` because of that) so that the garbage collector does63 not remove that object and with it the database from the filesystem.47 unmodified lines111 Ran 1 test in 0.034s112 113 OK114 115-Of course you can submit forms with the test client as well which we will115+Of course you can submit forms with the test client as well, which we will116 use now to log our user in.117 118 Logging In and Out119 ------------------120 121 The majority of the functionality of our application is only available for122-the administration user. So we need a way to log our test client into the122+the administration user. So we need a way to log our test client in to the123 application and out of it again. For that we fire some requests to the124 login and logout pages with the required form data (username and125 password). Because the login and logout pages redirect, we tell the126 client to `follow_redirects`.127 128-Add the following two methods do your `FlaskrTestCase` class::128+Add the following two methods to your `FlaskrTestCase` class::129 130 def login(self, username, password):131 return self.app.post('/login', data=dict(132 username=username,3 unmodified lines136 def logout(self):137 return self.app.get('/logout', follow_redirects=True)138 139 Now we can easily test if logging in and out works and that it fails with140-invalid credentials. Add this as new test to the class::140+invalid credentials. Add this new test to the class::141 142 def test_login_logout(self):143 rv = self.login(flaskr.USERNAME, flaskr.PASSWORD)144 assert 'You were logged in' in rv.data19 unmodified lines164 assert 'No entries here so far' not in rv.data165 assert '<Hello>' in rv.data166 assert '<strong>HTML</strong> allowed here' in rv.data167 168-Here we also check that HTML is allowed in the text but not in the title168+Here we check that HTML is allowed in the text but not in the title,169 which is the intended behavior.170 171 Running that should now give us three passing tests::172 14 unmodified lines