Spaces:
Sleeping
Sleeping
import pandas as pd | |
import plotly.graph_objects as go | |
import gradio as gr | |
from io import StringIO | |
def create_interactive_bitcoin_chart(csv_text=None): | |
"""Create interactive Bitcoin volume chart from CSV data""" | |
try: | |
if csv_text and csv_text.strip(): | |
# Parse CSV from text area | |
df = pd.read_csv(StringIO(csv_text)) | |
else: | |
# Fallback to default file if no CSV provided | |
df = pd.read_csv('BTCUSD_5Y_FROM_PERPLEXITY.csv') | |
# Process the data | |
df['Date'] = pd.to_datetime(df['Date']) | |
df = df.sort_values('Date') | |
# Fix the deprecated 'M' frequency - use 'ME' instead | |
df_monthly = df.set_index('Date').resample('ME').sum(numeric_only=True) | |
df_monthly = df_monthly.reset_index() | |
# Filter for 2009-2025 (full history) | |
df_monthly = df_monthly[ | |
(df_monthly['Date'] >= '2009-01-01') & | |
(df_monthly['Date'] <= '2025-12-31') | |
] | |
# Create interactive Plotly chart | |
fig = go.Figure() | |
fig.add_trace(go.Scatter( | |
x=df_monthly['Date'], | |
y=df_monthly['Volume'], | |
mode='lines+markers', | |
marker=dict(color='#1FB8CD', size=4), | |
line=dict(color='#1FB8CD', width=2), | |
name='Monthly Volume', | |
hovertemplate='<b>Date:</b> %{x|%Y-%m}<br>' + | |
'<b>Volume:</b> %{y:,.0f}<br>' + | |
'<extra></extra>' | |
)) | |
fig.update_layout( | |
title='Bitcoin Monthly Trading Volume (Interactive)', | |
xaxis_title='Date', | |
yaxis_title='Volume', | |
hovermode='x unified', | |
showlegend=False, | |
height=600 | |
) | |
return fig | |
except Exception as e: | |
fig = go.Figure() | |
fig.add_annotation( | |
text=f"Error processing data: {str(e)}", | |
xref="paper", yref="paper", | |
x=0.5, y=0.5, xanchor='center', yanchor='middle', | |
showarrow=False, | |
font=dict(size=16, color="red") | |
) | |
fig.update_layout( | |
title="Error Loading Chart", | |
xaxis=dict(visible=False), | |
yaxis=dict(visible=False) | |
) | |
return fig | |
def create_gradio_interface(): | |
"""Create a Gradio interface for interactive Bitcoin volume chart""" | |
with gr.Blocks(title="Interactive Bitcoin Volume Analysis") as demo: | |
gr.Markdown("# Interactive Bitcoin Monthly Trading Volume") | |
gr.Markdown("**Features:** Zoom, pan, hover for exact values, and explore the data interactively!") | |
with gr.Row(): | |
# CSV TextArea input | |
csv_input = gr.Textbox( | |
label="CSV Data (Optional)", | |
placeholder="Date,Open,High,Low,Close,Volume\n2025-07-14,119130.81,123231.07,118949.18,121943.63,4009\n...", | |
lines=10, | |
max_lines=20, | |
interactive=True, | |
value="" | |
) | |
with gr.Row(): | |
generate_btn = gr.Button("Generate Interactive Chart", variant="primary") | |
with gr.Row(): | |
chart_output = gr.Plot(label="Interactive Bitcoin Volume Chart") | |
generate_btn.click( | |
fn=create_interactive_bitcoin_chart, | |
inputs=csv_input, | |
outputs=chart_output | |
) | |
csv_input.change( | |
fn=create_interactive_bitcoin_chart, | |
inputs=csv_input, | |
outputs=chart_output | |
) | |
demo.load(fn=create_interactive_bitcoin_chart, outputs=chart_output) | |
return demo | |
# Launch the interface | |
if __name__ == "__main__": | |
demo = create_gradio_interface() | |
demo.launch() | |