0
私はフラスコを使い始めました。フラスコのルートを動的に定義してデータをインポートする
私は成功したルートの一握りを作成するためにフラスコを使用することができましたが、私は動的に生成しようとすると、私はアサーションエラーになっておいてください。 「はAssertionError:ビュー機能マッピングは、既存のエンドポイント機能を上書きされます。j_show_html」
これを回避するために、私は動的にPython関数を作成しようとしました(これは悪い考えです)。これらの日付ベースのページを動的に作成するためのより良い方法は何ですか?ここで
は私のPythonスクリプトです:
from flask import render_template, Flask
import pandas
from pandas.tseries.holiday import USFederalHolidayCalendar
from datetime import timedelta, datetime
app = Flask(__name__)
out_IP_address = "0.0.0.0"
out_port = 5000
fileLoc = "C:/"
fileName = "Rand_QA_Calls"
start_date = "2017-01-01"
out_date = []
cal = USFederalHolidayCalendar()
holidays = cal.holidays(start='2017-01-01', end='2017-12-31').to_pydatetime()
for i in range(0,365):
temp_date = datetime.strptime(start_date,"%Y-%m-%d") + timedelta(days=i)
code = """
def {0}():
report = pandas.read_excel('{1}'+"/"+'{2}'+"_"+'{3}'+"_"+'{3}'+".xlsx")
return render_template('view.html',
tables=[report.to_html(index=False)])""".format("j_show_html_"+str(i),fileLoc,fileName,temp_date.strftime("%Y%m%d"))
print(code)
@app.route("/"+temp_date.strftime("%Y%m%d"))
exec(eval(code))
if __name__ == "__main__":
app.run(host=out_IP_address,port=out_port,debug=True)
そして、ここでは私のHTMLは(Jinja2のを利用)である:
<!doctype html>
<title>Simple tables</title>
<link rel=stylesheet type=text/css href="{{ url_for('static', filename='style.css') }}">
<div class=page>
<h1>Rand_QA_Calls</h1>
{% for table in tables %}
<h2>{{titles[loop.index]}}</h2>
{{ table|safe }}
{% endfor %}
</div>
そして最後に、ここでは、CSSです:
body { font-family: "Lucida Sans Unicode", "Lucida Grande", sans-serif;}
a, h1, h2 { color: #377ba8; }
h1, h2 { margin: 0; }
h1 { border-bottom: 2px solid #eee; }
h2 { font-size: 1.2em; }
table.dataframe, .dataframe th, .dataframe td {
border: none;
border-bottom: 1px solid #C8C8C8;
border-collapse: collapse;
text-align:left;
padding: 10px;
margin-bottom: 40px;
font-size: 0.9em;
}
tr:nth-child(odd) { background-color:#eee; }
tr:nth-child(even) { background-color:#fff; }
tr:hover { background-color: #ffff99;}