0
我開始使用瓶子,老實說我已經達到了對桌子部分學習的爆炸頭。動態定義瓶頸路徑和導入數據
我是能夠成功地使用燒瓶創建路線了一把,但是當我嘗試生成這些動態總是收到一個斷言錯誤: 「Asse田:查看功能映射覆蓋現有的端點功能: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;}