dibend commited on
Commit
bca40a6
·
verified ·
1 Parent(s): 72d608d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -20
app.py CHANGED
@@ -1,5 +1,5 @@
1
  import gradio as gr
2
- import matplotlib.pyplot as plt
3
  import requests
4
  import os
5
  import datetime
@@ -41,33 +41,37 @@ def fetch_crypto_data(crypto):
41
  return crypto_dates, crypto_prices
42
 
43
 
44
- def plot_crypto_price(crypto):
45
- """Plots the historical price data for the selected cryptocurrency."""
46
- crypto_dates, crypto_prices = fetch_crypto_data(crypto)
47
 
48
- if isinstance(crypto_dates, str):
49
- return crypto_dates # Return error message
 
 
 
 
 
 
50
 
51
- plt.figure(figsize=(12, 6))
52
- plt.xticks(ticks=range(0, len(crypto_dates), 7), labels=crypto_dates[::7], rotation=90)
53
- color = random.choice(colors)
54
- plt.plot(crypto_dates, crypto_prices, label=crypto, color=color)
55
- plt.fill_between(crypto_dates, crypto_prices, alpha=0.5, color=color)
56
- plt.xlabel("Date")
57
- plt.ylabel("Price (USD)")
58
- plt.title(f"{crypto} Price History")
59
- plt.legend()
60
- plt.grid()
61
 
62
- return plt.gcf()
63
 
64
  # Create Gradio Interface
65
  demo = gr.Interface(
66
- fn=plot_crypto_price,
67
- inputs=gr.Dropdown(choices=cryptos, label="Select Cryptocurrency"),
68
  outputs=gr.Plot(label="Price Chart"),
69
  title="Cryptocurrency Price Visualization",
70
- description="Select a cryptocurrency to view its historical price data over the past year."
71
  )
72
 
73
  if __name__ == "__main__":
 
1
  import gradio as gr
2
+ import plotly.graph_objects as go
3
  import requests
4
  import os
5
  import datetime
 
41
  return crypto_dates, crypto_prices
42
 
43
 
44
+ def plot_crypto_prices():
45
+ """Plots the historical price data for all selected cryptocurrencies."""
46
+ fig = go.Figure()
47
 
48
+ for crypto in cryptos:
49
+ crypto_dates, crypto_prices = fetch_crypto_data(crypto)
50
+
51
+ if isinstance(crypto_dates, str):
52
+ continue # Skip on error
53
+
54
+ color = random.choice(colors)
55
+ fig.add_trace(go.Scatter(x=crypto_dates, y=crypto_prices, mode='lines', name=crypto, line=dict(color=color)))
56
 
57
+ fig.update_layout(
58
+ title="Cryptocurrency Price History",
59
+ xaxis_title="Date",
60
+ yaxis_title="Price (USD)",
61
+ template="plotly_dark",
62
+ xaxis=dict(tickangle=-45),
63
+ legend_title="Cryptos"
64
+ )
 
 
65
 
66
+ return fig
67
 
68
  # Create Gradio Interface
69
  demo = gr.Interface(
70
+ fn=plot_crypto_prices,
71
+ inputs=[],
72
  outputs=gr.Plot(label="Price Chart"),
73
  title="Cryptocurrency Price Visualization",
74
+ description="Displays historical price data for multiple cryptocurrencies over the past year."
75
  )
76
 
77
  if __name__ == "__main__":