def app(): db_fd, db_path = tempfile.mkstemp() app = create_app({ 'TESTING': True, 'DATABASE': db_path, }) with app.app_context(): init_db() get_db().executescript(_data_sql) yield app os.close(db_fd) os.unlink(db_path)
def app(): # Creates and opens a temporary file, returning the file object and the path to it. db_fd, db_path = tempfile.mkstemp() # The app fixture will call the factory and pass test_config to configure the application # and database for testing instead of using your local development configuration. app = create_app({ # Tells Flask that the app is in test mode. 'TESTIING': True, # The DATABASE path is overridden so it points to this temporary path instead of the instance folder. 'DATABASE': db_path, }) with app.app_context(): init_db() get_db().executescript(_data_sql) yield app os.close(db_fd) os.unlink(db_path)
from portfolio import create_app app = create_app() if __name__ == "__main__": app.run(debug=True)
import portfolio, os, json app = portfolio.create_app() @app.cli.command('create_db', with_appcontext=True) def create_db(): portfolio.db.create_all() @app.cli.command('delete_db', with_appcontext=True) def delete_db(): portfolio.db.drop_all() @app.cli.command('reset_admin', with_appcontext=True) def reset_admin(): portfolio.db.drop_all() portfolio.db.create_all() create_admin() def create_admin(): from portfolio.models import Admin, Tag from portfolio import bcrypt file_path = os.path.join(app.instance_path, 'admin.json') try: with open(file_path, 'r') as file: data = json.load(file) master = Admin(name=data['name'], email=data['email'],
#!env/bin/python import sys from portfolio import create_app class ProductionConfig(object): DEBUG = False TESTING = False app = create_app(config=ProductionConfig) if __name__ == "__main__": if len(sys.argv) > 1 and sys.argv[1] == "-p": print(" * Running in production mode") app = create_app(config=ProductionConfig) else: app = create_app() app.run(host="0.0.0.0")
def test_config(): assert not create_app().testing assert create_app({'TESTING': True}).testing
from flask import Flask, render_template, url_for from portfolio import create_app application = create_app() @application.route('/') def main(): return render_template('about.html') if __name__ == '__main__': application.run(host='0.0.0.0', debug=True)