def arcgis_setup(flush_output_db=False, spatial_reference=54016): """Orchestration for setting up the arcpy geoprocessing workspace. Args: flush_output_db: =False contents of output db will not be deleted. =True contents of output db will be deleted. spatial_reference: =54016: Default spatial reference Gall stereographic projection https://www.spatialreference.org/ref/esri/54016/ Returns: Side effect is output db is setup, and ArcGIS Pro workspace orchestration is started with correct db. """ output_db = config_dict.get('output_gdb_dir') if flush_output_db: arcpy.AddMessage('\nFlushing Output DB') arcpy.env.workspace = output_db arcpy.AddMessage( f'Contents of Output DB Before Flush {arcpy.ListFeatureClasses()}') for fc in arcpy.ListFeatureClasses(): arcpy.AddMessage(f'Deleting: {fc}') if arcpy.Exists(fc): arcpy.Delete_management(fc) arcpy.AddMessage( f'Contents of Output DB After Flush {arcpy.ListFeatureClasses()}\n' ) # Setup Geoprocessing Environment input_db = config_dict.get('input_gdb_dir') setup_env(input_db, spatial_reference)
def create_app(config_name): app = Flask(__name__) config = config_dict.get(config_name) #根据传入的配置类名称,取出对应的配置类 config = config_dict.get(config_name) #加载配置类 app.config.from_object(config) #创建sqlalchemy对象,关联app db.init_app(app) #调用日志方法,记录程序运行信息 log_file(config.LEVEL_NAME) # 创建Session对象,读取APP中session配置信息 Session(app) # 使用CSRFProtect保护app CSRFProtect(app) # 创建redis对象 global redis_store # global将局部变量声明为一个全局的 redis_store = StrictRedis(host=config.REDIS_HOST, port=config.REDIS_PORT, decode_responses=True) # 将函数添加到系统默认的过滤器列表中 # 参数1: 函数的名字, 参数2: 过滤器的名字 from info.utils.commons import hot_news_filter app.add_template_filter(hot_news_filter, "my_filter") # 将首页蓝图index_bp,注册到app中 from info.modules.index import index_bp app.register_blueprint(index_bp) # 将新闻蓝图news_bp,注册到app中 from info.modules.news import news_bp app.register_blueprint(news_bp) # 将认证蓝图passport,注册到app中 from info.modules.passport import passport_bp app.register_blueprint(passport_bp) # 使用请求钩子拦截所有的请求,通过的在cookie中设置csrf_token @app.after_request def after_request(resp): # 调用系统方法,获取csrf_token csrf_token = generate_csrf() # 将csrf_token设置到cookie中 resp.set_cookie("csrf_token", csrf_token) # 返回响应 return resp return app
def create_app(config_name='develop'): app = Flask(__name__) config_obj = config_dict.get(config_name) log_file(config_obj.LEVEL) app.config.from_object(config_obj) app.add_template_filter(news_filter) redis_store = redis.StrictRedis(host=config_obj.REDIS_HOST, port=config_obj.REDIS_PORT, decode_responses=True) db = SQLAlchemy(app) CSRFProtect(app) Session(app) @app.after_request def csrf_protect(resp): csrf_token = generate_csrf() resp.set_cookie('csrf_token', csrf_token) return resp @app.errorhandler(404) def page_not_found(e): current_app.logger.error(e) return render_template('404.html') return app, db, redis_store
def create_app(config_type,): config_class = config_dict.get(config_type) app = Flask(__name__) app.config.from_object(config_class) global db, sr db = SQLAlchemy(app) sr = StrictRedis(host=Config.REDIS_HOST, port=Config.REDIS_PORT,decode_responses=True) # 创建session存储对象 Session(app) Migrate(app, db) from info.modules.passport import passport_blu app.register_blueprint(passport_blu) from info.modules.home import home_blu app.register_blueprint(home_blu) from info.modules.news import news_blu app.register_blueprint(news_blu) setup_log(config_class.LOG_LEVEL) # 管理表模型文件 import info.models # 在utils中定义的函数想要变成过滤器,需要在app中注册,并且转换成过滤器 from info.utils.common import func_index_convert app.add_template_filter(func_index_convert,"index_convert") return app
def create_app(config_name): # set config env Config = config_dict.get(config_name) # call log_file() register_log(Config.LEVEL_NAME) # init app app = Flask(__name__) app.config.from_object(Config) # config database db.init_app(app) # config redis global redis_store redis_store = StrictRedis(host=Config.redis_host, port=Config.redis_port, decode_responses=True) # config session sess = Session() sess.init_app(app) # config csrf csrf = CSRFProtect(app) # register bluepints from App.modules.index import index_bp app.register_blueprint(index_bp) # print(app.url_map) return app
def create_app(config_name='dev'): # create and configure the app app = Flask(__name__, instance_relative_config=True) # 根据传入的环境名称获取配置 config = config_dict.get(config_name) # 加载配置A app.config.from_object(config) ''' db.init_app(app) # 将上述配置项初始化到flask-sqlachemy配置中并进行相关初始化 ''' db.init_app(app) # 导入注册蓝图 from .views import auth app.register_blueprint(auth.bp) # 与验证蓝图不同,博客蓝图没有 url_prefix 。 # 因此 index 视图会用于 / , create 会用于 /create ,以此类推。 # 用例是 qtppy 的主要 功能,因此把用例索引作为主索引是合理的。 from .views import case app.register_blueprint(case.bp) app.add_url_rule('/', endpoint='index') #导入项目蓝图 from .views import project app.register_blueprint(project.bp) return app
def creat_app(config_name): """创建flask应用对象""" app = Flask(__name__) # 获取配置信息 conf = config_dict.get(config_name) app.config.from_object(conf) # 初始化数据库db db.init_app(app) global redis_store redis_store = redis.StrictRedis(host=conf.REDIS_HOST, port=conf.REDIS_PORT) # 初始化csrf csrf.init_app(app) # 将flask里的session数据保存到redis中 Session(app) # 向app中添加自定义的路由转换器 app.url_map.converters["re"] = RegexConverter # 注册蓝图 import api_1_0 app.register_blueprint(api_1_0.api, url_prefix="/api/v1_0") # 提供html静态文件的蓝图 import web_html app.register_blueprint(web_html.html) return app
def render_layout(map_subtitle, map_features, map_spatial_reference, address_count, output_db): """Map renderer orchestration. Orchestrates the rendering of layout and converting the layout to a pdf output file. Args: map_subtitle: Desired subtitle for the output map. map_features: Desired features to map. map_spatial_reference: Desired spatial reference for map. address_count: Addresses count variable previously calculated for output map. output_db: File location for map_features. Returns: Side effect is rendering functions are provided with correct inputs for layout configuration and pdf file output. """ aprx_path = set_path(config_dict.get('proj_dir'), 'WestNileOutbreak.aprx') aprx = arcpy.mp.ArcGISProject(aprx_path) arcpy.AddMessage(f'aprx path: {aprx.filePath}') mp = get_map(aprx, 'Map') set_spatial_reference(mp, map_spatial_reference) for f, c in map_features: fc_name = f fc = set_path(output_db, f) colour = c add_feature_to_map(mp, fc_name, fc, colour, transparency=50) # Export final map export_map(aprx, map_subtitle, address_count)
def create_app(config_name): app = Flask(__name__) config = config_dict.get(config_name) log_file(config.LEVEL_NAME) # 加载配置类到app中 app.config.from_object(config) # 创建SQLAlchemy对象,关联app db.init_app(app) # 创建redis对象 global redis_store redis_store = StrictRedis(host=config.REDIS_HOST,port=config.REDIS_PORT,decode_responses=True) # 创建session对象,读取APP中session的配置信息 Session(app) #使用csrfprotect保护app CSRFProtect(app) # 将首页蓝图index_blue,注册到app中 from info.modules.index import index_blue app.register_blueprint(index_blue) return app
def create_app(config_name): app = Flask(__name__) #type:Flask Config = config_dict.get(config_name) app.config.from_object(Config) # 配置日志文件 log_file(Config.LEVEL_NAME) # 创建SQLAlchemy global db # print(id(db)) # db.SQLAlchemy(app) db.init_app(app) # 创建redis对象 global redis_store redis_store = StrictRedis(host=Config.REDIS_HSOT,port=Config.REDIS_PORT,decode_responses=True) # 配置session信息,通过Session读取App中的session Session(app) # CSRF保护 # CSRFProtect(app) # 注册蓝图 from .modules.index import index_blue app.register_blueprint(index_blue) from .modules.passport import passport_blue app.register_blueprint(passport_blue) return app
def create_app(config_name): """通过传入不同的配置名,切换不同的环境""" app = Flask(__name__) config = config_dict.get(config_name) # 设置日志级别 log_file(config.LEVEL) app.config.from_object(Config) # app.config.from_object(Config)要 init_app之前 # SQLAlchemy对象关联app db.init_app(app) # 初始化redis配置 global redis_store redis_store = redis.StrictRedis(host=config.REDIS_HOST, port=config.REDIS_PORT) # 开启csrf 保护, 只用于服务器验证功能 # TODO # CSRFProtect(app) # 设置session保存指定位置 Session(app) # 注册蓝图 时, 导入和注册写在一起 from info.modules.index import index_blu app.register_blueprint(index_blu) from info.modules.passport import passport_blu app.register_blueprint(passport_blu) return app
def create_app(config_name): # 创建应用程序对象 app = Flask(__name__) # 通过配置名,获取配置类 config = config_dict.get(config_name) # 将配置信息加载到app中 app.config.from_object(config) # 创建日志记录文件 log_file(config.DEBUG_LEVEL) # 初始化session Session(app) # 初始化数据库,绑定数据库和应用程序app db.init_app(app) # csrf配置 CSRFProtect(app) # 初始化redis global redis_store redis_store = redis.StrictRedis(host=config.REDIS_HOST, port=config.REDIS_PORT) # 添加自定义的转换器到转换器列表中 app.url_map.converters['re'] = RegexConverter # 注册蓝图 from ihome_flask.api_1_0 import api app.register_blueprint(api, url_prefix="/api/v1.0") # 注册静态文件蓝图 from ihome_flask.ihome_flask import html app.register_blueprint(html) print(app.url_map) return app
def create_app(config_name): app = Flask(__name__) # 根据传入的配置名称,获取对应的配置类 config = config_dict.get(config_name) # 记录日志信息的方法 log_file(config.LEVEL) # 加载配置类信息 app.config.from_object(config) # 创建SQLAlchemy对象,关联app # db = SQLAlchemy(app) db.init_app(app) global redis_store #创建redis对象 redis_store = redis.StrictRedis(host=config.REDIS_HOST, port=config.REDIS_PORT, decode_responses=True) #初始化Session,读取app身上session的配置信息 Session(app) # 保护app,使用CSRFProtect CSRFProtect(app) # 注册首页蓝图index_blue, 到app中 from info.modules.index import index_blue app.register_blueprint(index_blue) # 注册认证蓝图passport_blue到app中 from info.modules.passport import passport_blue app.register_blueprint(passport_blue) # 注册新闻蓝图news_blue,到app中 from info.modules.news import news_blue app.register_blueprint(news_blue) # 注册个人中心user_blue,到app中 from info.modules.user import user_blue app.register_blueprint(user_blue) #将过滤添加,到过滤器模板列表中 app.add_template_filter(index_class, "index_class") # 使用请求钩子after_request,对所有的响应进行拦截,做统一的csrf_token的设置 @app.after_request def after_request(resp): value = generate_csrf() resp.set_cookie("csrf_token", value) return resp print(app.url_map) return app
def arcgis_setup(flush_output_db=False): # Optional flush output_db output_db = config_dict.get('output_gdb_dir') if flush_output_db: arcpy.AddMessage('\nFlushing Output DB') arcpy.env.workspace = output_db arcpy.AddMessage( f'Contents of Output DB Before Flush {arcpy.ListFeatureClasses()}') for fc in arcpy.ListFeatureClasses(): arcpy.AddMessage(f'Deleting: {fc}') if arcpy.Exists(fc): arcpy.Delete_management(fc) arcpy.AddMessage( f'Contents of Output DB After Flush {arcpy.ListFeatureClasses()}\n' ) # Setup Geoprocessing Environment spatial_ref_dataset = config_dict.get('spatial_ref_dataset') input_db = config_dict.get('input_gdb_dir') setup_env(input_db, spatial_ref_dataset)
def create_app(config_name): app = Flask(__name__) #根据传入的config_name取出对应的运行环境 config = config_dict.get(config_name) #调用日志记录方法 log_file(config.LEVEL) app.config.from_object(config) #创建SQLALchemy对象,关联app # db = SQLAlchemy(app) db.init_app(app) #创建redis对象 global redis_store redis_store = redis.StrictRedis(host=config.REDIS_HOST, port=config.REDIS_PORT, decode_responses=True) #使用CSRFProtect,对app请求保护 CSRFProtect(app) #使用Session,关联app,指定存储位置 Session(app) #将首页蓝图对象index_blue注册到app from info.modules.index import index_blu app.register_blueprint(index_blu) #将认证蓝图对象passport_blu注册到app中 from info.modules.passport import passport_blue app.register_blueprint(passport_blue) #将新闻蓝图对象news_blue注册到app中 from info.modules.news import news_blue app.register_blueprint(news_blue) #将用户蓝图对象user_blue注册到app中 from info.modules.user import user_blue app.register_blueprint(user_blue) #将过滤器添加到默认过滤器列表中 app.add_template_filter(index_class, "index_class") #使用请求钩子,after_request拦截所有响应 @app.after_request def after_request(resp): value = generate_csrf() resp.set_cookie("csrf_token", value) return resp print(app.url_map) return app
def create_app(config_name): app = Flask(__name__) #根据传入的配置类名称取出对应的类 config = config_dict.get(config_name) # 调用日志方法,并从config文件中传入相应的等级参数 log_file(config.LEVEL_NAME) #把配置信息加载到app中 app.config.from_object(config) #创建数据库应用实例化程序 db.init_app(app) #创建redis实例化程序 #将局部变量redis_store声明为全局变量 global redis_store redis_store = StrictRedis(host=config.REDIS_HOST, port=config.REDIS_PORT, decode_responses=True) #创建session实例化程序,读取app中的session配置信息 Session(app) #使用CSRFProtect保护app ['POST', 'PUT', 'PATCH', 'DELETE'] 保护这四种请求方式 CSRFProtect(app) #注册首页蓝图 from new.modules.index import index_blue app.register_blueprint(index_blue) #注册图片验证码蓝图 from new.modules.passport import passport_blue app.register_blueprint(passport_blue) #注册新闻验证码蓝图 from new.modules.news import news_blue app.register_blueprint(news_blue) #用户信息显示蓝图 from new.modules.profile import profile_blue app.register_blueprint(profile_blue) #管理员蓝图 from new.modules.admin import admin_blue app.register_blueprint(admin_blue) #使用请求钩子拦截所有的请求 @app.after_request def after_request(resp): csrf_token = generate_csrf() resp.set_cookie('csrf_token', csrf_token) return resp #捕捉异常404错误渲染页面 @app.errorhandler(404) def page_not_found(e): #多重路径无法返回时,可以使用重定向对业务面进行渲染 return redirect('/404') # return render_template('admin1/404.html') #强自定义的过滤器加载到系统默认的过滤器中 app.add_template_filter(hot_news_filter, 'my_filter') print(app.url_map) return app
def create_app(config_name): # 创建app对象 app = Flask(__name__) #通过config_name获取配置类 config = config_dict.get(config_name) # 配置日志传入日志 setup_app(config.LOGIN_LEVEL) # 加载配置类到app app.config.from_object(config) # 创建SQLAlchemy对象,关联app db.init_app(app) #初始化redis global redis_store redis_store = StrictRedis(host=config.REDIS_HOST, port=config.REDIS_PORT, password=config.REDIS_PASSWD, decode_responses=True) #开启CSRF防护,只做服务器验证功能 CSRFProtect(app) #设置session保存位置 Session(app) #添加自定义过滤器 from info.utils.common import do_index_class app.add_template_filter(do_index_class, "index_class") #同一处里404界面 from info.utils.common import user_login_data @app.errorhandler(404) @user_login_data def page_not_fond(e): data = {"user": g.user.to_dict() if g.user else None} return render_template('news/404.html', data=data) #当开启了csrf防护后,返回响应时添加一个tocken,用到after_request这个装饰器 @app.after_request def after_request(response): #生成scrf_token csrf_token = generate_csrf() response.set_cookie("csrf_token", csrf_token) return response # 注册index_blu蓝图到app from info.modules.index import index_blu app.register_blueprint(index_blu) from info.modules.passport import passport_blu app.register_blueprint(passport_blu) from info.modules.news import news_blu app.register_blueprint(news_blu) from info.modules.profile import profile_blu app.register_blueprint(profile_blu) from info.modules.admin import admin_blu app.register_blueprint(admin_blu, url_prefix='/admin') return app
def export_map(subtitle): logger.debug('Starting map export.') # Setup aprx aprx_path = set_path(config_dict.get('proj_dir'), 'WestNileOutbreak.aprx') aprx = arcpy.mp.ArcGISProject(aprx_path) lyt = aprx.listLayouts()[0] for el in lyt.listElements(): arcpy.AddMessage(el.name) if 'Title' in el.name: el.text = f'{el.text} {subtitle}' arcpy.AddMessage(el.text) aprx.save() lyt.exportToPDF(f'{config_dict["proj_dir"]}/wnv.pdf') logger.debug('Export map complete.')
def create_app(config_name): """通过传入不同的配置名,切换不同的环境""" app = Flask(__name__) config = config_dict.get(config_name) # 设置日志级别 log_file(config.LEVEL) app.config.from_object(Config) # app.config.from_object(Config)要 init_app之前 # SQLAlchemy对象关联app db.init_app(app) # 创建redis对象 global redis_store redis_store = redis.StrictRedis(host=config.REDIS_HOST, port=config.REDIS_PORT, decode_responses=True) # 初始化数据库 # 初始化redis 对象 # 开启csrf保护,只做服务器验证功能, # TODO CSRFProtect(app) # 设置session保存指定位置 Session(app) # 注册蓝图时,导入和注册写在一起 from info.modules.index import index_blu app.register_blueprint(index_blu) from info.modules.news import news_blu app.register_blueprint(news_blu) from info.modules.passport import passport_blu app.register_blueprint(passport_blu) from info.utils.common import do_index_class app.add_template_filter(do_index_class, "index_class") @app.after_request def after_request(response): # 调用生成csrf 的函数 csrf_token = generate_csrf() # 通过cookie传递给前台 response.set_cookie("csrf_token", csrf_token) return response return app
def create_app(config_type): app = Flask(__name__) # 根据配置类型取出配置类 config_class = config_dict.get(config_type) # 从对象中加载数据信息 app.config.from_object(config_class) # 创建数据库连接对象 db = SQLAlchemy(app) # 创建redis数据库连接对象 sr = StrictRedis(host=Config.REDIS_HOST, port=Config.REDIS_PORT) # 初始化Session存储对象 Session(app) # 设置数据迁移 mgr = Manager(app) Migrate(app, db) return app
def create_app(config_name): app = Flask(__name__) # 根据manager传入的配置名称,取出不同的配置类 Config = config_dict.get(config_name) # 调用日志 log(Config.LEVEL) # 将配置类信息加载到app app.config.from_object(Config) # 创建数据库,关联app db.init_app(app) # 创建redis对象 global redis_store redis_store = redis.StrictRedis(host=Config.REDIS_HOST, port=Config.REDIS_PORT, decode_responses=True) # 开启CSRF保护 CSRFProtect(app) # 开启Session Session(app) # 注册index蓝图 from info.modules.index import index_blue app.register_blueprint(index_blue) # 注册passport蓝图 from info.modules.passport import passport_blue app.register_blueprint(passport_blue) @app.after_request def after_request(resp): # 生成csrf_token csrf_token = generate_csrf() # 往cookie中设置csrf_token resp.set_cookie('csrf_token', csrf_token) return resp # 添加自定义过滤器 app.add_template_filter(to_clicks_class, 'to_clicks_class') return app
def create_app(config_type): app = Flask(__name__) # 根据配置类型取出配置类 config_class = config_dict.get(config_type) # 从对象加载配置信息 app.config.from_object(config_class) # 声明全局变量 global db, sr # 创建数据库连接对象 db = SQLAlchemy(app) # 创建redis 连接对象 # sr = StrictRedis(host=config_class.REDIS_HOST, port=config_class.REDIS_PORT) # 加上decode_responses=True,写入的键值对中的value为str类型,不加这个参数写入的则为字节类型。 sr = StrictRedis(host=config_class.REDIS_HOST, port=config_class.REDIS_PORT, decode_responses=True) # 初始化session存储对象 Session(app) # 初始化迁移器 Migrate(app, db) # 只需要使用一次的内容, 在使用前导入即可(需要时才导入), 这样可以有效避免循环导入 from info.modules.home import home_blue # 3. 注册蓝图 app.register_blueprint(home_blue) from info.modules.passport import passport_blue app.register_blueprint(passport_blue) # 配置日志 set_log() # 导入models 文件 生成数据库 import info.models # 添加过滤器 from info.utils.common import func_index_convert app.add_template_filter(func_index_convert, "index_convert") return app
def main(flush_output_db=False): """main orchestration Coordinates the major operations of the West Nile Outbreak project. No try except block included because these exist in helper functions called by this function. Args: flush_output_db: =False means contents of the output db will not be deleted. =True means contents of the output db will be deleted """ # Setup geoprocessing environment. # NAD 1983 StatePlane Colorado North: https://www.spatialreference.org/ref/esri/102653/ pcs = 102653 # Setup arcgis environment arcgis_setup(flush_output_db, spatial_reference=pcs) # Setup output db output_db = config_dict.get('output_gdb_dir') arcpy.AddMessage(f'output db: {output_db}') # ----- run_etl ----- # Run etl, generates the avoid_points feature class. run_etl() # ----- run_analysis ----- # Run Analysis to create the final analysis features. analysis_results_dictionary = run_analysis(output_db) # ----- render_layout ----- # Render the map including analysis features, correct colours, subtitle, and addresses at risk count. # analysis_results_dictionary below is included for debugging so don't have to run_analysis: # analysis_results_dictionary = {'map_subtitle': 'debug subtitle', 'addresses_at_risk_count': 123} map_features = [('final_analysis', [255, 0, 0, 100]), ('avoid_points_buf', [115, 178, 255, 100]), ('Target_Addresses', [102, 119, 205, 100])] map_subtitle = analysis_results_dictionary['map_subtitle'] map_spatial_reference = pcs address_count = analysis_results_dictionary['addresses_at_risk_count'] render_layout(map_subtitle, map_features, map_spatial_reference, address_count, output_db) # ----- generate_report ----- # Generate a csv report in the WestNileOutbreak directory with the Target Addresses that require spraying. target_addresses_fc = set_path(output_db, 'Target_Addresses') generate_target_addresses_csv(target_addresses_fc)
def create_app(config_name): """通过传入不同的配置名字,初始化其对应配置的应用实例""" # 通过config_name获取配置类 config = config_dict.get(config_name) #日志信息,传递日志级别 log_file(config.LEVEL) app = Flask(__name__) app.config.from_object(Config) # 创建SQLAlchemy对象,关联app # app.config.from_object(Config)先执行于db.init_app(app) db.init_app(app) # 创建redis对象 global redis_store redis_store = redis.StrictRedis(host=config.REDIS_HOST, port=config.REDIS_PORT, decode_responses=True) # 初始化数据库 # 初始化redis 对象 # 开启csrf保护,只做服务器验证功能, # TODO from flask_wtf import CSRFProtect CSRFProtect(app) # 设置session保存指定位置 Session(app) # 注册蓝图 from info.modules.index import index_blu app.register_blueprint(index_blu) from info.modules.passport import passport_blu app.register_blueprint(passport_blu) from info.utils.common import do_index_class app.add_template_filter(do_index_class, "index_class") from info.modules.news import news_blu app.register_blueprint(news_blu) @app.after_request def after_request(response): csrf_tonken = generate_csrf() response.set_cookie("csrf_token", csrf_tonken) return response return app
def create_app(config_name): # 创建应用程序对象 app = Flask(__name__) # 通过配置名,获取到配置类 config = config_dict.get(config_name) # 将配置信息加载到app中 app.config.from_object(config) # 创建日志记录文件 log_file(config.DEBUG_LEVEL) # 初始化session Session(app) # #初始化数据库,绑定数据库和应用程序app # db.init_app(app) # csrf配置 # CSRFProtect(app) # 初始化redis global redis_store redis_store = redis.StrictRedis(host=config.REDIS_HOST, port=config.REDIS_PORT) # 初始化mongodb global mongodb_store mongodb_store = MongoClient(host=config.MONGO_DB_HOST, port=config.MONGO_DB_PORT) # 添加自定义的转换器到转换器列表中 app.url_map.converters["re"] = RegexConverter # 注册蓝图 from istock.api_1_0 import api app.register_blueprint(api, url_prefix="/api/v1.0") # 注册静态文件蓝图 from istock.web_html import html app.register_blueprint(html) print(app.url_map) return app
def create_app(config_name): app = Flask(__name__) config = config_dict.get(config_name) app.config.from_object(config) db = SQLAlchemy(app, "db") db.init_app(app) global redis_store redis_store = redis.StrictRedis(host=config.REDIS_HOST, port=config.REDIS_PORT) Session(app) CSRFProtect(app) app.url_map.converters["re"] = RegexConverter from ihome.api_1_0 import api app.register_blueprint(api) from ihome.web_html import web_html app.register_blueprint(web_html) return app
def create_app(config_name): app = Flask(__name__) config = config_dict.get(config_name) log_file(config.LEVEL_NAME) app.config.from_object(config) # global db db.init_app(app) global redis_store, redis_login_image redis_store = StrictRedis(host=config.REDIS_HOST, port=config.REDIS_PORT, decode_responses=True) redis_login_image = StrictRedis(host=config.REDIS_HOST, port=config.REDIS_PORT, decode_responses=True) Session(app) CSRFProtect(app) from Info.modules.index import index_blue app.register_blueprint(index_blue) #将函数添加到默认的过滤器列表中 from Info.utils.commons import hot_news_first app.add_template_filter(hot_news_first, "my_filter") from Info.modules.passport import passport_blue app.register_blueprint(passport_blue) # app.add_template_filter(hot_news_filter,"my_filter" @app.after_request def after_request(resp): # 调用系统方法,获取csrf_token csrf_token = generate_csrf() # 将csrf_token设置到cookie中 resp.set_cookie("csrf_token", csrf_token) # 返回响应 return resp return app
def creat_app(config_name): # 通过传入不同的配置,初始化对应配置的实例 config = config_dict.get(config_name) # 设置日志级别 log_file(config.LEVEL) app = Flask(__name__) app.config.from_object(Config) # 创建SQLAlchemy,关联对象 db.init_app(app) # 初始化redis global redis_store redis_store = redis.StrictRedis(host=config.REDIS_HOST, port=config.REDIS_PORT, decode_responses=True) # 开启csrf保护,用于服务器验证 CSRFProtect(app) # 设置session保存指定位置 Session(app) # 注册蓝图时,导入和注册写在一起 from info.modules.index import index_blu app.register_blueprint(index_blu) from info.modules.passport import passport_blu app.register_blueprint(passport_blu) from info.utils.common import do_index_class app.add_template_filter(do_index_class, 'index_class') from info.modules.news import news_blu app.register_blueprint(news_blu) @app.after_request def after_request(response): # 生成csrf函数 csrf_token = generate_csrf() # 通过cookie传递给前台 response.set_cookie('csrf_token', csrf_token) return response return app
def create_app(config_name): """通过传入不同的配置名字,初始化其对应配置的应用实例""" app = Flask(__name__) #通过配置名获取到配置类 config = config_dict.get(config_name) # 将配置信息加载到app中 app.config.from_object(config) # 初始化数据库 db.init_app(app) # 设置redis global redis_store redis_store = redis.StrictRedis(host=config.REDIS_HOST, port=config.REDIS_PORT) # 创建日志记录文件 log_file(config.DEBUG_LEVEL) # 开启session Session(app) # 开启csrf保护 csrf.init_app(app) # 向app中添加自定义的路由转换器 app.url_map.converters['re'] = RegexConverter # 注册蓝图 import api_1_0 # 注意,蓝图在注册的时候在导入,否则可能会出现循环导入 app.register_blueprint( api_1_0.api, url_prefix='/api/v1.0' ) # url_prefix关键字参数(这个参数默认是/),通过/api/v1.0可以访问到蓝图中定义的视图函数 # 注册html静态文件蓝图 import web_html app.register_blueprint(web_html.html) # print app.url_map return app
def create_application(config_type): config_class = config_dict.get(config_type) # 创建app对象 app = Flask(__name__) # 从类对象a加载配置信息 app.config.from_object(config_class) global db, strict_redis # 创建数据库连接对象 db = SQLAlchemy(app) # 创建redis连接对象 strict_redis = StrictRedis(host=config_class.REDIS_HOST, port=config_class.REDIS_PORT, decode_responses=True) # session对象 Session(app) # 初始化migrate Migrate(app, db) # register blueprint from info.modules.home import home_bp app.register_blueprint(home_bp) # 注册passport对应蓝图 from info.modules.passport import passport_bp app.register_blueprint(passport_bp) # 注册news对应蓝图 from info.modules.news import news_bp app.register_blueprint(news_bp) # 配置日志 setup_log() # 导入过滤器 from info.utils.common import func_index_convert app.add_template_filter(func_index_convert, 'index_convert') # 导入创建表结构 import info.models return app