File size: 3,732 Bytes
c093461
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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()