2017-02-16 2 views
0

플라스크를 시작하고 솔직히 학습의 책상 부분에 맞서고 있습니다. 플라스크 경로를 동적으로 정의하고 데이터 가져 오기

나는 성공적으로 경로의 소수를 만들 플라스크를 사용 할 수 있었다, 그러나 나는 그들을 생성 할 때 동적으로 나는 어설 션 오류가 계속 : 는 "j_show_html을 AssertionError를 :보기 기능 매핑 기존 엔드 포인트 기능을 덮어 쓰기한다"

이 문제를 해결하기 위해 필자는 파이썬 기능을 동적으로 만들려고 노력했습니다 (나쁜 생각 인 것 같습니다). 이러한 날짜 기반 페이지를 동적으로 만드는 더 좋은 방법은 무엇입니까? 내 HTML은 (사용 jinja2) 여기

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) 

: 그리고 마침내 여기

<!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;} 

답변

0

내가 생각 여기

내 파이썬 스크립트입니다 그것. 나는 경로를 통해 변수를 전달하지 않고 있지만 훨씬 쉬워졌습니다.

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) 
관련 문제