2017-02-03 106 views
-2

********************* ********************* *********************我試圖讓使用燒瓶的公告板,代碼工作完美之前但突然停止工作。請help.I沒有與燒瓶太多的經驗,我也試着聲明變量全球flask UnboundLocalError:分配前引用的局部變量'shift1'


的Python(瓶)代碼:

from flask import Flask, render_template 
import datetime 
import MySQLdb 

app = Flask(__name__) 

@app.route('/shutdown', methods=['POST']) 
def shutdown(): 
    shutdown_server() 
    return 'Server shutting down...' 




@app.route('/') 
def index(): 

    now = datetime.datetime.now() 
    timestamp = datetime.datetime.now().time() 

    if datetime.time(7, 30) <= timestamp <= datetime.time(15, 29): 
    shift1 = "A" 
    elif datetime.time(15, 30) <= timestamp <= datetime.time(23, 29): 
    shift1 = "B" 
    elif datetime.time(23, 30) <= timestamp <= datetime.time(7, 29): 
    shift1 = "C" 
    return render_template('hello.html', month=now.month, date=now.day, year=now.year, hour=now.hour, minute=now.minute, second=now.second, shift=shift1) 

if __name__ == '__main__': 
    app.run(host= '0.0.0.0', debug=True) 

HTML代碼:

<html> 
<head> 
<script> 
setInterval(function() { 
        window.location.reload(); 
       }, 1000); 

</script> 

<style> 
table, td, tr { 
    text-align: center; 
    height: 72px; 
    font-size: 53; 
} 


</style> 
</head> 

<body> 

<table border="1" width="100%" height="941px"> 
<tr> 
<td colspan="4"><p align="centre"><font color="blue">{{ date }}/{{ month }}/{{ year }}</font></p></td> 
<td colspan="4"><p align="centre"><font color="blue">{{ hour }} : {{ minute }} : {{ second }}</font></p></td> 
</tr> 
<tr> 
<td colspan="5"></td> 
<td colspan="2" ><font>Shift:</td> 
<td colspan="1" ><font color="red">{{ shift }}</td> 
</tr> 

<tr> 
<td colspan="2" width="20%"><font size="28" color="green">Plan</td> 
<td width="10%">1</td> 
<td></td> 
<td rowspan="8" colspan="4"></td> 
</tr> 
<tr> 
<td colspan="2" rowspan="3"></td> 
<td width="10%">2</td> 
<td></td> 
</tr> 
<tr> 
<td width="10%">3</td> 
<td></td> 

</tr> 

<tr> 
<td width="10%">4</td> 
<td></td> 

</tr> 
<tr> 
<td colspan="2"height="50px" width="20%"><font size="28" color="green">Actual</td> 
<td width="10%">5</td> 
<td></td> 

</tr> 
<td colspan="2" rowspan="3"></td> 
<td width="10%">6</td> 
<td></td> 
</tr> 
<tr> 
<td width="10%">7</td> 
<td></td> 

</tr> 

<tr> 
<td width="10%">8</td> 
<td></td> 

</tr> 
</table> 
</body> 
</html> 

錯誤:

click here to see the screenshot

+0

請包括在問題作爲代碼塊的所有錯誤 –

回答

1

你的時間比較是有缺陷的;您正在無視一個time()對象的秒部分:

>>> import datetime 
>>> datetime.time(15, 29, 30) <= datetime.time(15, 29) 
False 
>>> datetime.time(15, 29, 30) >= datetime.time(15, 30) 
False 

秒部分默認爲0,所以15:29:3015:29:00。這意味着有時間戳不會導致任何if .. elif分支匹配,並且shift1從不設置。

使用<測試代替,並用更好的上邊界:

if datetime.time(7, 30) < timestamp < datetime.time(15, 30): 
    shift1 = "A" 
elif datetime.time(15, 30) < timestamp < datetime.time(23, 30): 
    shift1 = "B" 
elif datetime.time(23, 30) < timestamp < datetime.time(7, 30): 
    shift1 = "C" 

對於轉移的數量較多,我會使用二分法;設置的開始時間在一個序列,然後使用所得到的插入點的當前時間作爲一個指數到移位的序列:

from bisect import bisect 

shift_starts = (datetime.time(7, 30), datetime.time(15, 30), datetime.time(23, 30)) 
shift_names = 'CABC' # C twice to account for the time before 7:30 
shift_index = bisect(shift_starts, timestamp) 
shift1 = shift_names[shift_index] 
相關問題