我是flask / python的新手。我正在为我们的客户建立一个门户,他们可以登录并上传其财务文件。有一个预定义的架构,可为客户映射财务项目。例如,所有与“现金”相关的帐户都将被简单地映射为“现金”。
如果上载的文档(test.xlsx)中没有新项目,则门户将输出一个表,其中显示了映射的项目。容易。
如果在财务文档(test_modify.xlsx)中添加了新帐户,我需要客户端手动定义该项目的架构。
在这种情况下,“幸运的钱”:现金,“坏债”:债务。
添加新架构后,将再次处理数据并输出新表。
下载测试数据的链接:https://wetransfer.com/downloads/fd9ee239593d8539fea2ba29e0a304a120190725150028/87bd98 -test.xlsx仅具有预定义的帐户 -test_modify.xlsx有一些新帐户
import pandas as pd
from flask import Flask, request, jsonify, render_template, send_file, make_response
from matplotlib import pyplot as plt
app = Flask(__name__)
@app.route("/")
def home():
return render_template("homepage.html")
@app.route("/file", methods=['POST'])
def process():
test_bs = pd.read_excel(request.files.get('test_bs'))
# Defining schemas for the accounts
Cash = ['Petty cash','RBC USD']
Debt = ['Visa Card 7123 - Warren','Paul CDN Visa','Warren CDN Visa']
Retained_Earnings = ['Retained earnings','Profit for the year']
chart_dict = {'Cash': Cash,'Debt':Debt,'Retained Earnings': Retained_Earnings}
# Mapping items
test_bs['mapping'] =None
for item in test_bs['Account Description']:
for value in list(chart_dict.keys()):
if item in chart_dict.get(value):
test_bs['mapping'][test_bs.index[test_bs['Account Description'] == item]] = value
result_df = test_bs.to_html(classes='data')
new_account = list(test_bs['Account Description'][test_bs['mapping'].isnull()])
# Check if there are any new accounts in the financial statement
if new_account != None:
new_dict ={}
for item in new_account:
# Client defining schema
entry = input(str(item)+": ")
###
new_dict[entry] = item
for i in new_dict:
for u in chart_dict:
if i==u:
chart_dict.get(u).append(new_dict.get(i))
test_bs['mapping'] =None
for item in test_bs['Account Description']:
for value in list(chart_dict.keys()):
if item in chart_dict.get(value):
test_bs['mapping'][test_bs.index[test_bs['Account Description'] == item]] = value
result_df = test_bs.to_html(classes='data')
return result_df
else:
return result_df
if __name__ == '__main__':
port = 5000
app.run(port=port,debug=True)
<!DOCTYPE html>
<html>
<head>
<title>Register</title>
</head>
<style>
body {
background-color: #202322;
color: white
}
</style>
<body>
<a href="/">
<h4>Homepage</h4>
</a>
<h1>Financials</h1>
<form method="POST" enctype=multipart/form-data action="file">
<label for="scoring" class="btn btn-primary btn-block btn-outlined">test_bs</label>
<input type=file name=test_bs id="test_bs">
<button id="submit-button">Process data</button>
</form>
</body>
</html>
我的问题是input()函数(行:entry = input(str(item)+“:”))在服务器端而不是浏览器上提示数据输入操作。有人可以告诉我如何做到这一点吗?
P / s:如果有人注意到明显的方法可以更好地编写我的代码,请随时提出建议。谢谢