setup.rst
This file
Path
docs/tutorial/setup.rst
64 lines · 2.2 KiB
Last changed by
Updated docs for external server information.Armin Ronacherbefore adoptionef34638
History of this fileTake it
New style for the documentation. Looks more like the website now.Armin Ronacherbefore adoptionbcf347f
lines 1–59why, one step back1Step 2: Application Setup Code2==============================34Now that we have the schema in place we can create the application module.5Let's call it `flaskr.py` inside the `flaskr` folder. For starters we6will add the imports we will need as well as the config section. For7small applications it's a possibility to drop the configuration directly8into the module which we will be doing here. However a cleaner solution9would be to create a separate `.ini` or `.py` file and load that or import10the values from there.1112::1314 # all the imports15 import sqlite316 from flask import Flask, request, session, g, redirect, url_for, \17 abort, render_template, flash1819 # configuration20 DATABASE = '/tmp/flaskr.db'21 DEBUG = True22 SECRET_KEY = 'development key'23 USERNAME = 'admin'24 PASSWORD = 'default'2526Next we can create our actual application and initialize it with the27config::2829 # create our little application :)30 app = Flask(__name__)31 app.secret_key = SECRET_KEY32 app.debug = DEBUG3334The `secret_key` is needed to keep the client-side sessions secure.35Choose that key wisely and as hard to guess and complex as possible. The36debug flag enables or disables the interactive debugger. Never leave37debug mode activated in a production system because it will allow users to38executed code on the server!3940We also add a method to easily connect to the database specified. That41can be used to open a connection on request and also from the interactive42Python shell or a script. This will come in handy later4344::4546 def connect_db():47 return sqlite3.connect(DATABASE)4849Finally we just add a line to the bottom of the file that fires up the50server if we run that file as standalone application::5152 if __name__ == '__main__':53 app.run()5455With that out of the way you should be able to start up the application56without problems. When you head over to the server you will get an 40457page not found error because we don't have any views yet. But we will58focus on that a little later. First we should get the database working.59Updated docs for external server information.Armin Ronacherbefore adoptionef34638
line 60why, one step back60.. admonition:: Externally Visible ServerNew style for the documentation. Looks more like the website now.Armin Ronacherbefore adoptionbcf347f
line 61why, one step back61Updated docs for external server information.Armin Ronacherbefore adoptionef34638
lines 62–64why, one step back62 Want your server to be publically available? Check out the63 :ref:`externally visible server <public-server>` section for more64 information.