| 
									
										
										
										
											2010-05-26 20:49:01 +08:00
										 |  |  | .. _tutorial-setup:
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-18 19:15:00 +08:00
										 |  |  | Step 2: Application Setup Code
 | 
					
						
							|  |  |  | ==============================
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Now that we have the schema in place we can create the application module.
 | 
					
						
							| 
									
										
										
										
											2014-05-12 16:01:53 +08:00
										 |  |  | Let's call it flaskr.py. We will place this file inside the flaskr folder.
 | 
					
						
							| 
									
										
										
										
											2013-11-09 21:41:09 +08:00
										 |  |  | We will begin by adding the imports we need and by adding the config
 | 
					
						
							|  |  |  | section.  For small applications, it is possible to drop the configuration
 | 
					
						
							|  |  |  | directly into the module, and this is what we will be doing here. However
 | 
					
						
							|  |  |  | a cleaner solution would be to create a separate `.ini` or `.py` file and
 | 
					
						
							|  |  |  | load that or import the values from there.
 | 
					
						
							| 
									
										
										
										
											2010-04-18 19:15:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-01 03:32:41 +08:00
										 |  |  | First we add the imports in `flaskr.py`::
 | 
					
						
							| 
									
										
										
										
											2010-04-18 19:15:00 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # all the imports
 | 
					
						
							| 
									
										
										
										
											2014-03-20 07:07:42 +08:00
										 |  |  |     import os
 | 
					
						
							| 
									
										
										
										
											2010-04-18 19:15:00 +08:00
										 |  |  |     import sqlite3
 | 
					
						
							| 
									
										
										
										
											2013-09-01 03:32:41 +08:00
										 |  |  |     from flask import Flask, request, session, g, redirect, url_for, abort, \
 | 
					
						
							|  |  |  |          render_template, flash
 | 
					
						
							| 
									
										
										
										
											2013-07-27 07:20:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-18 19:15:00 +08:00
										 |  |  | Next we can create our actual application and initialize it with the
 | 
					
						
							| 
									
										
										
										
											2012-03-12 11:20:32 +08:00
										 |  |  | config from the same file, in `flaskr.py`::
 | 
					
						
							| 
									
										
										
										
											2010-04-18 19:15:00 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # create our little application :)
 | 
					
						
							|  |  |  |     app = Flask(__name__)
 | 
					
						
							| 
									
										
										
										
											2010-05-28 03:17:25 +08:00
										 |  |  |     app.config.from_object(__name__)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-01 03:32:41 +08:00
										 |  |  |     # Load default config and override config from an environment variable
 | 
					
						
							|  |  |  |     app.config.update(dict(
 | 
					
						
							| 
									
										
										
										
											2014-02-09 06:07:13 +08:00
										 |  |  |         DATABASE=os.path.join(app.root_path, 'flaskr.db'),
 | 
					
						
							| 
									
										
										
										
											2013-09-01 03:32:41 +08:00
										 |  |  |         SECRET_KEY='development key',
 | 
					
						
							|  |  |  |         USERNAME='admin',
 | 
					
						
							|  |  |  |         PASSWORD='default'
 | 
					
						
							|  |  |  |     ))
 | 
					
						
							|  |  |  |     app.config.from_envvar('FLASKR_SETTINGS', silent=True)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | The :class:`~flask.Config` object works similar to a dictionary so we
 | 
					
						
							|  |  |  | can update it with new values.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 06:07:13 +08:00
										 |  |  | .. admonition:: Database Path
 | 
					
						
							| 
									
										
										
										
											2013-09-01 03:32:41 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 06:07:13 +08:00
										 |  |  |     Operating systems know the concept of a current working directory for
 | 
					
						
							|  |  |  |     each process.  Unfortunately you cannot depend on this in web
 | 
					
						
							|  |  |  |     applications because you might have more than one application in the
 | 
					
						
							|  |  |  |     same process.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     For this reason the ``app.root_path`` attribute can be used to
 | 
					
						
							|  |  |  |     get the path to the application.  Together with the ``os.path`` module
 | 
					
						
							|  |  |  |     files can then easily be found.  In this example we place the
 | 
					
						
							|  |  |  |     database right next to it.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     For a real-work application it's recommended to use
 | 
					
						
							|  |  |  |     :ref:`instance-folders` instead.
 | 
					
						
							| 
									
										
										
										
											2010-05-28 03:17:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-22 22:03:57 +08:00
										 |  |  | Usually, it is a good idea to load a separate, environment specific
 | 
					
						
							|  |  |  | configuration file.  Flask allows you to import multiple configurations and it 
 | 
					
						
							|  |  |  | will use the setting defined in the last import. This enables robust 
 | 
					
						
							|  |  |  | configuration setups.  :meth:`~flask.Config.from_envvar` can help achieve this. 
 | 
					
						
							|  |  |  |     
 | 
					
						
							| 
									
										
										
										
											2010-05-28 03:17:25 +08:00
										 |  |  |     app.config.from_envvar('FLASKR_SETTINGS', silent=True)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-22 22:03:57 +08:00
										 |  |  | Simply define the environment variable :envvar:`FLASKR_SETTINGS` that points to 
 | 
					
						
							|  |  |  | a config file to be loaded.  The silent switch just tells Flask to not complain 
 | 
					
						
							| 
									
										
										
										
											2012-03-12 11:20:32 +08:00
										 |  |  | if no such environment key is set.
 | 
					
						
							| 
									
										
										
										
											2010-04-18 19:15:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-01 03:32:41 +08:00
										 |  |  | In addition to that you can use the :meth:`~flask.Config.from_object`
 | 
					
						
							|  |  |  | method on the config object and provide it with an import name of a
 | 
					
						
							| 
									
										
										
										
											2014-05-11 14:08:35 +08:00
										 |  |  | module.  Flask will then initialize the variable from that module.  Note
 | 
					
						
							| 
									
										
										
										
											2013-09-01 03:32:41 +08:00
										 |  |  | that in all cases only variable names that are uppercase are considered.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | The ``SECRET_KEY`` is needed to keep the client-side sessions secure.
 | 
					
						
							| 
									
										
										
										
											2014-04-28 21:18:27 +08:00
										 |  |  | Choose that key wisely and as hard to guess and complex as possible.
 | 
					
						
							| 
									
										
										
										
											2010-04-18 19:15:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-09 21:41:09 +08:00
										 |  |  | We will also add a method that allows for  easily connecting to the
 | 
					
						
							|  |  |  | specified database.  This can be used to open a connection on request and
 | 
					
						
							|  |  |  | also from the interactive Python shell or a script.  This will come in
 | 
					
						
							|  |  |  | handy later.  We create a simple database connection through SQLite and
 | 
					
						
							|  |  |  | then tell it to use the :class:`sqlite3.Row` object to represent rows.
 | 
					
						
							|  |  |  | This allows us to treat the rows as if they were dictionaries instead of
 | 
					
						
							|  |  |  | tuples.
 | 
					
						
							| 
									
										
										
										
											2010-04-18 19:15:00 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | ::
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def connect_db():
 | 
					
						
							| 
									
										
										
										
											2013-09-01 03:32:41 +08:00
										 |  |  |         """Connects to the specific database."""
 | 
					
						
							|  |  |  |         rv = sqlite3.connect(app.config['DATABASE'])
 | 
					
						
							|  |  |  |         rv.row_factory = sqlite3.Row
 | 
					
						
							|  |  |  |         return rv
 | 
					
						
							| 
									
										
										
										
											2010-04-18 19:15:00 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | With that out of the way you should be able to start up the application
 | 
					
						
							| 
									
										
										
										
											2011-03-28 19:31:21 +08:00
										 |  |  | without problems.  Do this with the following command::
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-28 21:18:27 +08:00
										 |  |  |     flask --app=flaskr --debug run
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | The ``--debug`` flag enables or disables the interactive debugger.  *Never
 | 
					
						
							|  |  |  | leave debug mode activated in a production system*, because it will allow
 | 
					
						
							|  |  |  | users to execute code on the server!
 | 
					
						
							| 
									
										
										
										
											2011-03-28 19:31:21 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | You will see a message telling you that server has started along with
 | 
					
						
							|  |  |  | the address at which you can access it.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | When you head over to the server in your browser you will get an 404
 | 
					
						
							| 
									
										
										
										
											2010-04-18 19:15:00 +08:00
										 |  |  | page not found error because we don't have any views yet.  But we will
 | 
					
						
							|  |  |  | focus on that a little later.  First we should get the database working.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-24 23:46:06 +08:00
										 |  |  | .. admonition:: Externally Visible Server
 | 
					
						
							| 
									
										
										
										
											2010-04-18 19:15:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-06 14:05:35 +08:00
										 |  |  |    Want your server to be publicly available?  Check out the
 | 
					
						
							| 
									
										
										
										
											2010-04-24 23:46:06 +08:00
										 |  |  |    :ref:`externally visible server <public-server>` section for more
 | 
					
						
							|  |  |  |    information.
 | 
					
						
							| 
									
										
										
										
											2010-05-26 20:49:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-01 03:32:41 +08:00
										 |  |  | Continue with :ref:`tutorial-dbcon`.
 |