def upload_file(id): if 0 == id: return jsonify(errcode=-1, message='先保存配置再上传文件') if 'file' not in request.files: flash('No file part') return jsonify(errcode=-1, message='No file part') file = request.files['file'] if file.filename == '': flash('No selected file') return jsonify(errcode=-1, message='No selected file') if file and allowed_file(file.filename): from werkzeug.utils import secure_filename filename = file.filename #secure_filename(file.filename) filename = os.path.join(glb.user_report_upload_path(id, created=True), filename) file.save(filename) fsize = os.path.getsize(filename) if (fsize > 10 * 1024 * 1024): os.remove(filename) return jsonify(errcode=-1, message="上传失败,文件大小超过500K") return jsonify(errcode=0, message="上传成功") else: return jsonify(errcode=-1, message="没有该文件,或文件名后缀不正确。应该为:" + str(glb.config['ALLOWED_EXTENSIONS']))
def initDatafrom(): data_from = request.json['data_from'] report_id = request.json['curr_report_id'] upload_path = glb.user_report_upload_path(report_id) if data_from['type'] == "file": filename = os.path.join(upload_path, data_from['url']) ret = ce.load_from_file(filename, []) data_from = {**data_from, **ret['修改这里']['data_from']} elif data_from['url'].startswith("结果://"): ret = ce.get_excel_data(data_from, report_id, upload_path, ds_dict={}, ret={}) else: ret = asyncio.run( ce.load_from_url2(data_from, {}, 0, session['userid'])) ds_dict = {} for k, v in ret.items(): if isinstance(v['data'], pd.DataFrame): ds_dict[k] = v['data'].to_json(orient='split', force_ascii=False) # return { 'data_from': data_from, "ds_dict": ds_dict, 'df_arr': [x for x in ds_dict] }
def raw_get(id, rawtype, ds_names): glb.redis.hincrby("zb:tj", 'api') ds_names = ds_names.split(',') cur = None with glb.db_connect() as conn: with conn.cursor(as_dict=True) as cursor: cursor.execute('SELECT * FROM zhanbao_tbl WHERE id=%s', id) config_data = json.loads(cursor.fetchone()['config_txt']) #for one_data_from in json.loads(cur_row['config_txt'])['data_from'] for one on one_data_from['ds'] is one['name'] in ds_names user_input_form_data = {**request.args, **request.form} # ds_dict= ce.load_all_data(config_data,id,args=args,upload_path=glb.user_report_upload_path(id),userid=glb.ini['user_login']['test_user']) async_result = hnclic.tasks.load_all_data.delay( config_data, id, user_input_form_data=user_input_form_data, upload_path=glb.user_report_upload_path(id), userid=glb.ini['user_login']['test_user']) while not async_result.ready(): asyncio.run(asyncio.sleep(0.1)) if async_result.status == 'FAILURE': raise RuntimeError(async_result.traceback.replace('\n', '<br>\n')) df_arr, ds_dict, ret_config_data = async_result.result if isinstance(df_arr, str): raise RuntimeError(df_arr) ret_dict = dict( {key: value for key, value in ds_dict.items() if key in ds_names}) rawtype = rawtype.split(":") ret_str = '{"errcode":0,"message":"success",' if rawtype[0] == 'json': '''json的格式如下 split ,样式为 {index -> [index], columns -> [columns], data -> [values]} records ,样式为[{column -> value}, … , {column -> value}] index ,样式为 {index -> {column -> value}} columns ,样式为 {index -> {column -> value}} values ,数组样式 table ,样式为{‘schema': {schema}, ‘data': {data}},和records类似 ''' json_type = 'records' if len(rawtype) == 2: if rawtype[1] in "split,records,index,columns,values,table": json_type = rawtype[1] else: return '{"errcode":1,"message":"json选择的类型不正确,应该是下面之一: split,records,index,columns,values,table"}' for key, value in ret_dict.items(): ret = json.loads(value) ret_str = ret_str + '\n"' + key + '":' + pd.DataFrame( ret["data"], columns=ret["columns"]).to_json( orient=json_type, force_ascii=False) + ',' ret_str = ret_str[:-1] + '}' return ret_str
def remove_file(id, filename): file_path = glb.user_report_upload_path(id) file = os.path.join(file_path, filename) if os.path.realpath(file).startswith(file_path): if os.path.exists(file): os.remove(file) return {'errcode': 0, 'message': '删除成功'} else: return {'errcode': 0, 'message': '文件不存在'} else: return {'errcode': 1, 'message': '非法删除'}
def ReportDefine_get(id): my_path = glb.user_report_upload_path(id) my_file_list = [{ 'name': x, 'url': f'/mg/file/download/{id}/{x}' } for x in (os.listdir(my_path) if os.path.exists(my_path) else [])] with glb.db_connect() as conn: with conn.cursor(as_dict=True) as cursor: cursor.execute('SELECT * FROM zhanbao_tbl WHERE id=%d', id) ret = cursor.fetchone() ret['fileList'] = my_file_list return json.dumps(ret, ensure_ascii=False)
def initDatafrom(data_from,report_id,userid): try: upload_path=glb.user_report_upload_path(report_id ) if data_from['type']=="file": filename=os.path.join(upload_path, data_from['url']) ret=ce.load_from_file(filename,[]) data_from={**data_from,**ret['修改这里']['data_from']} elif data_from['url'].startswith("结果://"): ret=ce.get_excel_data(data_from,report_id,upload_path,ds_dict={},ret={}) else: ret= asyncio.run( ce.load_from_url2(data_from,{},0,userid) ) ds_dict={} for k,v in ret.items(): if isinstance(v['data'],pd.DataFrame): ds_dict[k]=v['data'].to_json(orient='split',force_ascii=False) # return [ x for x in ds_dict ],ds_dict,data_from except Exception as e: return str(e.args),None,data_from
async def load_one_data(config_data, curr_report_id): glb.redis.sadd("zb:executing", curr_report_id) try: ds_dict = await ce.load_all_data( config_data, curr_report_id, upload_path=glb.user_report_upload_path(curr_report_id), userid=session['userid']) #ds_dict=ce.load_all_data(config_data,curr_report_id,upload_path=glb.user_report_upload_path(curr_report_id),userid=session['userid']) df_arr = [] ret = {} for k, v in ds_dict.items(): if isinstance(v, pd.DataFrame): ret[k] = v.to_json(orient='split', force_ascii=False) # df_arr.append(k) elif k[:2] != '__': ret[k] = v return df_arr, ret finally: glb.redis.srem("zb:executing", curr_report_id)
async def load_one_data(config_data, curr_report_id): glb.redis.sadd("zb:executing", curr_report_id) try: async_result = hnclic.tasks.load_all_data.delay( config_data, curr_report_id, upload_path=glb.user_report_upload_path(curr_report_id), userid=session['userid']) while not async_result.ready(): await asyncio.sleep(0.1) if async_result.status == 'FAILURE': raise RuntimeError( async_result.traceback.replace('\n', '<br>\n')) df_arr, ds_dict, ret_config_data = async_result.result config_data.update(ret_config_data) if isinstance(df_arr, str): raise RuntimeError(df_arr) return df_arr, ds_dict, ret_config_data finally: glb.redis.srem("zb:executing", curr_report_id)
def download_file(id, filename): return send_from_directory(glb.user_report_upload_path(id), filename, as_attachment=True)