Update app.py
Browse files
app.py
CHANGED
|
@@ -31,5 +31,107 @@ def load_url(url):
|
|
| 31 |
return pd.read_csv(io.StringIO(response.text))
|
| 32 |
elif url.endswith(".json"):
|
| 33 |
return pd.read_json(io.StringIO(response.text))
|
| 34 |
-
elif url.
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
return pd.read_csv(io.StringIO(response.text))
|
| 32 |
elif url.endswith(".json"):
|
| 33 |
return pd.read_json(io.StringIO(response.text))
|
| 34 |
+
elif url.endswith(".xlsx") or url.endswith(".xls"):
|
| 35 |
+
return pd.read_excel(io.BytesIO(response.content))
|
| 36 |
+
except Exception as e:
|
| 37 |
+
print("URL load error:", e)
|
| 38 |
+
return pd.DataFrame()
|
| 39 |
+
|
| 40 |
+
# Process both file and URL
|
| 41 |
+
def process_inputs(file, url):
|
| 42 |
+
global df_global
|
| 43 |
+
df = load_url(url) if url else load_data(file)
|
| 44 |
+
|
| 45 |
+
if df.empty:
|
| 46 |
+
return pd.DataFrame(), gr.update(choices=[], value=None), gr.update(choices=[], value=None)
|
| 47 |
+
|
| 48 |
+
df_global = df
|
| 49 |
+
all_cols = list(df.columns)
|
| 50 |
+
num_cols = list(df.select_dtypes(include="number").columns)
|
| 51 |
+
|
| 52 |
+
default_x = all_cols[0] if all_cols else None
|
| 53 |
+
default_y = num_cols[0] if num_cols else None
|
| 54 |
+
|
| 55 |
+
return df.head(), gr.update(choices=all_cols, value=default_x), gr.update(choices=num_cols, value=default_y)
|
| 56 |
+
|
| 57 |
+
# Plot
|
| 58 |
+
def update_plot(chart_type, x, y):
|
| 59 |
+
global df_global
|
| 60 |
+
if df_global.empty:
|
| 61 |
+
return None
|
| 62 |
+
|
| 63 |
+
try:
|
| 64 |
+
if chart_type == "Bar Chart":
|
| 65 |
+
return px.bar(df_global, x=x, y=y)
|
| 66 |
+
elif chart_type == "Line Chart":
|
| 67 |
+
return px.line(df_global, x=x, y=y)
|
| 68 |
+
elif chart_type == "Scatter Plot":
|
| 69 |
+
return px.scatter(df_global, x=x, y=y)
|
| 70 |
+
elif chart_type == "Pie Chart":
|
| 71 |
+
return px.pie(df_global, names=x, values=y)
|
| 72 |
+
elif chart_type == "Box Plot":
|
| 73 |
+
return px.box(df_global, x=x, y=y)
|
| 74 |
+
except Exception as e:
|
| 75 |
+
print("Plot error:", e)
|
| 76 |
+
return None
|
| 77 |
+
|
| 78 |
+
# Themed UI
|
| 79 |
+
with gr.Blocks(css="""
|
| 80 |
+
body {
|
| 81 |
+
margin: 0;
|
| 82 |
+
padding: 0;
|
| 83 |
+
font-family: 'Segoe UI', sans-serif;
|
| 84 |
+
background: #0f2027;
|
| 85 |
+
background: linear-gradient(to right, #2c5364, #203a43, #0f2027);
|
| 86 |
+
color: white;
|
| 87 |
+
}
|
| 88 |
+
#bg-theme {
|
| 89 |
+
position: fixed;
|
| 90 |
+
top: 0;
|
| 91 |
+
left: 0;
|
| 92 |
+
width: 100%;
|
| 93 |
+
height: 100%;
|
| 94 |
+
z-index: -2;
|
| 95 |
+
background: linear-gradient(120deg, rgba(33,147,176,0.6), rgba(109,213,237,0.6));
|
| 96 |
+
background-size: 200% 200%;
|
| 97 |
+
animation: bgMove 20s ease infinite;
|
| 98 |
+
filter: blur(100px);
|
| 99 |
+
}
|
| 100 |
+
@keyframes bgMove {
|
| 101 |
+
0% { background-position: 0% 50%; }
|
| 102 |
+
50% { background-position: 100% 50%; }
|
| 103 |
+
100% { background-position: 0% 50%; }
|
| 104 |
+
}
|
| 105 |
+
.fade-in {
|
| 106 |
+
animation: fadeInUp 1s ease both;
|
| 107 |
+
}
|
| 108 |
+
@keyframes fadeInUp {
|
| 109 |
+
0% { opacity: 0; transform: translateY(20px); }
|
| 110 |
+
100% { opacity: 1; transform: translateY(0); }
|
| 111 |
+
}
|
| 112 |
+
""") as demo:
|
| 113 |
+
|
| 114 |
+
gr.HTML('<div id="bg-theme"></div>') # Themed animated background
|
| 115 |
+
|
| 116 |
+
gr.Markdown("## 📊 Thunder BI-Data Visualizer", elem_classes="fade-in")
|
| 117 |
+
|
| 118 |
+
with gr.Row():
|
| 119 |
+
file_input = gr.File(file_types=[".csv", ".xlsx", ".json"], label="Upload File", elem_classes="fade-in")
|
| 120 |
+
url_input = gr.Textbox(label="Or enter file URL", elem_classes="fade-in")
|
| 121 |
+
|
| 122 |
+
load_button = gr.Button("Load", elem_classes="fade-in")
|
| 123 |
+
|
| 124 |
+
df_preview = gr.Dataframe(label="Preview", interactive=False, elem_classes="fade-in")
|
| 125 |
+
x_dropdown = gr.Dropdown(label="X-axis", elem_classes="fade-in")
|
| 126 |
+
y_dropdown = gr.Dropdown(label="Y-axis", elem_classes="fade-in")
|
| 127 |
+
chart_type = gr.Dropdown(["Bar Chart", "Line Chart", "Scatter Plot", "Pie Chart", "Box Plot"], label="Chart Type", elem_classes="fade-in")
|
| 128 |
+
plot_area = gr.Plot(label="Chart", elem_classes="fade-in")
|
| 129 |
+
generate_button = gr.Button("Generate Plot", elem_classes="fade-in")
|
| 130 |
+
|
| 131 |
+
# Event hooks
|
| 132 |
+
load_button.click(process_inputs, inputs=[file_input, url_input], outputs=[df_preview, x_dropdown, y_dropdown])
|
| 133 |
+
generate_button.click(update_plot, inputs=[chart_type, x_dropdown, y_dropdown], outputs=plot_area)
|
| 134 |
+
|
| 135 |
+
if __name__ == "__main__":
|
| 136 |
+
demo.launch()
|
| 137 |
+
|