Ivan000 commited on
Commit
5de7e17
·
verified ·
1 Parent(s): 2191b78

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -0
app.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
+ # =============
3
+ # This is a complete app.py file for a countdown timer to New Year 2025 in multiple time zones.
4
+
5
+ import gradio as gr
6
+ from datetime import datetime, timedelta
7
+ import pytz
8
+
9
+ # Define a function to calculate the time remaining until New Year 2025 in the specified time zone
10
+ def countdown_to_new_year(timezone: str):
11
+ try:
12
+ # Get the current time in the specified time zone
13
+ tz = pytz.timezone(timezone)
14
+ now = datetime.now(tz)
15
+
16
+ # Define the target date and time for New Year 2025
17
+ new_year_2025 = tz.localize(datetime(2025, 1, 1, 0, 0, 0))
18
+
19
+ # Calculate the time remaining
20
+ if now >= new_year_2025:
21
+ return "Happy New Year 2025!"
22
+ time_remaining = new_year_2025 - now
23
+
24
+ # Extract days, hours, minutes, and seconds
25
+ days = time_remaining.days
26
+ seconds = time_remaining.seconds
27
+ hours = seconds // 3600
28
+ minutes = (seconds % 3600) // 60
29
+ seconds = seconds % 60
30
+
31
+ return f"{days} days, {hours} hours, {minutes} minutes, {seconds} seconds remaining"
32
+ except Exception as e:
33
+ return f"Error: {str(e)}"
34
+
35
+ # Create a list of common time zones for the user to choose from
36
+ TIME_ZONES = pytz.all_timezones
37
+
38
+ # Define the Gradio interface
39
+ def build_interface():
40
+ with gr.Blocks() as app:
41
+ gr.Markdown("""
42
+ # Countdown to New Year 2025
43
+ Select a time zone to see how much time remains until New Year 2025!
44
+ """)
45
+
46
+ timezone_selector = gr.Dropdown(
47
+ label="Select Time Zone",
48
+ choices=TIME_ZONES,
49
+ value="UTC"
50
+ )
51
+
52
+ countdown_display = gr.Textbox(label="Time Remaining", interactive=False)
53
+
54
+ calculate_button = gr.Button("Calculate Countdown")
55
+
56
+ calculate_button.click(
57
+ fn=countdown_to_new_year,
58
+ inputs=[timezone_selector],
59
+ outputs=[countdown_display]
60
+ )
61
+
62
+ return app
63
+
64
+ # Build and launch the app
65
+ if __name__ == "__main__":
66
+ app = build_interface()
67
+ app.launch()
68
+
69
+ # Dependencies
70
+ # =============
71
+ # The following dependencies are required to run this app:
72
+ # - gradio
73
+ # - pytz
74
+ #
75
+ # You can install these dependencies using pip:
76
+ # pip install gradio pytz