Python 动态定义烧瓶路径&;导入数据

Python 动态定义烧瓶路径&;导入数据,python,dynamic,flask,routes,Python,Dynamic,Flask,Routes,我刚开始学习Flask,老实说,我已经达到了学习过程中与桌面的碰撞 我能够成功地使用flask创建少量路由,但当我尝试动态生成它们时,我不断遇到断言错误: “AssertionError:视图函数映射正在覆盖现有端点函数:j_show_html” 为了解决这个问题,我尝试动态创建python函数(这似乎是个坏主意)。动态创建这些基于日期的页面的更好方法是什么 以下是我的python脚本: from flask import render_template, Flask import pandas

我刚开始学习Flask,老实说,我已经达到了学习过程中与桌面的碰撞

我能够成功地使用flask创建少量路由,但当我尝试动态生成它们时,我不断遇到断言错误: “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):


我想出来了。我没有在路由中传递变量,但这使它更容易

from flask import render_template, Flask
import pandas
from pandas.tseries.holiday import USFederalHolidayCalendar
#from datetime import datetime, timedelta 
import urllib2

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()

@app.route("/tables/")
@app.route("/tables/<date>")
def j_show_html(date):
    report = pandas.read_excel(fileLoc+"/"+fileName+"_"+date+"_"+date+".xlsx")
    return render_template('view.html',
    tables=[report.to_html(index=False)],
    titles = ['na'],
    labels = urllib2.unquote(date.encode('ascii','ignore')))

if __name__ == "__main__":
    app.run(host=out_IP_address,port=out_port,debug=True)
从flask导入渲染模板,flask
进口大熊猫
从pandas.tseries.holiday导入USFederalHolidayCalendar
#从datetime导入datetime,timedelta
导入urllib2
app=烧瓶(名称)
out\u IP\u address=“0.0.0.0”
输出端口=5000
fileLoc=“C:/”
fileName=“Rand_QA_调用”
开始日期=“2017-01-01”
过期日期=[]
cal=USFederalHolidayCalendar()
假日=法定假日(开始时间为2017-01-01',结束时间为2017-12-31')。至
@应用程序路径(“/tables/”)
@应用程序路径(“/tables/”)
def j_show_html(日期):
report=pandas.read_excel(fileLoc+“/”+fileName+““+date+““+date+”.xlsx))
返回渲染模板('view.html',
表=[report.to_html(index=False)],
标题=['na'],
labels=urlib2.unquote(date.encode('ascii','ignore'))
如果名称=“\uuuuu main\uuuuuuuu”:
运行(主机=out\u IP\u地址,端口=out\u端口,调试=True)
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;}
from flask import render_template, Flask
import pandas
from pandas.tseries.holiday import USFederalHolidayCalendar
#from datetime import datetime, timedelta 
import urllib2

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()

@app.route("/tables/")
@app.route("/tables/<date>")
def j_show_html(date):
    report = pandas.read_excel(fileLoc+"/"+fileName+"_"+date+"_"+date+".xlsx")
    return render_template('view.html',
    tables=[report.to_html(index=False)],
    titles = ['na'],
    labels = urllib2.unquote(date.encode('ascii','ignore')))

if __name__ == "__main__":
    app.run(host=out_IP_address,port=out_port,debug=True)