Я пытаюсь создать приложение, которое загружает файл csv / excel, выполняет аудит с помощью кода, приведенного ниже, для загруженного файла и выдает еще один проверенный файл excel / csv, но я не уверен, как добавить эта функция в Flask.
Пока что это мой код для самого приложения:
from openpyxl import load_workbook
from flask import Flask, request, render_template, redirect, url_for
app = Flask(__name__)
@app.route('/')
def index():
return """<title>Upload excel or csv file you would like to audit</title>
<h1>Upload excel or csv file you would like to audit</h1>
<form action="/uploader" method=post enctype=multipart/form-data>
<p><input type=file name=file>
<input type=submit value=Upload>
</form>"""
@app.route('/uploader', methods = ['GET', 'POST'])
def upload():
if request.method == 'POST':
f = request.files['file']
f.save(f.filename)
return process(f.filename)
if __name__ == '__main__':
app.run()
Я хочу, чтобы загруженный файл excel / csv обрабатывал код ниже:
import pandas as pd
#read file
df = pd.read_csv(input("Copy path of file you want to audit: "))
#check for certain condition (Y)
df2 = df.loc[(df['Track Item']=='Y')]
print(len(df2))
#function that determines to sample the category or subcategory
def simple_sample(df2):
if df['Subcategory'].isnull().all():
col_name = 'Subcategory'
else:
col_name = 'Category'
def sub_sample(df2, col_name, frac):
return df.groupby(col_name).apply(lambda x: x.sample(n=2) if x.size*frac < 2 else
x.sample(frac=frac))
if df2.shape[0] >= 15000:
return sub_sample(df, col_name, 0.05)
elif df2.shape[0] >= 10000:
return sub_sample(df, col_name, 0.03)
if df2.shape[0] >= 1500:
return sub_sample(df, col_name, 0.01)
else:
return None
#results of function simple_sample
final = simple_sample(df2)
#compares original dataframe to the varaible final and marks items thst need to be audited as "Audit"
df.loc[df['Retailer Item ID'].isin(final['Retailer Item ID']), 'Track Item'] =='Audit'
#converts results to a csv file
df.to_csv('Test.csv',index=False)
Может ли кто-нибудь помочь мне объединить эти два фрагмента кода для создания приложения?