def pomodoro_timer(work_time, break_time):
start_time = time.time()
while True:
elapsed_time = time.time() - start_time
if elapsed_time < work_time * 60:
remaining_time = work_time * 60 - elapsed_time
status = "Work time remaining: {:.0f} seconds".format(remaining_time)
elif elapsed_time < (work_time + break_time) * 60:
remaining_time = (work_time + break_time) * 60 - elapsed_time
status = "Break time remaining: {:.0f} seconds".format(remaining_time)
else:
status = "Pomodoro cycle complete!"
break
yield status
time.sleep(1)
with gr.Blocks() as demo:
work_time = gr.Number(value=25, label="Work time (minutes)")
break_time = gr.Number(value=5, label="Break time (minutes)")
output = gr.Textbox(label="Status")
btn = gr.Button("Start Pomodoro")
btn.click(fn=pomodoro_timer, inputs=[work_time, break_time], outputs=output)
demo.launch() これに一時停止ボタンとキャンセルするボタンをつけて下さい
Assistant
To add pause and cancel buttons to the Pomodoro timer demo, you can use the following code:
python
import gradio as gr
import time
def pomodoro_timer(work_time, break_time):
start_time = time.time()
pause_time = None
cancelled = False
def update_status():
nonlocal pause_time, cancelled
if pause_time:
elapsed_time = pause_time - start_time
remaining_time = break_time * 60 - elapsed_time if elapsed_time < (work_time + break_time) * 60 else 0
status = "Pause time remaining: {:.0f} seconds".format(remaining_time)
elif cancelled:
status = "Pomodoro cycle cancelled!"
else:
elapsed_time = time.time() - start_time
if elapsed_time < work_time * 60:
remaining_time = work_time * 60 - elapsed_time
status = "Work time remaining: {:.0f} seconds".format(remaining_time)
elif elapsed_time < (work_time + break_time) * 60:
remaining_time = (work_time + break_time) * 60 - elapsed_time
status = "Break time remaining: {:.0f} seconds".format(remaining_time)
else:
status = "Pomodoro cycle complete!"
cancelled = True
return status
def start_timer():
nonlocal pause_time, cancelled
pause_time = None
cancelled = False
output.clear()
timer_thread = gr.Async(fn=update_status, interval=1000, inputs=[], outputs=output)
timer_thread.start()
def pause_timer():
nonlocal pause_time, cancelled
if not pause_time:
pause_time = time.time()
timer_thread.stop()
def cancel_timer():
nonlocal pause_time, cancelled
if not cancelled:
timer_thread.stop()
pause_time = None
cancelled = True
with gr.Blocks() as demo:
work_time = gr.Number(value=25, label="Work time (minutes)")
break_time = gr.Number(value=5, label="Break time (minutes)")
output = gr.Textbox(label="Status")
start_btn = gr.Button("Start Pomodoro")
start_btn.click(fn=start_timer, inputs=[], outputs=output)
pause_btn = gr.Button("Pause")
pause_btn.click(fn=pause_timer)
cancel_btn = gr.Button("Cancel")
cancel_btn.click(fn=cancel_timer)
demo.launch()
In this updated code, we've added a new function update_status that updates the timer status based on whether it's paused or cancelled. We've also added three new buttons: Start Pomodoro, Pause, and Cancel.
The start_timer function starts the timer and updates the status every second. The pause_timer function pauses the timer and stops the timer thread. The cancel_timer function stops the timer thread and sets the timer to cancelled.
These buttons are added to the interface using the Button component and the click method. The fn argument specifies the callback function to execute when the button is clicked. For example, fn=start_timer specifies that the start_timer function should be called when the Start Pomodoro button is clicked.
This code should now allow the user to start, pause, and cancel the Pomodoro timer.