Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,12 +4,16 @@ import threading
|
|
| 4 |
|
| 5 |
app = Flask(__name__)
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
for remaining in range(seconds, 0, -1):
|
| 10 |
time.sleep(1) # Wait for 1 second
|
| 11 |
-
|
| 12 |
-
|
| 13 |
|
| 14 |
@app.route('/')
|
| 15 |
def index():
|
|
@@ -17,16 +21,20 @@ def index():
|
|
| 17 |
|
| 18 |
@app.route('/start_timer', methods=['POST'])
|
| 19 |
def start_timer_route():
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
if __name__ == '__main__':
|
| 32 |
-
app.run(debug=True)
|
|
|
|
| 4 |
|
| 5 |
app = Flask(__name__)
|
| 6 |
|
| 7 |
+
# Global variable to store the message
|
| 8 |
+
message = ""
|
| 9 |
+
|
| 10 |
+
# Timer function to run in a background thread
|
| 11 |
+
def start_timer(seconds):
|
| 12 |
+
global message
|
| 13 |
for remaining in range(seconds, 0, -1):
|
| 14 |
time.sleep(1) # Wait for 1 second
|
| 15 |
+
message = f"{remaining} seconds remaining..."
|
| 16 |
+
message = "Time's up!"
|
| 17 |
|
| 18 |
@app.route('/')
|
| 19 |
def index():
|
|
|
|
| 21 |
|
| 22 |
@app.route('/start_timer', methods=['POST'])
|
| 23 |
def start_timer_route():
|
| 24 |
+
try:
|
| 25 |
+
seconds = int(request.form['seconds'])
|
| 26 |
+
|
| 27 |
+
# Start the timer in a new thread
|
| 28 |
+
timer_thread = threading.Thread(target=start_timer, args=(seconds,))
|
| 29 |
+
timer_thread.start()
|
| 30 |
+
|
| 31 |
+
return jsonify({"message": "Timer started"})
|
| 32 |
+
except Exception as e:
|
| 33 |
+
return jsonify({"error": str(e)}), 400
|
| 34 |
+
|
| 35 |
+
@app.route('/get_timer', methods=['GET'])
|
| 36 |
+
def get_timer():
|
| 37 |
+
return jsonify({"message": message})
|
| 38 |
|
| 39 |
if __name__ == '__main__':
|
| 40 |
+
app.run(debug=True, threaded=True)
|