Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,7 @@ import os
|
|
| 6 |
from threading import Thread
|
| 7 |
|
| 8 |
import dash
|
| 9 |
-
from dash import dcc, html, Input, Output, State
|
| 10 |
import dash_bootstrap_components as dbc
|
| 11 |
import pandas as pd
|
| 12 |
import plotly.graph_objs as go
|
|
@@ -38,7 +38,12 @@ app.layout = dbc.Container([
|
|
| 38 |
},
|
| 39 |
multiple=False
|
| 40 |
),
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
dbc.Button("Generate Insights", id="submit-button", color="primary", className="mt-3"),
|
| 43 |
])
|
| 44 |
], className="mb-4"),
|
|
@@ -55,7 +60,8 @@ app.layout = dbc.Container([
|
|
| 55 |
])
|
| 56 |
])
|
| 57 |
]
|
| 58 |
-
)
|
|
|
|
| 59 |
], fluid=True)
|
| 60 |
|
| 61 |
def parse_contents(contents, filename):
|
|
@@ -150,13 +156,51 @@ def generate_plot(df, plot_info):
|
|
| 150 |
fig.update_layout(title=plot_info['title'], xaxis_title=plot_info['x'], yaxis_title=plot_info['y'])
|
| 151 |
return fig
|
| 152 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
@app.callback(
|
| 154 |
[Output('visualization-1', 'figure'),
|
| 155 |
Output('visualization-2', 'figure'),
|
| 156 |
Output('visualization-3', 'figure'),
|
| 157 |
Output('error-message', 'children')],
|
| 158 |
[Input('submit-button', 'n_clicks')],
|
| 159 |
-
[State('
|
| 160 |
State('upload-data', 'filename'),
|
| 161 |
State('instructions', 'value')]
|
| 162 |
)
|
|
|
|
| 6 |
from threading import Thread
|
| 7 |
|
| 8 |
import dash
|
| 9 |
+
from dash import dcc, html, Input, Output, State, callback_context
|
| 10 |
import dash_bootstrap_components as dbc
|
| 11 |
import pandas as pd
|
| 12 |
import plotly.graph_objs as go
|
|
|
|
| 38 |
},
|
| 39 |
multiple=False
|
| 40 |
),
|
| 41 |
+
html.Div(id='upload-feedback', className="mt-2"),
|
| 42 |
+
html.Div([
|
| 43 |
+
html.Span(id='filename-display', className="mr-2"),
|
| 44 |
+
dbc.Button("Delete File", id="delete-file-button", color="danger", className="mt-2", style={'display': 'none'})
|
| 45 |
+
], className="mt-2"),
|
| 46 |
+
dbc.Input(id="instructions", placeholder="Describe the analysis you want...", type="text", className="mt-3"),
|
| 47 |
dbc.Button("Generate Insights", id="submit-button", color="primary", className="mt-3"),
|
| 48 |
])
|
| 49 |
], className="mb-4"),
|
|
|
|
| 60 |
])
|
| 61 |
])
|
| 62 |
]
|
| 63 |
+
),
|
| 64 |
+
dcc.Store(id='uploaded-data')
|
| 65 |
], fluid=True)
|
| 66 |
|
| 67 |
def parse_contents(contents, filename):
|
|
|
|
| 156 |
fig.update_layout(title=plot_info['title'], xaxis_title=plot_info['x'], yaxis_title=plot_info['y'])
|
| 157 |
return fig
|
| 158 |
|
| 159 |
+
@app.callback(
|
| 160 |
+
[Output('upload-feedback', 'children'),
|
| 161 |
+
Output('filename-display', 'children'),
|
| 162 |
+
Output('delete-file-button', 'style'),
|
| 163 |
+
Output('uploaded-data', 'data')],
|
| 164 |
+
[Input('upload-data', 'contents'),
|
| 165 |
+
Input('delete-file-button', 'n_clicks')],
|
| 166 |
+
[State('upload-data', 'filename')]
|
| 167 |
+
)
|
| 168 |
+
def update_upload_feedback(contents, delete_clicks, filename):
|
| 169 |
+
ctx = callback_context
|
| 170 |
+
if not ctx.triggered:
|
| 171 |
+
return dash.no_update, dash.no_update, dash.no_update, dash.no_update
|
| 172 |
+
|
| 173 |
+
trigger_id = ctx.triggered[0]['prop_id'].split('.')[0]
|
| 174 |
+
|
| 175 |
+
if trigger_id == 'delete-file-button':
|
| 176 |
+
return "File deleted.", "", {'display': 'none'}, None
|
| 177 |
+
|
| 178 |
+
if contents is not None:
|
| 179 |
+
df = parse_contents(contents, filename)
|
| 180 |
+
if df is not None:
|
| 181 |
+
return (
|
| 182 |
+
dbc.Alert("File uploaded successfully!", color="success"),
|
| 183 |
+
f"Uploaded: {filename}",
|
| 184 |
+
{'display': 'inline-block'},
|
| 185 |
+
contents
|
| 186 |
+
)
|
| 187 |
+
else:
|
| 188 |
+
return (
|
| 189 |
+
dbc.Alert("Error parsing the file. Please upload a valid CSV or Excel file.", color="danger"),
|
| 190 |
+
"",
|
| 191 |
+
{'display': 'none'},
|
| 192 |
+
None
|
| 193 |
+
)
|
| 194 |
+
|
| 195 |
+
return dash.no_update, dash.no_update, dash.no_update, dash.no_update
|
| 196 |
+
|
| 197 |
@app.callback(
|
| 198 |
[Output('visualization-1', 'figure'),
|
| 199 |
Output('visualization-2', 'figure'),
|
| 200 |
Output('visualization-3', 'figure'),
|
| 201 |
Output('error-message', 'children')],
|
| 202 |
[Input('submit-button', 'n_clicks')],
|
| 203 |
+
[State('uploaded-data', 'data'),
|
| 204 |
State('upload-data', 'filename'),
|
| 205 |
State('instructions', 'value')]
|
| 206 |
)
|