| import gradio as gr |
| import pandas as pd |
| import plotly.express as px |
| import plotly.graph_objects as go |
| from plotly.subplots import make_subplots |
| import io |
|
|
| def create_dashboard(file, chart_type, x_column, y_column, color_column): |
| """Create dashboard based on uploaded dataset""" |
| |
| if file is None: |
| return None, "Please upload a CSV file" |
| |
| try: |
| |
| if file.name.endswith('.csv'): |
| df = pd.read_csv(file.name) |
| elif file.name.endswith('.xlsx'): |
| df = pd.read_excel(file.name) |
| else: |
| return None, "Please upload a CSV or Excel file" |
| |
| |
| info = f""" |
| Dataset Info: |
| - Shape: {df.shape[0]} rows × {df.shape[1]} columns |
| - Columns: {', '.join(df.columns.tolist())} |
| - Memory usage: {df.memory_usage().sum()} bytes |
| """ |
| |
| |
| if x_column not in df.columns or y_column not in df.columns: |
| return None, f"Columns not found. Available: {list(df.columns)}" |
| |
| |
| if chart_type == "Bar Chart": |
| fig = px.bar(df, x=x_column, y=y_column, |
| color=color_column if color_column in df.columns else None, |
| title=f"{chart_type}: {y_column} by {x_column}") |
| |
| elif chart_type == "Line Chart": |
| fig = px.line(df, x=x_column, y=y_column, |
| color=color_column if color_column in df.columns else None, |
| title=f"{chart_type}: {y_column} vs {x_column}") |
| |
| elif chart_type == "Scatter Plot": |
| fig = px.scatter(df, x=x_column, y=y_column, |
| color=color_column if color_column in df.columns else None, |
| title=f"{chart_type}: {y_column} vs {x_column}") |
| |
| elif chart_type == "Histogram": |
| fig = px.histogram(df, x=x_column, |
| color=color_column if color_column in df.columns else None, |
| title=f"{chart_type}: Distribution of {x_column}") |
| |
| elif chart_type == "Box Plot": |
| fig = px.box(df, x=x_column, y=y_column, |
| color=color_column if color_column in df.columns else None, |
| title=f"{chart_type}: {y_column} by {x_column}") |
| |
| elif chart_type == "Multi-Chart Dashboard": |
| |
| fig = make_subplots( |
| rows=2, cols=2, |
| subplot_titles=(f'{y_column} by {x_column}', |
| f'Distribution of {x_column}', |
| f'Correlation Plot', |
| 'Summary Statistics'), |
| specs=[[{"type": "bar"}, {"type": "histogram"}], |
| [{"type": "scatter"}, {"type": "table"}]] |
| ) |
| |
| |
| fig.add_trace( |
| go.Bar(x=df[x_column], y=df[y_column], name="Bar"), |
| row=1, col=1 |
| ) |
| |
| |
| fig.add_trace( |
| go.Histogram(x=df[x_column], name="Distribution"), |
| row=1, col=2 |
| ) |
| |
| |
| numeric_cols = df.select_dtypes(include=['number']).columns |
| if len(numeric_cols) >= 2: |
| fig.add_trace( |
| go.Scatter(x=df[numeric_cols[0]], y=df[numeric_cols[1]], |
| mode='markers', name="Correlation"), |
| row=2, col=1 |
| ) |
| |
| |
| summary_df = df.describe() |
| fig.add_trace( |
| go.Table( |
| header=dict(values=['Statistic'] + list(summary_df.columns)), |
| cells=dict(values=[summary_df.index] + |
| [summary_df[col] for col in summary_df.columns]) |
| ), |
| row=2, col=2 |
| ) |
| |
| fig.update_layout(height=800, title="Comprehensive Dashboard") |
| |
| else: |
| return None, "Chart type not supported" |
| |
| |
| fig.update_layout( |
| template="plotly_white", |
| width=800, |
| height=600 |
| ) |
| |
| return fig, info |
| |
| except Exception as e: |
| return None, f"Error processing file: {str(e)}" |
|
|
| def get_columns(file): |
| """Extract column names from uploaded file""" |
| if file is None: |
| return gr.Dropdown(choices=[]) |
| |
| try: |
| if file.name.endswith('.csv'): |
| df = pd.read_csv(file.name) |
| elif file.name.endswith('.xlsx'): |
| df = pd.read_excel(file.name) |
| else: |
| return gr.Dropdown(choices=[]) |
| |
| columns = df.columns.tolist() |
| return (gr.Dropdown(choices=columns, value=columns[0] if columns else None), |
| gr.Dropdown(choices=columns, value=columns[1] if len(columns) > 1 else columns[0]), |
| gr.Dropdown(choices=['None'] + columns, value='None')) |
| except: |
| return (gr.Dropdown(choices=[]), gr.Dropdown(choices=[]), gr.Dropdown(choices=[])) |
|
|
| |
| with gr.Blocks(title="Dynamic Dashboard Creator") as demo: |
| gr.Markdown("# 📊 Dynamic Dashboard Creator") |
| gr.Markdown("Upload any CSV/Excel file and create interactive dashboards!") |
| |
| with gr.Row(): |
| with gr.Column(scale=1): |
| file_upload = gr.File( |
| label="Upload Dataset (CSV or Excel)", |
| file_types=[".csv", ".xlsx"] |
| ) |
| |
| chart_type = gr.Dropdown( |
| choices=["Bar Chart", "Line Chart", "Scatter Plot", |
| "Histogram", "Box Plot", "Multi-Chart Dashboard"], |
| label="Chart Type", |
| value="Bar Chart" |
| ) |
| |
| x_column = gr.Dropdown( |
| label="X-axis Column", |
| choices=[] |
| ) |
| |
| y_column = gr.Dropdown( |
| label="Y-axis Column", |
| choices=[] |
| ) |
| |
| color_column = gr.Dropdown( |
| label="Color Column (Optional)", |
| choices=[] |
| ) |
| |
| create_btn = gr.Button("Create Dashboard", variant="primary") |
| |
| with gr.Column(scale=2): |
| plot_output = gr.Plot(label="Dashboard") |
| info_output = gr.Textbox(label="Dataset Info", lines=5) |
| |
| |
| file_upload.change( |
| fn=get_columns, |
| inputs=[file_upload], |
| outputs=[x_column, y_column, color_column] |
| ) |
| |
| |
| create_btn.click( |
| fn=create_dashboard, |
| inputs=[file_upload, chart_type, x_column, y_column, color_column], |
| outputs=[plot_output, info_output] |
| ) |
|
|
| if __name__ == "__main__": |
| demo.launch(share=True) |