haraberget commited on
Commit
e9a414a
·
verified ·
1 Parent(s): c15b99f

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +91 -0
app.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import time
4
+ import datetime
5
+ import threading
6
+
7
+ # Function to fetch cryptocurrency prices from CoinGecko
8
+ def get_crypto_prices(crypto_ids, vs_currency="usd"):
9
+ try:
10
+ ids = ",".join(crypto_ids)
11
+ url = f"https://api.coingecko.com/api/v3/simple/price?ids={ids}&vs_currencies={vs_currency}"
12
+ response = requests.get(url)
13
+ response.raise_for_status()
14
+ data = response.json()
15
+ prices = {}
16
+ for crypto_id in crypto_ids:
17
+ if crypto_id in data:
18
+ prices[crypto_id] = data[crypto_id][vs_currency]
19
+ else:
20
+ prices[crypto_id] = "N/A" # Indicate if price is not available
21
+ return prices
22
+ except requests.exceptions.RequestException as e:
23
+ return {crypto_id: f"Error: {e}" for crypto_id in crypto_ids}
24
+ except Exception as e:
25
+ return {crypto_id: f"Error: {e}" for crypto_id in crypto_ids}
26
+
27
+ # Function to get the current time as a string
28
+ def get_current_time():
29
+ now = datetime.datetime.now()
30
+ return now.strftime("%Y-%m-%d %H:%M:%S")
31
+
32
+ # Function to calculate the countdown to the next minute
33
+ def get_countdown():
34
+ now = datetime.datetime.now()
35
+ seconds_to_next_minute = 60 - now.second
36
+ return f"Next Update in: {seconds_to_next_minute} seconds"
37
+
38
+ # Shared data (prices)
39
+ shared_prices = {} # This is the "Prices" that is shown to the GUI.
40
+
41
+ def scheduled_update(): # schedule a thread to trigger every minute.
42
+ global shared_prices # import the global shared prices so we can apply changes.
43
+ crypto_ids = ["bitcoin", "ethereum", "binancecoin", "ripple", "litecoin", "dash"]
44
+ shared_prices = get_crypto_prices(crypto_ids) # Apply the new prices
45
+
46
+ now = datetime.datetime.now()
47
+ seconds_to_next_minute = 60 - now.second # get correct timing, it triggers in 1 minute.
48
+ threading.Timer(seconds_to_next_minute, scheduled_update).start() # Schedule the next update
49
+
50
+ # Gradio Interface
51
+ def update_all(): # runs the GUI clock.
52
+ global shared_prices
53
+ crypto_ids = ["bitcoin", "ethereum", "binancecoin", "ripple", "litecoin", "dash"] # we want it as one function.
54
+ price_strings = []
55
+ for crypto_id in crypto_ids:
56
+ if crypto_id not in shared_prices: # when it loads the price might not be there, show some error message instead, make it more user friendy.
57
+ price_strings.append("Loading, Please wait.") # give a warning state that something is going on.
58
+ continue
59
+
60
+ price = shared_prices[crypto_id] # it might come as error
61
+ if isinstance(price, str): # means an error string.
62
+ price_strings.append(f"Current {crypto_id.upper()} Price: {price}")
63
+ else:
64
+ price_strings.append(f"Current {crypto_id.upper()} Price: ${price:.2f}") # apply it to the UI.
65
+
66
+ return get_current_time(), get_countdown(), *price_strings
67
+
68
+ with gr.Blocks() as demo:
69
+ clock_output = gr.Textbox(label="Current Time") # put it before the row.
70
+ countdown_output = gr.Textbox(label="Next Update")
71
+
72
+ with gr.Row(): # prices are in these objects now.
73
+ with gr.Column():
74
+ output_btc = gr.Textbox(label="BTC Price")
75
+ output_eth = gr.Textbox(label="ETH Price")
76
+ output_bnb = gr.Textbox(label="BNB Price")
77
+ with gr.Column():
78
+ output_xrp = gr.Textbox(label="XRP Price")
79
+ output_ltc = gr.Textbox(label="LTC Price")
80
+ output_dash = gr.Textbox(label="DASH Price")
81
+
82
+ demo.load(update_all, None, [clock_output, countdown_output, output_btc, output_eth, output_bnb, output_xrp, output_ltc, output_dash], every=1) # Update clock every second, the timer runs every second. Might be bad, as it updates more frequent.
83
+
84
+ # Start the scheduled updates and fetch the initial data
85
+ now = datetime.datetime.now() # so it can run for a bit before setting.
86
+ seconds_to_next_minute = 60 - now.second
87
+ time.sleep(seconds_to_next_minute) # wait for start, you may want to do this to display what you want in the GUI so you can debug.
88
+ scheduled_update() # start it, get new results
89
+
90
+ if __name__ == "__main__":
91
+ demo.launch()