-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
101 lines (78 loc) · 2.08 KB
/
main.py
File metadata and controls
101 lines (78 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
"""`main` is the top level module for your Flask application."""
# Import the Flask Framework
from flask import Flask
app = Flask(__name__)
# Note: We don't need to call run() since our application is embedded within
# the App Engine WSGI application server.
from httphandler import get_htmltemplate
timerHtml = """<div id="sec" style="font-size:128px">0.00</div>
<div id="buttons">
<input type="button" value="Start" id="run">
<input type="button" value="Stop" id="stop">
<input type="button" value="Reset" id="reset">
</div>
"""
script = """
(function(){
window.onload = function(){
var startTime;
var stopTime;
var timerId;
var running = false;
document.getElementById('run').onclick = function(){
run();
return true;
}
document.getElementById('stop').onclick = function(){
stop();
return true;
}
document.getElementById('reset').onclick = function(){
reset();
return true;
}
var run = function(){
if(running) return;
running = true;
if(stopTime){
startTime = startTime + (new Date()).getTime() - stopTime;
}
if(!startTime){
startTime = (new Date()).getTime();
}
timer();
}
// app print: startTime - current time
function timer(){
document.getElementById('sec').innerHTML = (((new Date()).getTime() - startTime)/1000).toFixed(2);
timerId = setTimeout(function(){
timer();
}, 100);
}
function stop(){
if(!running) return false;
running = false;
clearTimeout(timerId);
stopTime = (new Date()).getTime();
}
function reset(){
if(running) return;
startTime = undefined;
document.getElementById('sec').innerHTML = "0:00";
}
}
})();
"""
@app.route('/')
def hello():
"""Return a friendly HTTP greeting."""
body = get_htmltemplate()
return body.format(title="Simple Stopwatch", script=script, body=timerHtml)
@app.errorhandler(404)
def page_not_found(e):
"""Return a custom 404 error."""
return 'Sorry, Nothing at this URL.', 404
@app.errorhandler(500)
def page_not_found(e):
"""Return a custom 500 error."""
return 'Sorry, unexpected error: {}'.format(e), 500