如何使用FLASK python app.run()动态加载excel数据一次

时间:2017-06-27 15:01:54

标签: python flask

当我使用FLASK PYTHON调用app.run()

时,只动态加载excel文件一次,如何动态构建字典一次

每当我们对DATA SCIENCE模型进行评分时,我们需要传递包含训练模型的Excel数据的字典对象

def dictCall():

    model_root = "/app/RDD/Data/appdrain/"
    predictions_root = "/app/RDD/Data/appdrain/Predictions"
    model_stats_file = '/app/RDD/Data/appdrain/per_app_stats.tsv'
    models_dict = dict()

    with open(model_stats_file, 'r') as csvfile:
        model_stats_reader = csv.reader(csvfile, delimiter='\t')
        for row in model_stats_reader:
            make = row[0]
            model = row[1]
            appname=row[2]
            appversion=row[4]
            threshold=row[5]
            if row[5] == 'NULL':
                continue

            percentiles = [float(i) for i in row[8][1:-1].split(",")]
            app_drain_model = AppDrainModel(make, model, appname, appversion, threshold , percentiles)
            keys=make+model+appname+appversion
            models_dict[keys] = app_drain_model
    return models_dict
    @app.route('/MachineLearning/AppDrain/score', methods=['GET', 'POST'])
     modeldict=dictCall()
     MODELRESULT2=AppDrainScoring.appcall(Make, Model, appName, appVer, 
     meandr,modeldict)

     app.wsgi_app = ProxyFix(app.wsgi_app)
     if __name__ == '__main__':
        app.run()

1 个答案:

答案 0 :(得分:0)

您是否尝试过将变量models_dict设为全局:

models_dict = dict()
def dictCall():
    global models_dict 

    model_root = "/app/RDD/Data/appdrain/"
    predictions_root = "/app/RDD/Data/appdrain/Predictions"
    model_stats_file = '/app/RDD/Data/appdrain/per_app_stats.tsv'

    if len(models_dict) == 0:

        with open(model_stats_file, 'r') as csvfile:
            model_stats_reader = csv.reader(csvfile, delimiter='\t')
            for row in model_stats_reader:
                make = row[0]
                model = row[1]
                appname=row[2]
                appversion=row[4]
                threshold=row[5]
                if row[5] == 'NULL':
                    continue

                percentiles = [float(i) for i in row[8][1:-1].split(",")]
                app_drain_model = AppDrainModel(make, model, appname, appversion, threshold , percentiles)
                keys=make+model+appname+appversion
                models_dict[keys] = app_drain_model
    return models_dict

    @app.route('/MachineLearning/AppDrain/score', methods=['GET', 'POST'])
     modeldict=dictCall()
     MODELRESULT2=AppDrainScoring.appcall(Make, Model, appName, appVer, 
     meandr,modeldict)

     app.wsgi_app = ProxyFix(app.wsgi_app)
     if __name__ == '__main__':
        app.run()