Grammatical fixes in Foreword and Tutorial
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Kevin Yap · committed by Kevin Yap · parent 475d707 · Browse files · 12 files·+98 added−96 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
12 files+98 added−96 removed
- other:+98 added−96 removed
Provenance
- Commit
23fc2e5- Tree
094d1c1- Author
Kevin Yap <me@kevinyap.ca>- Committer
Kevin Yap <me@kevinyap.ca>- Parent
475d707
12 files changed+98 added−96 removed
modified:docs/tutorial/css.rst+2 added-2 removed
2 unmodified lines3 Step 7: Adding Style4 ====================5 6 Now that everything else works, it's time to add some style to the7-application. Just create a stylesheet called :file:`style.css` in the :file:`static`8-folder we created before:7+application. Just create a stylesheet called :file:`style.css` in the8+:file:`static` folder we created before:9 10 .. sourcecode:: css11 12 body { font-family: sans-serif; background: #eee; }19 unmodified linesmodified:docs/tutorial/dbcon.rst+13 added-13 removed
2 unmodified lines3 Step 3: Database Connections4 ----------------------------5 6 We have created a function for establishing a database connection with7-`connect_db` but by itself that's not particularly useful. Creating and7+`connect_db`, but by itself, that's not particularly useful. Creating and8 closing database connections all the time is very inefficient, so we want9 to keep it around for longer. Because database connections encapsulate a10-transaction we also need to make sure that only one request at the time11-uses the connection. So how can we elegantly do that with Flask?10+transaction, we also need to make sure that only one request at the time11+uses the connection. How can we elegantly do that with Flask?12 13-This is where the application context comes into play. So let's start13+This is where the application context comes into play, so let's start14 there.15 16 Flask provides us with two contexts: the application context and the17-request context. For the time being all you have to know is that there18-are special variables that use these. For instance the17+request context. For the time being, all you have to know is that there18+are special variables that use these. For instance, the19 :data:`~flask.request` variable is the request object associated with20 the current request, whereas :data:`~flask.g` is a general purpose21 variable associated with the current application context. We will go into22 the details of this a bit later.23 24-For the time being all you have to know is that you can store information24+For the time being, all you have to know is that you can store information25 safely on the :data:`~flask.g` object.26 27 So when do you put it on there? To do that you can make a helper28-function. The first time the function is called it will create a database29-connection for the current context and successive calls will return the28+function. The first time the function is called, it will create a database29+connection for the current context, and successive calls will return the30 already established connection::31 32 def get_db():33 """Opens a new database connection if there is none yet for the4 unmodified lines38 return g.sqlite_db39 40 41 So now we know how to connect, but how do we properly disconnect? For42-that flask provides us with the :meth:`~flask.Flask.teardown_appcontext`42+that, Flask provides us with the :meth:`~flask.Flask.teardown_appcontext`43 decorator. It's executed every time the application context tears down::44 45 @app.teardown_appcontext46 def close_db(error):1 unmodified line48 if hasattr(g, 'sqlite_db'):49 g.sqlite_db.close()50 51 Functions marked with :meth:`~flask.Flask.teardown_appcontext` are called52-every time the app context tears down. So what does this mean?53-Essentially the app context is created before the request comes in and is52+every time the app context tears down. What does this mean?53+Essentially, the app context is created before the request comes in and is54 destroyed (torn down) whenever the request finishes. A teardown can55 happen because of two reasons: either everything went well (the error56-parameter will be ``None``) or an exception happened in which case the error56+parameter will be ``None``) or an exception happened, in which case the error57 is passed to the teardown function.58 59 Curious about what these contexts mean? Have a look at the60 :ref:`app-context` documentation to learn more.18 unmodified linesmodified:docs/tutorial/dbinit.rst+20 added-20 removed
3 unmodified lines4 =============================5 6 As outlined earlier, Flaskr is a database powered application, and more7 precisely, it is an application powered by a relational database system. Such8-systems need a schema that tells them how to store that information. So9-before starting the server for the first time it's important to create8+systems need a schema that tells them how to store that information.9+Before starting the server for the first time, it's important to create10 that schema.11 12-Such a schema can be created by piping the `schema.sql` file into the12+Such a schema can be created by piping the ``schema.sql`` file into the13 `sqlite3` command as follows::14 15 sqlite3 /tmp/flaskr.db < schema.sql16 17-The downside of this is that it requires the sqlite3 command to be18-installed which is not necessarily the case on every system. This also19-requires that we provide the path to the database which can introduce17+The downside of this is that it requires the ``sqlite3`` command to be18+installed, which is not necessarily the case on every system. This also19+requires that we provide the path to the database, which can introduce20 errors. It's a good idea to add a function that initializes the database21 for you to the application.22 23-To do this we can create a function and hook it into the :command:`flask` command24-that initializes the database. Let me show you the code first. Just add25-this function below the `connect_db` function in :file:`flaskr.py`::23+To do this, we can create a function and hook it into the :command:`flask`24+command that initializes the database. Let me show you the code first. Just25+add this function below the `connect_db` function in :file:`flaskr.py`::26 27 def init_db():28 db = get_db()29 with app.open_resource('schema.sql', mode='r') as f:6 unmodified lines36 init_db()37 print 'Initialized the database.'38 39 The ``app.cli.command()`` decorator registers a new command with the40-:command:`flask` script. When the command executes Flask will automatically40+:command:`flask` script. When the command executes, Flask will automatically41 create a application context for us bound to the right application.42-Within the function we can then access :attr:`flask.g` and other things as42+Within the function, we can then access :attr:`flask.g` and other things as43 we would expect. When the script ends, the application context tears down44 and the database connection is released.45 46-We want to keep an actual functions around that initializes the database47-though so that we can easily create databases in unittests later. (For46+We want to keep an actual functions around that initializes the database,47+though, so that we can easily create databases in unit tests later on. (For48 more information see :ref:`testing`.)49 50 The :func:`~flask.Flask.open_resource` method of the application object51 is a convenient helper function that will open a resource that the52 application provides. This function opens a file from the resource53-location (your `flaskr` folder) and allows you to read from it. We are53+location (your ``flaskr`` folder) and allows you to read from it. We are54 using this here to execute a script on the database connection.55 56 The connection object provided by SQLite can give us a cursor object.57-On that cursor there is a method to execute a complete script. Finally we58-only have to commit the changes. SQLite 3 and other transactional57+On that cursor, there is a method to execute a complete script. Finally, we58+only have to commit the changes. SQLite3 and other transactional59 databases will not commit unless you explicitly tell it to.60 61-Now it is possible to create a database with the :command:`flask` script::61+Now, it is possible to create a database with the :command:`flask` script::62 63 flask --app=flaskr initdb64 Initialized the database.65 66 .. admonition:: Troubleshooting67 68- If you get an exception later that a table cannot be found check that69- you did execute the `initdb` command and that your table names are70- correct (singular vs. plural for example).68+ If you get an exception later on stating that a table cannot be found, check69+ that you did execute the ``initdb`` command and that your table names are70+ correct (singular vs. plural, for example).71 72 Continue with :ref:`tutorial-views`modified:docs/tutorial/folders.rst+6 added-6 removed
8 unmodified lines9 /flaskr10 /static11 /templates12 13-The `flaskr` folder is not a python package, but just something where we14-drop our files. We will then put our database schema as well as main module15-into this folder. It is done in the following way. The files inside13+The ``flaskr`` folder is not a Python package, but just something where we14+drop our files. Later on, we will put our database schema as well as main15+module into this folder. It is done in the following way. The files inside16 the :file:`static` folder are available to users of the application via HTTP.17-This is the place where css and javascript files go. Inside the18-:file:`templates` folder Flask will look for `Jinja2`_ templates. The19-templates you create later in the tutorial will go in this directory.17+This is the place where CSS and Javascript files go. Inside the18+:file:`templates` folder, Flask will look for `Jinja2`_ templates. The19+templates you create later on in the tutorial will go in this directory.20 21 Continue with :ref:`tutorial-schema`.22 23 .. _Jinja2: http://jinja.pocoo.org/modified:docs/tutorial/index.rst+3 added-3 removed
2 unmodified lines3 Tutorial4 ========5 6 You want to develop an application with Python and Flask? Here you have7-the chance to learn that by example. In this tutorial we will create a8-simple microblog application. It only supports one user that can create7+the chance to learn by example. In this tutorial, we will create a simple8+microblogging application. It only supports one user that can create9 text-only entries and there are no feeds or comments, but it still10 features everything you need to get started. We will use Flask and SQLite11-as database which comes out of the box with Python, so there is nothing11+as a database (which comes out of the box with Python) so there is nothing12 else you need.13 14 If you want the full source code in advance or for comparison, check out15 the `example source`_.17 unmodified linesmodified:docs/tutorial/introduction.rst+11 added-11 removed
1 unmodified line2 3 Introducing Flaskr4 ==================5 6-We will call our blogging application flaskr here, feel free to choose a7-less web-2.0-ish name ;) Basically we want it to do the following things:6+We will call our blogging application flaskr, but feel free to choose your own7+less Web-2.0-ish name ;) Essentially, we want it to do the following things:8 9-1. let the user sign in and out with credentials specified in the9+1. Let the user sign in and out with credentials specified in the10 configuration. Only one user is supported.11-2. when the user is logged in they can add new entries to the page11+2. When the user is logged in, they can add new entries to the page12 consisting of a text-only title and some HTML for the text. This HTML13 is not sanitized because we trust the user here.14-3. the page shows all entries so far in reverse order (newest on top) and15- the user can add new ones from there if logged in.14+3. The index page shows all entries so far in reverse chronological order15+ (newest on top) and the user can add new ones from there if logged in.16 17-We will be using SQLite3 directly for that application because it's good18-enough for an application of that size. For larger applications however19-it makes a lot of sense to use `SQLAlchemy`_ that handles database20-connections in a more intelligent way, allows you to target different17+We will be using SQLite3 directly for this application because it's good18+enough for an application of this size. For larger applications, however,19+it makes a lot of sense to use `SQLAlchemy`_, as it handles database20+connections in a more intelligent way, allowing you to target different21 relational databases at once and more. You might also want to consider22 one of the popular NoSQL databases if your data is more suited for those.23 24-Here a screenshot from the final application:24+Here a screenshot of the final application:25 26 .. image:: ../_static/flaskr.png27 :align: center28 :class: screenshot5 unmodified linesmodified:docs/tutorial/schema.rst+4 added-4 removed
1 unmodified line2 3 Step 1: Database Schema4 =======================5 6-First we want to create the database schema. Only a single table is needed7-for this application and we only want to support SQLite so creating the6+First, we want to create the database schema. Only a single table is needed7+for this application and we only want to support SQLite, so creating the8 database schema is quite easy. Just put the following contents into a file9 named `schema.sql` in the just created `flaskr` folder:10 11 .. sourcecode:: sql4 unmodified lines16 title text not null,17 'text' text not null18 );19 20-This schema consists of a single table called `entries` and each row in21-this table has an `id`, a `title` and a `text`. The `id` is an20+This schema consists of a single table called ``entries``. Each row in21+this table has an ``id``, a ``title``, and a ``text``. The ``id`` is an22 automatically incrementing integer and a primary key, the other two are23 strings that must not be null.24 25 Continue with :ref:`tutorial-setup`.modified:docs/tutorial/setup.rst+26 added-24 removed
1 unmodified line2 3 Step 2: Application Setup Code4 ==============================5 6-Now that we have the schema in place we can create the application module.7-Let's call it flaskr.py. We will place this file inside the flaskr folder.8-We will begin by adding the imports we need and by adding the config6+Now that we have the schema in place, we can create the application module.7+Let's call it ``flaskr.py``. We will place this file inside the ``flaskr``8+folder. We will begin by adding the imports we need and by adding the config9 section. For small applications, it is possible to drop the configuration10-directly into the module, and this is what we will be doing here. However11-a cleaner solution would be to create a separate ``.ini`` or ``.py`` file and12-load that or import the values from there.10+directly into the module, and this is what we will be doing here. However,11+a cleaner solution would be to create a separate ``.ini`` or ``.py`` file,12+load that, and import the values from there.13 14-First we add the imports in :file:`flaskr.py`::14+First, we add the imports in :file:`flaskr.py`::15 16 # all the imports17 import os18 import sqlite319 from flask import Flask, request, session, g, redirect, url_for, abort, \20 render_template, flash21 22-Next we can create our actual application and initialize it with the23-config from the same file, in :file:`flaskr.py`::22+Next, we can create our actual application and initialize it with the23+config from the same file in :file:`flaskr.py`::24 25 # create our little application :)26 app = Flask(__name__)27 app.config.from_object(__name__)6 unmodified lines34 PASSWORD='default'35 ))36 app.config.from_envvar('FLASKR_SETTINGS', silent=True)37 38-The :class:`~flask.Config` object works similar to a dictionary so we38+The :class:`~flask.Config` object works similarly to a dictionary so we39 can update it with new values.40 41 .. admonition:: Database Path42 43 Operating systems know the concept of a current working directory for44- each process. Unfortunately you cannot depend on this in web44+ each process. Unfortunately, you cannot depend on this in web45 applications because you might have more than one application in the46 same process.47 48 For this reason the ``app.root_path`` attribute can be used to49- get the path to the application. Together with the ``os.path`` module50- files can then easily be found. In this example we place the49+ get the path to the application. Together with the ``os.path`` module,50+ files can then easily be found. In this example, we place the51 database right next to it.52 53- For a real-work application it's recommended to use53+ For a real-world application, it's recommended to use54 :ref:`instance-folders` instead.55 56-Usually, it is a good idea to load a separate, environment specific56+Usually, it is a good idea to load a separate, environment-specific57 configuration file. Flask allows you to import multiple configurations and it58 will use the setting defined in the last import. This enables robust59 configuration setups. :meth:`~flask.Config.from_envvar` can help achieve this.60 61- app.config.from_envvar('FLASKR_SETTINGS', silent=True)61+.. code-block:: python62 63+ app.config.from_envvar('FLASKR_SETTINGS', silent=True)64+65 Simply define the environment variable :envvar:`FLASKR_SETTINGS` that points to66 a config file to be loaded. The silent switch just tells Flask to not complain67 if no such environment key is set.68 67-In addition to that you can use the :meth:`~flask.Config.from_object`69+In addition to that, you can use the :meth:`~flask.Config.from_object`70 method on the config object and provide it with an import name of a71 module. Flask will then initialize the variable from that module. Note70-that in all cases only variable names that are uppercase are considered.72+that in all cases, only variable names that are uppercase are considered.73 74 The ``SECRET_KEY`` is needed to keep the client-side sessions secure.75 Choose that key wisely and as hard to guess and complex as possible.76 75-We will also add a method that allows for easily connecting to the76-specified database. This can be used to open a connection on request and77+We will also add a method that allows for easy connections to the78+specified database. This can be used to open a connection on request and79 also from the interactive Python shell or a script. This will come in80 handy later. We create a simple database connection through SQLite and81 then tell it to use the :class:`sqlite3.Row` object to represent rows.82 This allows us to treat the rows as if they were dictionaries instead of6 unmodified lines89 rv = sqlite3.connect(app.config['DATABASE'])90 rv.row_factory = sqlite3.Row91 return rv92 91-With that out of the way you should be able to start up the application93+With that out of the way, you should be able to start up the application94 without problems. Do this with the following command::95 96 flask --app=flaskr --debug run97 3 unmodified lines101 102 You will see a message telling you that server has started along with103 the address at which you can access it.104 103-When you head over to the server in your browser you will get an 404104-page not found error because we don't have any views yet. But we will105-focus on that a little later. First we should get the database working.105+When you head over to the server in your browser, you will get a 404 error106+because we don't have any views yet. We will focus on that a little later,107+but first, we should get the database working.108 109 .. admonition:: Externally Visible Server110 111 Want your server to be publicly available? Check out the4 unmodified linesmodified:docs/tutorial/templates.rst+3 added-3 removed
1 unmodified line2 3 Step 6: The Templates4 =====================5 6-Now we should start working on the templates. If we request the URLs now7-we would only get an exception that Flask cannot find the templates. The6+Now we should start working on the templates. If we were torequest the URLs7+now, we would only get an exception that Flask cannot find the templates. The8 templates are using `Jinja2`_ syntax and have autoescaping enabled by9 default. This means that unless you mark a value in the code with10 :class:`~flask.Markup` or with the ``|safe`` filter in the template,11 Jinja2 will ensure that special characters such as ``<`` or ``>`` are75 unmodified lines87 88 login.html89 ----------90 91-Finally the login template which basically just displays a form to allow91+This is the login template, which basically just displays a form to allow92 the user to login:93 94 .. sourcecode:: html+jinja95 16 unmodified linesmodified:docs/tutorial/testing.rst+1 added-1 removed
4 unmodified lines5 6 Now that you have finished the application and everything works as7 expected, it's probably not a bad idea to add automated tests to simplify8 modifications in the future. The application above is used as a basic9-example of how to perform unittesting in the :ref:`testing` section of the9+example of how to perform unit testing in the :ref:`testing` section of the10 documentation. Go there to see how easy it is to test Flask applications.modified:docs/tutorial/views.rst+6 added-6 removed
1 unmodified line2 3 Step 5: The View Functions4 ==========================5 6-Now that the database connections are working we can start writing the6+Now that the database connections are working, we can start writing the7 view functions. We will need four of them:8 9 Show Entries10 ------------3 unmodified lines14 The one with the highest id (the newest entry) will be on top. The rows15 returned from the cursor look a bit like tuples because we are using16 the :class:`sqlite3.Row` row factory.17 18-The view function will pass the entries as dicts to the18+The view function will pass the entries as dictionaries to the19 :file:`show_entries.html` template and return the rendered one::20 21 @app.route('/')22 def show_entries():5 unmodified lines28 Add New Entry29 -------------30 31 This view lets the user add new entries if they are logged in. This only32-responds to ``POST`` requests, the actual form is shown on the33-`show_entries` page. If everything worked out well we will32+responds to ``POST`` requests; the actual form is shown on the33+`show_entries` page. If everything worked out well, we will34 :func:`~flask.flash` an information message to the next request and35 redirect back to the `show_entries` page::36 37 @app.route('/add', methods=['POST'])21 unmodified lines59 ----------------60 61 These functions are used to sign the user in and out. Login checks the62 username and password against the ones from the configuration and sets the63-`logged_in` key in the session. If the user logged in successfully, that63+`logged_in` key for the session. If the user logged in successfully, that64 key is set to ``True``, and the user is redirected back to the `show_entries`65 page. In addition, a message is flashed that informs the user that he or66 she was logged in successfully. If an error occurred, the template is67 notified about that, and the user is asked again::11 unmodified lines79 flash('You were logged in')80 return redirect(url_for('show_entries'))81 return render_template('login.html', error=error)82 83-The logout function, on the other hand, removes that key from the session83+The `logout` function, on the other hand, removes that key from the session84 again. We use a neat trick here: if you use the :meth:`~dict.pop` method85 of the dict and pass a second parameter to it (the default), the method86 will delete the key from the dictionary if present or do nothing when that87 key is not in there. This is helpful because now we don't have to check11 unmodified linesmodified:docs/foreword.rst+3 added-3 removed
7 unmodified lines8 What does "micro" mean?9 -----------------------10 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 lacking12+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, so10 unmodified lines27 Configuration and Conventions28 -----------------------------29 30 Flask has many configuration values, with sensible defaults, and a few31-conventions when getting started. By convention templates and static files are31+conventions when getting started. By convention, templates and static files are32 stored in subdirectories within the application's Python source tree, with the33-names :file:`templates` and :file:`static` respectively. While this can be changed you33+names :file:`templates` and :file:`static` respectively. While this can be changed, you34 usually don't have to, especially when getting started.35 36 Growing with Flask37 ------------------20 unmodified lines