File size: 4,740 Bytes
f6a9785
 
b2a8be9
b4fc6fa
 
f6a9785
b2a8be9
4c4c16b
b4fc6fa
b2a8be9
a81ca9e
b4fc6fa
 
fd895d9
b4fc6fa
fd895d9
b4fc6fa
fd895d9
b4fc6fa
fd895d9
a81ca9e
9937af7
b4fc6fa
4464909
b4fc6fa
a81ca9e
b2a8be9
b4fc6fa
 
b2a8be9
b4fc6fa
b2a8be9
b4fc6fa
02dc6b9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fd895d9
 
02dc6b9
 
 
 
 
 
 
12eee27
02dc6b9
 
 
 
 
 
 
 
 
 
 
fd895d9
02dc6b9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fd895d9
63b951e
 
 
 
 
 
 
 
 
 
 
160656e
63b951e
 
 
 
 
 
 
 
 
 
 
02dc6b9
 
fd895d9
 
02dc6b9
fd895d9
02dc6b9
fd895d9
 
 
 
 
 
02dc6b9
 
 
 
 
 
fd895d9
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import gradio as gr
import pandas as pd
import plotly.express as px
import io
import requests

df_global = pd.DataFrame()

# Load from file
def load_data(file):
    try:
        if file is None:
            return pd.DataFrame()
        ext = file.name.split('.')[-1]
        if ext == 'csv':
            return pd.read_csv(file.name)
        elif ext in ['xls', 'xlsx']:
            return pd.read_excel(file.name)
        elif ext == 'json':
            return pd.read_json(file.name)
    except Exception as e:
        print("File load error:", e)
    return pd.DataFrame()

# Load from URL
def load_url(url):
    try:
        response = requests.get(url)
        response.raise_for_status()
        if url.endswith(".csv"):
            return pd.read_csv(io.StringIO(response.text))
        elif url.endswith(".json"):
            return pd.read_json(io.StringIO(response.text))
        elif url.endswith(".xlsx") or url.endswith(".xls"):
            return pd.read_excel(io.BytesIO(response.content))
    except Exception as e:
        print("URL load error:", e)
    return pd.DataFrame()

# Process both file and URL
def process_inputs(file, url):
    global df_global
    df = load_url(url) if url else load_data(file)

    if df.empty:
        return pd.DataFrame(), gr.update(choices=[], value=None), gr.update(choices=[], value=None)

    df_global = df
    all_cols = list(df.columns)
    num_cols = list(df.select_dtypes(include="number").columns)

    default_x = all_cols[0] if all_cols else None
    default_y = num_cols[0] if num_cols else None

    return df.head(), gr.update(choices=all_cols, value=default_x), gr.update(choices=num_cols, value=default_y)

# Plot
def update_plot(chart_type, x, y):
    global df_global
    if df_global.empty:
        return None

    try:
        if chart_type == "Bar Chart":
            return px.bar(df_global, x=x, y=y)
        elif chart_type == "Line Chart":
            return px.line(df_global, x=x, y=y)
        elif chart_type == "Scatter Plot":
            return px.scatter(df_global, x=x, y=y)
        elif chart_type == "Pie Chart":
            return px.pie(df_global, names=x, values=y)
        elif chart_type == "Box Plot":
            return px.box(df_global, x=x, y=y)
    except Exception as e:
        print("Plot error:", e)
        return None


# Themed UI
with gr.Blocks(css="""
body {
    margin: 0;
    padding: 0;
    font-family: 'Segoe UI', sans-serif;
    background: #0f2027;
    background: linear-gradient(to right, #2c5364, #203a43, #0f2027);
    color: skyblue;
}
#bg-theme {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: -2;
    background: linear-gradient(120deg, rgba(33,147,176,0.6), rgba(109,213,237,0.6));
    background-size: 200% 200%;
    animation: bgMove 20s ease infinite;
    filter: blur(100px);    
}
@keyframes bgMove {
    0% { background-position: 0% 50%; }
    50% { background-position: 100% 50%; }
    100% { background-position: 0% 50%; }
}
.fade-in {
    animation: fadeInUp 1s ease both;
}
@keyframes fadeInUp {
    0% { opacity: 0; transform: translateY(20px); }
    100% { opacity: 1; transform: translateY(0); }
}
""") as demo:

    gr.HTML('<div id="bg-theme"></div>')  # Themed animated background
    gr.Markdown(
    """
    <h1 style="
        font-size: 3.5em;
        background: linear-gradient(270deg, #ff004c, #00ffea, #ffb600, #ff004c);
        background-size: 800% 800%;
        -webkit-background-clip: text;
        -webkit-text-fill-color: transparent;
        animation: rainbow 8s ease infinite;
        font-weight: bold;
        ">
        📈⚡ Thunder BI-Data Visualizer
    </h1>

    <style>
    @keyframes rainbow {
        0%{background-position:0% 50%}
        50%{background-position:100% 50%}
        100%{background-position:0% 50%}
    }
    </style>
    """, elem_classes="fade-in"
    )

    with gr.Row():
        file_input = gr.File(file_types=[".csv", ".xlsx", ".json"], label="Upload File")
        url_input = gr.Textbox(label="Or enter file URL")

    load_button = gr.Button("Load")

    df_preview = gr.Dataframe(label="Preview", interactive=False)
    x_dropdown = gr.Dropdown(label="X-axis")
    y_dropdown = gr.Dropdown(label="Y-axis")
    chart_type = gr.Dropdown(["Bar Chart", "Line Chart", "Scatter Plot", "Pie Chart", "Box Plot"], label="Chart Type")
    plot_area = gr.Plot(label="Chart")
    generate_button = gr.Button("Generate Plot")

    # Event hooks
    load_button.click(process_inputs, inputs=[file_input, url_input], outputs=[df_preview, x_dropdown, y_dropdown])
    generate_button.click(update_plot, inputs=[chart_type, x_dropdown, y_dropdown], outputs=plot_area)

if __name__ == "__main__":
    demo.launch()