Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,26 +1,32 @@
|
|
| 1 |
-
import
|
| 2 |
import time
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
# Timer function
|
| 5 |
-
def start_timer(seconds):
|
| 6 |
-
# Countdown loop
|
| 7 |
for remaining in range(seconds, 0, -1):
|
| 8 |
time.sleep(1) # Wait for 1 second
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
start_button = gr.Button("Start Timer") # Button to start timer
|
| 19 |
-
|
| 20 |
-
timer_output = gr.Textbox(label="Timer Output", interactive=False)
|
| 21 |
|
| 22 |
-
|
| 23 |
-
start_button.click(start_timer, inputs=time_input, outputs=timer_output)
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
|
|
|
| 1 |
+
from flask import Flask, render_template, request, jsonify
|
| 2 |
import time
|
| 3 |
+
import threading
|
| 4 |
+
|
| 5 |
+
app = Flask(__name__)
|
| 6 |
|
| 7 |
# Timer function
|
| 8 |
+
def start_timer(seconds, callback):
|
|
|
|
| 9 |
for remaining in range(seconds, 0, -1):
|
| 10 |
time.sleep(1) # Wait for 1 second
|
| 11 |
+
callback(f"{remaining} seconds remaining...")
|
| 12 |
+
callback("Time's up!")
|
| 13 |
+
|
| 14 |
+
@app.route('/')
|
| 15 |
+
def index():
|
| 16 |
+
return render_template('index.html')
|
| 17 |
|
| 18 |
+
@app.route('/start_timer', methods=['POST'])
|
| 19 |
+
def start_timer_route():
|
| 20 |
+
seconds = int(request.form['seconds'])
|
| 21 |
+
|
| 22 |
+
# Callback function to send updates back to the frontend
|
| 23 |
+
def callback(message):
|
| 24 |
+
app.jinja_env.globals['message'] = message # Update the message globally
|
| 25 |
|
| 26 |
+
# Start timer in a separate thread to avoid blocking
|
| 27 |
+
threading.Thread(target=start_timer, args=(seconds, callback)).start()
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
return jsonify({"message": "Timer started"})
|
|
|
|
| 30 |
|
| 31 |
+
if __name__ == '__main__':
|
| 32 |
+
app.run(debug=True)
|