Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import plotly.graph_objects as go
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from io import StringIO
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def create_interactive_bitcoin_chart(csv_text=None):
|
| 8 |
+
"""Create interactive Bitcoin volume chart from CSV data"""
|
| 9 |
+
try:
|
| 10 |
+
if csv_text and csv_text.strip():
|
| 11 |
+
# Parse CSV from text area
|
| 12 |
+
df = pd.read_csv(StringIO(csv_text))
|
| 13 |
+
else:
|
| 14 |
+
# Fallback to default file if no CSV provided
|
| 15 |
+
df = pd.read_csv('BTCUSD_5Y_FROM_PERPLEXITY.csv')
|
| 16 |
+
|
| 17 |
+
# Process the data
|
| 18 |
+
df['Date'] = pd.to_datetime(df['Date'])
|
| 19 |
+
df = df.sort_values('Date')
|
| 20 |
+
|
| 21 |
+
# Fix the deprecated 'M' frequency - use 'ME' instead
|
| 22 |
+
df_monthly = df.set_index('Date').resample('ME').sum(numeric_only=True)
|
| 23 |
+
df_monthly = df_monthly.reset_index()
|
| 24 |
+
|
| 25 |
+
# Filter for 2009-2025 (full history)
|
| 26 |
+
df_monthly = df_monthly[
|
| 27 |
+
(df_monthly['Date'] >= '2009-01-01') &
|
| 28 |
+
(df_monthly['Date'] <= '2025-12-31')
|
| 29 |
+
]
|
| 30 |
+
|
| 31 |
+
# Create interactive Plotly chart
|
| 32 |
+
fig = go.Figure()
|
| 33 |
+
|
| 34 |
+
fig.add_trace(go.Scatter(
|
| 35 |
+
x=df_monthly['Date'],
|
| 36 |
+
y=df_monthly['Volume'],
|
| 37 |
+
mode='lines+markers',
|
| 38 |
+
marker=dict(color='#1FB8CD', size=4),
|
| 39 |
+
line=dict(color='#1FB8CD', width=2),
|
| 40 |
+
name='Monthly Volume',
|
| 41 |
+
hovertemplate='<b>Date:</b> %{x|%Y-%m}<br>' +
|
| 42 |
+
'<b>Volume:</b> %{y:,.0f}<br>' +
|
| 43 |
+
'<extra></extra>'
|
| 44 |
+
))
|
| 45 |
+
|
| 46 |
+
fig.update_layout(
|
| 47 |
+
title='Bitcoin Monthly Trading Volume (Interactive)',
|
| 48 |
+
xaxis_title='Date',
|
| 49 |
+
yaxis_title='Volume',
|
| 50 |
+
hovermode='x unified',
|
| 51 |
+
showlegend=False,
|
| 52 |
+
height=600
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
return fig
|
| 56 |
+
|
| 57 |
+
except Exception as e:
|
| 58 |
+
fig = go.Figure()
|
| 59 |
+
fig.add_annotation(
|
| 60 |
+
text=f"Error processing data: {str(e)}",
|
| 61 |
+
xref="paper", yref="paper",
|
| 62 |
+
x=0.5, y=0.5, xanchor='center', yanchor='middle',
|
| 63 |
+
showarrow=False,
|
| 64 |
+
font=dict(size=16, color="red")
|
| 65 |
+
)
|
| 66 |
+
fig.update_layout(
|
| 67 |
+
title="Error Loading Chart",
|
| 68 |
+
xaxis=dict(visible=False),
|
| 69 |
+
yaxis=dict(visible=False)
|
| 70 |
+
)
|
| 71 |
+
return fig
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
def create_gradio_interface():
|
| 75 |
+
"""Create a Gradio interface for interactive Bitcoin volume chart"""
|
| 76 |
+
|
| 77 |
+
with gr.Blocks(title="Interactive Bitcoin Volume Analysis") as demo:
|
| 78 |
+
gr.Markdown("# Interactive Bitcoin Monthly Trading Volume")
|
| 79 |
+
gr.Markdown("**Features:** Zoom, pan, hover for exact values, and explore the data interactively!")
|
| 80 |
+
|
| 81 |
+
with gr.Row():
|
| 82 |
+
# CSV TextArea input
|
| 83 |
+
csv_input = gr.Textbox(
|
| 84 |
+
label="CSV Data (Optional)",
|
| 85 |
+
placeholder="Date,Open,High,Low,Close,Volume\n2025-07-14,119130.81,123231.07,118949.18,121943.63,4009\n...",
|
| 86 |
+
lines=10,
|
| 87 |
+
max_lines=20,
|
| 88 |
+
interactive=True,
|
| 89 |
+
value=""
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
with gr.Row():
|
| 93 |
+
generate_btn = gr.Button("Generate Interactive Chart", variant="primary")
|
| 94 |
+
|
| 95 |
+
with gr.Row():
|
| 96 |
+
chart_output = gr.Plot(label="Interactive Bitcoin Volume Chart")
|
| 97 |
+
|
| 98 |
+
generate_btn.click(
|
| 99 |
+
fn=create_interactive_bitcoin_chart,
|
| 100 |
+
inputs=csv_input,
|
| 101 |
+
outputs=chart_output
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
csv_input.change(
|
| 105 |
+
fn=create_interactive_bitcoin_chart,
|
| 106 |
+
inputs=csv_input,
|
| 107 |
+
outputs=chart_output
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
demo.load(fn=create_interactive_bitcoin_chart, outputs=chart_output)
|
| 111 |
+
|
| 112 |
+
return demo
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
# Launch the interface
|
| 116 |
+
if __name__ == "__main__":
|
| 117 |
+
demo = create_gradio_interface()
|
| 118 |
+
demo.launch()
|