def create_app(test_config=None): # create and configure the app app = Flask(__name__) # app = Flask(__name__, static_folder="../dist/static", template_folder="../dist") CORS(app) # app.config['SECRET_KEY'] = '12345_QBMS' app.config.from_mapping( SECRET_KEY='12345_QBMS', DEBUG = True, # DATABASE=os.path.join(app.instance_path, 'flaskr.sqlite'), ) # if test_config is None: # # load the instance config, if it exists, when not testing # app.config.from_pyfile('config.py', silent=True) # else: # # load the test config if passed in # app.config.from_mapping(test_config) # @app.route('/', methods=['GET']) # def ping_pong(): # return jsonify(app.static_folder) from flaskr import db db.init_app(app) from flaskr import api_auth, api_get, api_upload app.register_blueprint(api_auth.bp) app.register_blueprint(api_get.bp) app.register_blueprint(api_upload.bp) return app
def create_app(test_config=None): app = Flask(__name__, instance_relative_config=True) app.config.from_mapping( SECRET_KEY='dev', DATABASE=os.path.join(app.instance_path, 'flaskr.sqlite'), ) if test_config is None: app.config.from_pyfile('config.py', silent=True) else: app.config.from_mapping(test_config) try: os.mkdir(app.instance_path) except OSError: pass @app.route('/hello') def hello(): return 'Hello, World!' # register the database commands from flaskr import db db.init_app(app) # apply the blueprints to the app from flaskr import auth, blog app.register_blueprint(auth.bp) app.register_blueprint(blog.bp) # make url_for('index') == url_for('blog.index') app.add_url_rule('/', endpoint='index') return app
def create_app(test_config=None): app = Flask(__name__, instance_relative_config=True) app.config.from_mapping( SECRET_KEY='dev', DATABASE=os.path.join(app.instance_path, 'flaskr.sqlite'), ) if test_config is None: # load the instance config, if it exists, when not testing app.config.from_pyfile('config.py', silent=True) else: # load the test config if passed in app.config.from_mapping(test_config) # ensure the instance folder exists try: os.makedirs(app.instance_path) except OSError: pass @app.route('/') def hello(): return 'index page' with app.app_context(): from flaskr import db db.init_app(app) from flaskr import rack, server app.register_blueprint(rack.bp) app.register_blueprint(server.bp) return app
def create_app(test_config=None): app = Flask(__name__, instance_relative_config=True) app.config.from_mapping(SECRET_KEY=os.urandom(16), DATABASE=os.path.join(app.instance_path, 'flaskr.sqlite')) if test_config is None: print('>>> No Config File Provided') app.config.from_pyfile('config.py', silent=True) else: app.config.update(test_config) try: os.makedirs(app.instance_path) except OSError: pass from flaskr import db db.init_app(app) from flaskr import auth, blog app.register_blueprint(auth.bp) app.register_blueprint(blog.bp) app.add_url_rule('/', endpoint='index') @app.route('/') @app.route('/hello') def hello(): go_to_register = Markup(url_for('auth.register')) go_to_login = Markup(url_for('auth.login')) return f''' <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Index</title> </head> <body> <br><br><br> <button><a href="{ go_to_register }">Signup</a></button> <br><br><br> <hr> <br><br><br> <button><a href="{ go_to_login }">Login</a></button> </body> </html> ''' # Register Database Command return app
def create_app(test_config=None): app = Flask(__name__, instance_relative_config=True) app.config.from_mapping( SECRET_KEY="dev", DATABASE=os.path.join(app.instance_path, 'flaskr.sqlite'), ) if test_config is None: app.config.from_pyfile('config.py', silent=True) else: app.config.from_mapping(test_config) try: os.makedirs(app.instance_path) except OSError: pass @app.route('/hello') def hello(): return 'Hello, World!' from flaskr import db db.init_app(app) from . import auth app.register_blueprint(auth.bp) return app
def create_app(test_config=None): # create and configure the app app = Flask(__name__, instance_relative_config=True) app.config.from_mapping( SECRET_KEY='dev', DATABASE=os.path.join(app.instance_path, 'flaskr.sqlite'), ) app.config['MONGODB_SETTINGS'] = { "db": "myapp", } from flaskr import db db.init_app(app) client_db() app.register_blueprint(auth.bp) app.register_blueprint(blog.bp) app.add_url_rule('/', endpoint='index') if test_config is None: # load the instance config, if it exists, when not testing app.config.from_pyfile('config.py', silent=True) else: # load the test config if passed in app.config.from_mapping(test_config) # ensure the instance folder exists try: os.makedirs(app.instance_path) except OSError: pass return app
def create_app(test_config=None): """Create and configure an instance of the Flask application.""" print('start create app') app = Flask(__name__, instance_relative_config=True) app.config.from_mapping( # a default secret that should be overridden by instance config SECRET_KEY='dev', # store the database in the instance folder DATABASE=os.path.join(app.instance_path, 'flaskr.sqlite'), ) if test_config is None: # load the instance config, if it exists, when not testing app.config.from_pyfile('config.py', silent=True) else: # load the test config if passed in app.config.update(test_config) # ensure the instance folder exists try: os.makedirs(app.instance_path) except OSError: pass # register the database commands from flaskr import db db.init_app(app) # apply the blueprints to the app from flaskr import auth, editor app.register_blueprint(auth.bp) app.register_blueprint(editor.bp) return app
def create_app(test_config=None): app = Flask(__name__) try: if test_config is not None: environment = "test" else: environment = getenv("FLASK_ENV") app.config.from_object(get_config(environment)) except ValueError as error: print(error) exit(1) @app.route("/health-check") def health_check(): # pylint: disable=unused-variable return "FLASKR app is running." db.init_app(app) app.register_blueprint(auth.AUTH_BP) app.register_blueprint(blog.BLOG_BP) app.add_url_rule("/", endpoint="index") print(app.config) return app
def create_app(test_config=None): """Create and configure an instance of the Flask application.""" app = Flask(__name__, instance_relative_config=True) app.config.from_mapping( # a default secret that should be overridden by instance config SECRET_KEY="dev", # store the database in the instance folder DATABASE=os.path.join(app.instance_path, "flaskr.sqlite"), ) if test_config is None: # load the instance config, if it exists, when not testing app.config.from_pyfile("config.py", silent=True) else: # load the test config if passed in app.config.update(test_config) # ensure the instance folder exists try: os.makedirs(app.instance_path) except OSError: pass db.init_app(app) app.register_blueprint(auth.bp) app.register_blueprint(blog.bp) # make url_for('index') == url_for('blog.index') # in another app, you might define a separate main index here with # app.route, while giving the blog blueprint a url_prefix, but for # the tutorial the blog will be the main index app.add_url_rule("/", endpoint="index") return app
def create_app(test_config=None): """Create and configure an instance of the Flask application.""" app = Flask(__name__, instance_relative_config=True) app.config.from_mapping( # a default secret that should be overridden by instance config SECRET_KEY="dev", # store the database in the instance folder DATABASE=os.path.join(app.instance_path, "flaskr.sqlite"), ) if test_config is None: # load the instance config, if it exists, when not testing app.config.from_pyfile("config.py", silent=True) else: # load the test config if passed in app.config.update(test_config) # ensure the instance folder exists try: os.makedirs(app.instance_path) except OSError: pass @app.route("/hello") def hello(): return "Hello, World!" @app.route("/users") def show_entries(): from flaskr.db import get_db db = get_db() cur = db.execute('SELECT id, username FROM user order by id') uListStr = "" for row in cur.fetchall(): uListStr += 'ID: '+str(row[0])+' \t '+'Username:'+str(row[1])+ ' \r' return uListStr if __name__ == '__main__': app.run(debug=True) # register the database commands from flaskr import db db.init_app(app) # apply the blueprints to the app from flaskr import auth, blog app.register_blueprint(auth.bp) app.register_blueprint(blog.bp) # make url_for('index') == url_for('blog.index') # in another app, you might define a separate main index here with # app.route, while giving the blog blueprint a url_prefix, but for # the tutorial the blog will be the main index app.add_url_rule("/", endpoint="index") return app
def create_app(test_config=None): """Create and configure an instance of the Flask application.""" app = Flask(__name__, instance_relative_config=True) app.jinja_env.variable_start_string = '[[' app.jinja_env.variable_end_string = ']]' if test_config is None: # load the instance config, if it exists, when not testing app.config.from_pyfile('config.py', silent=True) else: # load the test config if passed in app.config.update(test_config) # ensure the instance folder exists try: os.makedirs(app.instance_path) except OSError: pass @app.route('/hello') def hello(): return 'Hello, World!' # register the database commands from flaskr import db db.init_app(app) app.register_blueprint(finance.bp) # make url_for('index') == url_for('blog.index') # in another app, you might define a separate main index here with # app.route, while giving the blog blueprint a url_prefix, but for # the tutorial the blog will be the main index app.add_url_rule('/', endpoint='index') return app
def create_app(test_config=None): # creating and configuring the app app = Flask(__name__, instance_relative_config=True) app.config.from_mapping(SECRET_KEY='dev', DATABASE=os.path.join(app.instance_path, 'flaskr.sqlite')) if test_config is None: # loading instance config, if it exists, when not testing app.config.from_pyfile('config.py', silent=True) else: # loading the config file if its passed app.config.from_mapping(test_config) # ensuring instance folder exists try: os.makedirs(app.instance_path) except OSError: pass from flaskr import db db.init_app(app) from flaskr import auth, blog app.register_blueprint(auth.bp) app.register_blueprint(blog.bp) app.add_url_rule('/', endpoint='index') return app
def create_app(): """Create and configure an instance of the Flask application.""" app = Flask(__name__, instance_relative_config=True) app.config.from_mapping( SECRET_KEY="dev", DATABASE=os.path.join(app.instance_path, "flaskr.sqlite"), ) app.config.from_pyfile('config.py', silent=True) try: os.makedirs(app.instance_path) except OSError: pass from flaskr import db db.init_app(app) from flaskr.blog import blog from flaskr.auth import auth from flaskr.comment import comment app.register_blueprint(auth.bp) app.register_blueprint(blog.bp) app.register_blueprint(comment.bp) return app
def create_app(test_config=None): app = flask.Flask(__name__, instance_relative_config=True) app.config.from_mapping( SECRET_KEY="dev", DATABASE=os.path.join(app.instance_path, "flaskr.sqlite"), ) if test_config is None: app.config.from_pyfile("config.py", silent=True) else: app.config.from_mapping(test_config) try: os.makedirs(app.instance_path) except OSError: pass db.init_app(app) app.register_blueprint(auth.bp) app.register_blueprint(blog.bp) app.add_url_rule("/", endpoint="/index") return app
def create_app(test_config=None): # create and configure the app app = Flask(__name__, instance_relative_config=True) app.config.from_mapping( SECRET_KEY='dev', DATABASE=os.path.join(app.instance_path, 'flaskr.sqlite'), ) if test_config is None: # load the instance config, if it exists, when not testing app.config.from_pyfile('config.py', silent=True) else: # load the test config if passed in app.config.from_mapping(test_config) # ensure the instance folder exists try: os.makedirs(app.instance_path) except OSError: pass # a simple page that says hello @app.route('/hello') def hello(): return 'Hello, 123!' from flaskr import db db.init_app(app) from flaskr import auth, blog app.register_blueprint(auth.bp) app.register_blueprint(blog.bp) app.add_url_rule('/', endpoint='index') return app
def create_app(): """Create and configure an instance of the Flask application.""" app = Flask(__name__, instance_relative_config=True) app.config.from_mapping( # a default secret that should be overridden by instance config SECRET_KEY="dev", # store the database in the instance folder DATABASE=os.path.join(app.instance_path, "flaskr.sqlite"), ) app.config.from_pyfile('config.py', silent=True) # ensure the instance folder exists try: os.makedirs(app.instance_path) except OSError: pass # register the database commands from flaskr import db db.init_app(app) # apply the blueprints to the app from flaskr.blog import blog from flaskr.auth import auth from flaskr.comment import comment app.register_blueprint(auth.bp) app.register_blueprint(blog.bp) app.register_blueprint(comment.bp) return app
def create_app(test_config=None): # create and configure the app app = Flask(__name__, instance_relative_config=True) app.config.from_mapping( SECRET_KEY='dev', DATABASE=os.path.join(app.instance_path, 'flaskr.sqlite'), ) if test_config is None: app.config.from_pyfile('config.py', silent=True) else: app.config.from_mapping(test_config) try: os.makedirs(app.instance_path) except OSError: pass @app.route('/hello') def hello(): return 'Hello World!' from flaskr import db db.init_app(app) from flaskr import auth, blog app.register_blueprint(auth.bp) app.register_blueprint(blog.bp) app.add_url_rule('/', endpoint='index') return app
def create_app(test_config= None): app = Flask(__name__, instance_relative_config= True) app.config.from_mapping( SECRET_KEY = 'dEV', DATABASE = os.path.join(app.instance_path, 'flaskr.sqlite') ) if test_config is None: app.config.from_pyfile('config.py', silent= True) else: app.config.update(test_config) try: os.makedirs(app.instance_path) except OSError: pass from flaskr import db db.init_app(app) from flaskr import auth, blog app.register_blueprint(auth.bp) app.register_blueprint(blog.bp) app.add_url_rule('/', endpoint='login') return app
def create_app(test_config=None): """Create and configure an instance of the Flask application.""" # initialize the OneAgent Python SDK if dynatrace.sdk_init(): atexit.register(dynatrace.sdk_shutdown) # create the app app = Flask(__name__, instance_relative_config=True) app.config.from_mapping( # a default secret that should be overridden by instance config SECRET_KEY='dev', # store the database in the instance folder DATABASE=os.path.join(app.instance_path, 'flaskr.sqlite'), ) if test_config is None: # load the instance config, if it exists, when not testing app.config.from_pyfile('config.py', silent=True) else: # load the test config if passed in app.config.update(test_config) # ensure the instance folder exists try: os.makedirs(app.instance_path) except OSError: pass # Do this after configuration is applied app.wsgi_app = DynatraceWSGIMiddleware( app.wsgi_app, app.name, virtual_host=app.config.get('SERVER_NAME'), context_root=app.config.get('APPLICATION_ROOT')) @app.route('/hello') def hello(): return 'Hello, World!' # register the database commands from flaskr import db db.init_app(app) # apply the blueprints to the app from flaskr import auth, blog app.register_blueprint(auth.bp) app.register_blueprint(blog.bp) app.before_request(dynatrace.start_web_request) # make url_for('index') == url_for('blog.index') # in another app, you might define a separate main index here with # app.route, while giving the blog blueprint a url_prefix, but for # the tutorial the blog will be the main index app.add_url_rule('/', endpoint='index') return app
def create_app(test_config=None): """Create and configure an instance of the Flask application.""" app = Flask(__name__, instance_relative_config=True) app.config.from_mapping( # a default secret that should be overridden by instance config SECRET_KEY="dev", # store the database in the instance folder DATABASE=os.path.join(app.instance_path, "flaskr.sqlite"), ) app.config['MYSQL_HOST'] = 'localhost' app.config['MYSQL_USER'] = '******' app.config['MYSQL_PASSWORD'] = '******' app.config['MYSQL_DB'] = 'db' mysql = MySQL(app) if test_config is None: # load the instance config, if it exists, when not testing app.config.from_pyfile("config.py", silent=True) else: # load the test config if passed in app.config.update(test_config) # ensure the instance folder exists try: os.makedirs(app.instance_path) except OSError: pass @app.route("/hello") def hello(): return "Hello, World!" # register the database commands from flaskr import db db.init_app(app) # apply the blueprints to the app from flaskr import auth, blog, oee, oee_setting, oee_add_job app.register_blueprint(auth.bp) app.register_blueprint(blog.bp) app.register_blueprint(oee.bp) app.register_blueprint(oee_setting.bp) app.register_blueprint(oee_add_job.bp) # make url_for('index') == url_for('blog.index') # in another app, you might define a separate main index here with # app.route, while giving the blog blueprint a url_prefix, but for # the tutorial the blog will be the main index app.add_url_rule("/", endpoint="index") return app
def create_app(test_config=None): """Create and configure an instance of the Flask application.""" app = Flask(__name__, instance_relative_config=True) app.config.from_mapping( # a default secret that should be overridden by instance config SECRET_KEY="dev", # store the database in the instance folder DATABASE=os.path.join(app.instance_path, "flaskr.sqlite"), # Flask Debug Toolbar defaults DEBUG_TB_ENABLED=True, DEBUG_TB_PROFILER_ENABLED=True, ) if test_config is None: # load the instance config, if it exists, when not testing app.config.from_pyfile("config.py", silent=True) else: # load the test config if passed in app.config.update(test_config) # ensure the instance folder exists try: os.makedirs(app.instance_path) except OSError: pass # register the database commands from flaskr import db db.init_app(app) # apply the blueprints to the app from flaskr import auth, blog app.register_blueprint(auth.bp) app.register_blueprint(blog.bp) # make url_for('index') == url_for('blog.index') # in another app, you might define a separate main index here with # app.route, while giving the blog blueprint a url_prefix, but for # the tutorial the blog will be the main index app.add_url_rule("/", endpoint="index") # Flask Debug Toolbar is only enabled in debug mode #app.debug = True #app.config['DEBUG_TB_HOSTS'] = ['0.0.0.0:3000', '127.0.0.1', 'port-3000.flask-tools-mlanser.codeanyapp.com'] #app.config['DEBUG_TB_PROFILER_ENABLED'] = True toolbar = DebugToolbarExtension(app) return app
def create_app(test_config=None): """Create and configure an instance of the Flask application.""" app = Flask(__name__, instance_relative_config=True) app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://localhost/project' app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False #DATABASE_URL = os.environ['DATABASE_URL'] #heroku = Heroku(app) #DATABASE_URL = os.environ['DATABASE_URL'] db = SQLAlchemy(app) app.config['SECRET_KEY'] = 'this-is-my-secret-key' # app.config.from_mapping( # # a default secret that should be overridden by instance config # SECRET_KEY="dev", # # store the database in the instance folder # DATABASE=os.path.join(app.instance_path, "flaskr.sqlite"), # ) # if test_config is None: # # load the instance config, if it exists, when not testing # app.config.from_pyfile("config.py", silent=True) # else: # # load the test config if passed in # app.config.update(test_config) # # # ensure the instance folder exists # try: # os.makedirs(app.instance_path) # except OSError: # pass # register the database commands from flaskr import db db.init_app(app) # apply the blueprints to the app from flaskr import auth, user_req app.register_blueprint(auth.bp) app.register_blueprint(user_req.bp) # make url_for('index') == url_for('blog.index') # in another app, you might define a separate main index here with # app.route, while giving the blog blueprint a url_prefix, but for # the tutorial the blog will be the main index app.add_url_rule("/", endpoint="index") return app
def app(): db_fd, db_path = tempfile.mkstemp() app = create_app({'TESTING': True, 'DATABASE': db_path}) with app.app_context(): init_app() get_db().executescript(_data_sql) yield app os.close(db_fd) os.unlink(db_path)
def create_app(test_config=None): # create and configure the app app = Flask(__name__, instance_relative_config=True) csrf = CSRFProtect(app) bootstrap = Bootstrap(app) login_manager = LoginManager(app) login_manager.login_view = 'auth.login' login_manager.login_message_category = 'info' login_manager.login_message = 'Access denied.' @login_manager.user_loader def user_loader(user_id): user = get_user_by_id(user_id) if not user: return fl_user = User() fl_user.id = user['id'] fl_user.username = user['username'] return fl_user app.config.from_mapping( SECRET_KEY='dev', DATABASE=os.path.join(app.instance_path, 'flaskr.sqlite'), ) if test_config is None: # load the instance config, if it exists, when not testing app.config.from_pyfile('config.py', silent=True) else: # load the test config if passed in app.config.from_mapping(test_config) # ensure the instance folder exists try: os.makedirs(app.instance_path) except OSError: pass from flaskr import db db.init_app(app) from flaskr import auth app.register_blueprint(auth.bp) from flaskr import three app.register_blueprint(three.bp) app.add_url_rule('/', endpoint='index') return app
def create_app(): """Create and configure an instance of the Flask application.""" app = Flask(__name__, instance_relative_config=True) app.config.from_mapping( # a default secret that should be overridden by instance config SECRET_KEY="dev", # store the database in the instance folder DATABASE=os.path.join(app.instance_path, "flaskr.sqlite"), ) app.config.from_pyfile('config.py', silent=True) # ensure the instance folder exists try: os.makedirs(app.instance_path) except OSError: pass # register the database commands from flaskr import db db.init_app(app) # apply the blueprints to the app from flaskr.blog import blog from flaskr.auth import auth from flaskr.comment import comment app.register_blueprint(auth.bp) app.register_blueprint(blog.bp) app.register_blueprint(comment.bp) @app.errorhandler(Exception) def handle_error(e): code = 500 if isinstance(e, HTTPException): code = e.code return jsonify(error=str(e)), code for ex in default_exceptions: app.register_error_handler(ex, handle_error) # make url_for('index') == url_for('blog.index') # in another app, you might define a separate main index here with # app.route, while giving the blog blueprint a url_prefix, but for # the tutorial the blog will be the main index app.add_url_rule("/", endpoint="index") return app
def create_app(test_config=None): """Create and configure an instance of the Flask application.""" app = Flask(__name__, instance_relative_config=True) app.config.from_mapping( # a default secret that should be overridden by instance config SECRET_KEY='dev', # store the database in the instance folder DATABASE=os.path.join(app.instance_path, 'flaskr.sqlite'), ) if test_config is None: # load the instance config, if it exists, when not testing app.config.from_pyfile('config.py', silent=True) else: # load the test config if passed in app.config.update(test_config) # ensure the instance folder exists try: os.makedirs(app.instance_path) except OSError: pass @app.route('/hello') def hello(): return 'Hello, World!' # register the database commands from flaskr import db db.init_app(app) # apply the blueprints to the app from flaskr import auth app.register_blueprint(auth.bp) from . import blog app.register_blueprint(blog.bp) app.add_url_rule('/', endpoint='index') from . import userview app.register_blueprint(userview.bp) # make url_for('index') == url_for('blog.index') # in another app, you might define a separate main index here with # app.route, while giving the blog blueprint a url_prefix, but for # the tutorial the blog will be the main index return app
def create_app(test_config=None): """Create and configure an instance of the Flask application.""" app = Flask(__name__, instance_relative_config=True) app.config.from_mapping( # a default secret that should be overridden by instance config SECRET_KEY="dev", # store the database in the instance folder DATABASE=os.path.join(app.instance_path, "flaskr.sqlite"), ) if test_config is None: # load the instance config, if it exists, when not testing app.config.from_pyfile("config.py", silent=True) else: # load the test config if passed in app.config.update(test_config) # ensure the instance folder exists try: os.makedirs(app.instance_path) except OSError: pass @app.route('/predict', methods=['POST']) def predict(): if request.method == 'POST': file = request.files['file'] img_bytes = file.read() class_id, class_name = get_prediction(image_bytes=img_bytes) return jsonify({'class_id': class_id, 'class_name': class_name}) # register the database commands from flaskr import db db.init_app(app) # apply the blueprints to the app from flaskr import auth, blog app.register_blueprint(auth.bp) app.register_blueprint(blog.bp) # make url_for('index') == url_for('blog.index') # in another app, you might define a separate main index here with # app.route, while giving the blog blueprint a url_prefix, but for # the tutorial the blog will be the main index app.add_url_rule("/", endpoint="index") return app
def create_app(test_config=None): # Create and configure the app: app = Flask(__name__, instance_relative_config=True) # creates Flask instance. # name of current Python module # relative to the instance folder (outside the 'flaskr' package - security reasons) app.config.from_mapping( SECRET_KEY='dev', # this value should be replace to random one when deploying: # python -c 'import os; print(os.urandom(16))' DATABASE=os.path.join(app.instance_path, 'flaskr_sqlite'), # instance_path refers to path where Flask instance is (instance folder). ) if test_config is None: # Load the instance config, if it exists, when not testing: app.config.from_pyfile('config.py', silent=True) # Overrides the default configuration with the values taken from the 'config.py' # in the instance folder if it exists. # Ex. it can be used to set a real 'SECRET_KEY' when deploying. else: app.config.from_mapping(test_config) # Ensure the instance folder exists: try: os.makedirs(app.instance_path) except OSError: pass # A simple page that says hello to user: @app.route('/hello') def hello(): return 'Hello, sir!' # Register the database commands: from flaskr import db db.init_app(app) # Apply the blueprints to the app: from flaskr import auth from flaskr import blog app.register_blueprint(auth.bp) app.register_blueprint(blog.bp) # Make any 'endpoint' 'index' equals to "/": # It means that 'url_for('index') == url_for('blog.index')' is True. app.add_url_rule('/', endpoint='index') return app
def app(): db_fd, db_path = tempfile.mkstemp() app = create_app({ 'TESTING': True, 'DATABASE': db_path, }) with app.app_context(): import flaskr.db as db db.init_app(app) yield app os.close(db_fd) os.unlink(db_path)
def init_app(app): """Initialize SQLAlchemy and flask-security.""" db.init_app(app) security.init_app(app, datastore=user_datastore, login_form=FlaskrLoginForm, register_form=FlaskrRegisterForm) # Initialize the tables on-load. with app.app_context(): initialize_tables() # Register the "Add sample user" command to the CLI app.cli.add_command(add_sample_user_command) # Register the on-request callback. app.before_request(load_logged_in_user)
def create_app(test_config=None): """Create and configure an instance of the Flask application.""" app = Flask(__name__, instance_relative_config=True) app.config.from_mapping( # a default secret that should be overridden by instance config SECRET_KEY="dev", # store the database in the instance folder DATABASE=os.path.join(app.instance_path, "flaskr.sqlite"), ) if test_config is None: # load the instance config, if it exists, when not testing app.config.from_pyfile("config.py", silent=True) else: # load the test config if passed in app.config.update(test_config) # ensure the instance folder exists try: os.makedirs(app.instance_path) except OSError: pass @app.route("/hello") def hello(): return "Hello, World!" # register the database commands from flaskr import db db.init_app(app) # apply the blueprints to the app from flaskr import auth, blog app.register_blueprint(auth.bp) app.register_blueprint(blog.bp) # make url_for('index') == url_for('blog.index') # in another app, you might define a separate main index here with # app.route, while giving the blog blueprint a url_prefix, but for # the tutorial the blog will be the main index app.add_url_rule("/", endpoint="index") return app