Update app.py
Browse files
app.py
CHANGED
|
@@ -1,37 +1,197 @@
|
|
| 1 |
-
|
| 2 |
-
import plotly.express as px
|
| 3 |
import pandas as pd
|
| 4 |
-
import
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
server = app.server # This is important for deployment
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
#
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
)
|
| 30 |
-
])
|
| 31 |
|
| 32 |
-
if __name__ ==
|
| 33 |
-
|
| 34 |
-
app.run_server(host='0.0.0.0', port=7860, debug=False)
|
| 35 |
-
else:
|
| 36 |
-
# For deployment (Hugging Face Spaces will use this)
|
| 37 |
-
server = app.server
|
|
|
|
| 1 |
+
import gradio as gr
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
+
import plotly.express as px
|
| 4 |
+
import plotly.graph_objects as go
|
| 5 |
+
from plotly.subplots import make_subplots
|
| 6 |
+
import io
|
|
|
|
| 7 |
|
| 8 |
+
def create_dashboard(file, chart_type, x_column, y_column, color_column):
|
| 9 |
+
"""Create dashboard based on uploaded dataset"""
|
| 10 |
+
|
| 11 |
+
if file is None:
|
| 12 |
+
return None, "Please upload a CSV file"
|
| 13 |
+
|
| 14 |
+
try:
|
| 15 |
+
# Read the uploaded file
|
| 16 |
+
if file.name.endswith('.csv'):
|
| 17 |
+
df = pd.read_csv(file.name)
|
| 18 |
+
elif file.name.endswith('.xlsx'):
|
| 19 |
+
df = pd.read_excel(file.name)
|
| 20 |
+
else:
|
| 21 |
+
return None, "Please upload a CSV or Excel file"
|
| 22 |
+
|
| 23 |
+
# Data info
|
| 24 |
+
info = f"""
|
| 25 |
+
Dataset Info:
|
| 26 |
+
- Shape: {df.shape[0]} rows × {df.shape[1]} columns
|
| 27 |
+
- Columns: {', '.join(df.columns.tolist())}
|
| 28 |
+
- Memory usage: {df.memory_usage().sum()} bytes
|
| 29 |
+
"""
|
| 30 |
+
|
| 31 |
+
# Validate columns exist
|
| 32 |
+
if x_column not in df.columns or y_column not in df.columns:
|
| 33 |
+
return None, f"Columns not found. Available: {list(df.columns)}"
|
| 34 |
+
|
| 35 |
+
# Create the chart based on type
|
| 36 |
+
if chart_type == "Bar Chart":
|
| 37 |
+
fig = px.bar(df, x=x_column, y=y_column,
|
| 38 |
+
color=color_column if color_column in df.columns else None,
|
| 39 |
+
title=f"{chart_type}: {y_column} by {x_column}")
|
| 40 |
+
|
| 41 |
+
elif chart_type == "Line Chart":
|
| 42 |
+
fig = px.line(df, x=x_column, y=y_column,
|
| 43 |
+
color=color_column if color_column in df.columns else None,
|
| 44 |
+
title=f"{chart_type}: {y_column} vs {x_column}")
|
| 45 |
+
|
| 46 |
+
elif chart_type == "Scatter Plot":
|
| 47 |
+
fig = px.scatter(df, x=x_column, y=y_column,
|
| 48 |
+
color=color_column if color_column in df.columns else None,
|
| 49 |
+
title=f"{chart_type}: {y_column} vs {x_column}")
|
| 50 |
+
|
| 51 |
+
elif chart_type == "Histogram":
|
| 52 |
+
fig = px.histogram(df, x=x_column,
|
| 53 |
+
color=color_column if color_column in df.columns else None,
|
| 54 |
+
title=f"{chart_type}: Distribution of {x_column}")
|
| 55 |
+
|
| 56 |
+
elif chart_type == "Box Plot":
|
| 57 |
+
fig = px.box(df, x=x_column, y=y_column,
|
| 58 |
+
color=color_column if color_column in df.columns else None,
|
| 59 |
+
title=f"{chart_type}: {y_column} by {x_column}")
|
| 60 |
+
|
| 61 |
+
elif chart_type == "Multi-Chart Dashboard":
|
| 62 |
+
# Create a comprehensive dashboard
|
| 63 |
+
fig = make_subplots(
|
| 64 |
+
rows=2, cols=2,
|
| 65 |
+
subplot_titles=(f'{y_column} by {x_column}',
|
| 66 |
+
f'Distribution of {x_column}',
|
| 67 |
+
f'Correlation Plot',
|
| 68 |
+
'Summary Statistics'),
|
| 69 |
+
specs=[[{"type": "bar"}, {"type": "histogram"}],
|
| 70 |
+
[{"type": "scatter"}, {"type": "table"}]]
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
# Bar chart
|
| 74 |
+
fig.add_trace(
|
| 75 |
+
go.Bar(x=df[x_column], y=df[y_column], name="Bar"),
|
| 76 |
+
row=1, col=1
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
# Histogram
|
| 80 |
+
fig.add_trace(
|
| 81 |
+
go.Histogram(x=df[x_column], name="Distribution"),
|
| 82 |
+
row=1, col=2
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
# Scatter plot if we have numeric columns
|
| 86 |
+
numeric_cols = df.select_dtypes(include=['number']).columns
|
| 87 |
+
if len(numeric_cols) >= 2:
|
| 88 |
+
fig.add_trace(
|
| 89 |
+
go.Scatter(x=df[numeric_cols[0]], y=df[numeric_cols[1]],
|
| 90 |
+
mode='markers', name="Correlation"),
|
| 91 |
+
row=2, col=1
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
# Summary table
|
| 95 |
+
summary_df = df.describe()
|
| 96 |
+
fig.add_trace(
|
| 97 |
+
go.Table(
|
| 98 |
+
header=dict(values=['Statistic'] + list(summary_df.columns)),
|
| 99 |
+
cells=dict(values=[summary_df.index] +
|
| 100 |
+
[summary_df[col] for col in summary_df.columns])
|
| 101 |
+
),
|
| 102 |
+
row=2, col=2
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
fig.update_layout(height=800, title="Comprehensive Dashboard")
|
| 106 |
+
|
| 107 |
+
else:
|
| 108 |
+
return None, "Chart type not supported"
|
| 109 |
+
|
| 110 |
+
# Update layout
|
| 111 |
+
fig.update_layout(
|
| 112 |
+
template="plotly_white",
|
| 113 |
+
width=800,
|
| 114 |
+
height=600
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
return fig, info
|
| 118 |
+
|
| 119 |
+
except Exception as e:
|
| 120 |
+
return None, f"Error processing file: {str(e)}"
|
| 121 |
|
| 122 |
+
def get_columns(file):
|
| 123 |
+
"""Extract column names from uploaded file"""
|
| 124 |
+
if file is None:
|
| 125 |
+
return gr.Dropdown(choices=[])
|
| 126 |
+
|
| 127 |
+
try:
|
| 128 |
+
if file.name.endswith('.csv'):
|
| 129 |
+
df = pd.read_csv(file.name)
|
| 130 |
+
elif file.name.endswith('.xlsx'):
|
| 131 |
+
df = pd.read_excel(file.name)
|
| 132 |
+
else:
|
| 133 |
+
return gr.Dropdown(choices=[])
|
| 134 |
+
|
| 135 |
+
columns = df.columns.tolist()
|
| 136 |
+
return (gr.Dropdown(choices=columns, value=columns[0] if columns else None),
|
| 137 |
+
gr.Dropdown(choices=columns, value=columns[1] if len(columns) > 1 else columns[0]),
|
| 138 |
+
gr.Dropdown(choices=['None'] + columns, value='None'))
|
| 139 |
+
except:
|
| 140 |
+
return (gr.Dropdown(choices=[]), gr.Dropdown(choices=[]), gr.Dropdown(choices=[]))
|
| 141 |
|
| 142 |
+
# Create Gradio interface
|
| 143 |
+
with gr.Blocks(title="Dynamic Dashboard Creator") as demo:
|
| 144 |
+
gr.Markdown("# 📊 Dynamic Dashboard Creator")
|
| 145 |
+
gr.Markdown("Upload any CSV/Excel file and create interactive dashboards!")
|
| 146 |
+
|
| 147 |
+
with gr.Row():
|
| 148 |
+
with gr.Column(scale=1):
|
| 149 |
+
file_upload = gr.File(
|
| 150 |
+
label="Upload Dataset (CSV or Excel)",
|
| 151 |
+
file_types=[".csv", ".xlsx"]
|
| 152 |
+
)
|
| 153 |
+
|
| 154 |
+
chart_type = gr.Dropdown(
|
| 155 |
+
choices=["Bar Chart", "Line Chart", "Scatter Plot",
|
| 156 |
+
"Histogram", "Box Plot", "Multi-Chart Dashboard"],
|
| 157 |
+
label="Chart Type",
|
| 158 |
+
value="Bar Chart"
|
| 159 |
+
)
|
| 160 |
+
|
| 161 |
+
x_column = gr.Dropdown(
|
| 162 |
+
label="X-axis Column",
|
| 163 |
+
choices=[]
|
| 164 |
+
)
|
| 165 |
+
|
| 166 |
+
y_column = gr.Dropdown(
|
| 167 |
+
label="Y-axis Column",
|
| 168 |
+
choices=[]
|
| 169 |
+
)
|
| 170 |
+
|
| 171 |
+
color_column = gr.Dropdown(
|
| 172 |
+
label="Color Column (Optional)",
|
| 173 |
+
choices=[]
|
| 174 |
+
)
|
| 175 |
+
|
| 176 |
+
create_btn = gr.Button("Create Dashboard", variant="primary")
|
| 177 |
+
|
| 178 |
+
with gr.Column(scale=2):
|
| 179 |
+
plot_output = gr.Plot(label="Dashboard")
|
| 180 |
+
info_output = gr.Textbox(label="Dataset Info", lines=5)
|
| 181 |
+
|
| 182 |
+
# Update column dropdowns when file is uploaded
|
| 183 |
+
file_upload.change(
|
| 184 |
+
fn=get_columns,
|
| 185 |
+
inputs=[file_upload],
|
| 186 |
+
outputs=[x_column, y_column, color_column]
|
| 187 |
+
)
|
| 188 |
+
|
| 189 |
+
# Create dashboard when button is clicked
|
| 190 |
+
create_btn.click(
|
| 191 |
+
fn=create_dashboard,
|
| 192 |
+
inputs=[file_upload, chart_type, x_column, y_column, color_column],
|
| 193 |
+
outputs=[plot_output, info_output]
|
| 194 |
)
|
|
|
|
| 195 |
|
| 196 |
+
if __name__ == "__main__":
|
| 197 |
+
demo.launch(share=True)
|
|
|
|
|
|
|
|
|
|
|
|