Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from data_loader import load_data
|
| 4 |
+
from visualizer import create_plot
|
| 5 |
+
from utils import get_numeric_columns, get_all_columns
|
| 6 |
+
|
| 7 |
+
df_global = None # Used to persist the DataFrame
|
| 8 |
+
|
| 9 |
+
def process_file(file):
|
| 10 |
+
global df_global
|
| 11 |
+
df_global = load_data(file)
|
| 12 |
+
return (
|
| 13 |
+
df_global.head(),
|
| 14 |
+
get_all_columns(df_global),
|
| 15 |
+
get_numeric_columns(df_global)
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
def update_plot(chart_type, x_col, y_col):
|
| 19 |
+
if df_global is not None:
|
| 20 |
+
fig = create_plot(df_global, chart_type, x_col, y_col)
|
| 21 |
+
return fig
|
| 22 |
+
return None
|
| 23 |
+
|
| 24 |
+
with gr.Blocks() as demo:
|
| 25 |
+
gr.Markdown("# 📊 Power BI-style Data Visualization App")
|
| 26 |
+
|
| 27 |
+
with gr.Row():
|
| 28 |
+
file_input = gr.File(label="Upload your data file (.csv, .xlsx, .json)")
|
| 29 |
+
dataframe_output = gr.Dataframe(label="Data Preview", interactive=False)
|
| 30 |
+
|
| 31 |
+
with gr.Row():
|
| 32 |
+
chart_type = gr.Dropdown(["Bar Chart", "Line Chart", "Scatter Plot", "Pie Chart", "Box Plot"], label="Chart Type")
|
| 33 |
+
x_col = gr.Dropdown(label="X-axis Column")
|
| 34 |
+
y_col = gr.Dropdown(label="Y-axis Column")
|
| 35 |
+
|
| 36 |
+
plot_output = gr.Plot(label="Generated Chart")
|
| 37 |
+
|
| 38 |
+
file_input.change(fn=process_file, inputs=file_input, outputs=[dataframe_output, x_col, y_col])
|
| 39 |
+
gr.Button("Generate Chart").click(fn=update_plot, inputs=[chart_type, x_col, y_col], outputs=plot_output)
|
| 40 |
+
|
| 41 |
+
demo.launch()
|