Update app.py
Browse files
app.py
CHANGED
|
@@ -42,13 +42,20 @@ app.layout = dbc.Container([
|
|
| 42 |
dbc.Button("Generate Insights", id="submit-button", color="primary", className="mt-3"),
|
| 43 |
])
|
| 44 |
], className="mb-4"),
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
], fluid=True)
|
| 53 |
|
| 54 |
def parse_contents(contents, filename):
|
|
@@ -141,7 +148,8 @@ def generate_plot(df, plot_info):
|
|
| 141 |
@app.callback(
|
| 142 |
[Output('visualization-1', 'figure'),
|
| 143 |
Output('visualization-2', 'figure'),
|
| 144 |
-
Output('visualization-3', 'figure')
|
|
|
|
| 145 |
[Input('submit-button', 'n_clicks')],
|
| 146 |
[State('upload-data', 'contents'),
|
| 147 |
State('upload-data', 'filename'),
|
|
@@ -149,19 +157,26 @@ def generate_plot(df, plot_info):
|
|
| 149 |
State('api-key', 'value')]
|
| 150 |
)
|
| 151 |
def update_output(n_clicks, contents, filename, instructions, api_key):
|
| 152 |
-
if n_clicks is None or contents is None
|
| 153 |
-
return dash.no_update, dash.no_update, dash.no_update
|
|
|
|
|
|
|
|
|
|
| 154 |
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
|
|
|
| 158 |
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
|
| 163 |
-
|
| 164 |
-
|
|
|
|
|
|
|
|
|
|
| 165 |
|
| 166 |
if __name__ == '__main__':
|
| 167 |
app.run(debug=True, host='0.0.0.0', port=7860, threaded=True)
|
|
|
|
| 42 |
dbc.Button("Generate Insights", id="submit-button", color="primary", className="mt-3"),
|
| 43 |
])
|
| 44 |
], className="mb-4"),
|
| 45 |
+
html.Div(id="error-message", className="text-danger mb-3"),
|
| 46 |
+
dcc.Loading(
|
| 47 |
+
id="loading-visualizations",
|
| 48 |
+
type="default",
|
| 49 |
+
children=[
|
| 50 |
+
dbc.Card([
|
| 51 |
+
dbc.CardBody([
|
| 52 |
+
dcc.Graph(id='visualization-1'),
|
| 53 |
+
dcc.Graph(id='visualization-2'),
|
| 54 |
+
dcc.Graph(id='visualization-3'),
|
| 55 |
+
])
|
| 56 |
+
])
|
| 57 |
+
]
|
| 58 |
+
)
|
| 59 |
], fluid=True)
|
| 60 |
|
| 61 |
def parse_contents(contents, filename):
|
|
|
|
| 148 |
@app.callback(
|
| 149 |
[Output('visualization-1', 'figure'),
|
| 150 |
Output('visualization-2', 'figure'),
|
| 151 |
+
Output('visualization-3', 'figure'),
|
| 152 |
+
Output('error-message', 'children')],
|
| 153 |
[Input('submit-button', 'n_clicks')],
|
| 154 |
[State('upload-data', 'contents'),
|
| 155 |
State('upload-data', 'filename'),
|
|
|
|
| 157 |
State('api-key', 'value')]
|
| 158 |
)
|
| 159 |
def update_output(n_clicks, contents, filename, instructions, api_key):
|
| 160 |
+
if n_clicks is None or contents is None:
|
| 161 |
+
return dash.no_update, dash.no_update, dash.no_update, ""
|
| 162 |
+
|
| 163 |
+
if not api_key:
|
| 164 |
+
return dash.no_update, dash.no_update, dash.no_update, "Please enter a valid API key."
|
| 165 |
|
| 166 |
+
try:
|
| 167 |
+
df = parse_contents(contents, filename)
|
| 168 |
+
if df is None:
|
| 169 |
+
return dash.no_update, dash.no_update, dash.no_update, "Unable to parse the uploaded file."
|
| 170 |
|
| 171 |
+
plots = process_data(df, instructions, api_key)
|
| 172 |
+
if plots is None or len(plots) < 3:
|
| 173 |
+
return dash.no_update, dash.no_update, dash.no_update, "Unable to generate visualizations. Please check your instructions and try again."
|
| 174 |
|
| 175 |
+
figures = [generate_plot(df, plot_info) for plot_info in plots[:3]]
|
| 176 |
+
return figures[0], figures[1], figures[2], ""
|
| 177 |
+
except Exception as e:
|
| 178 |
+
error_message = f"An error occurred: {str(e)}"
|
| 179 |
+
return dash.no_update, dash.no_update, dash.no_update, error_message
|
| 180 |
|
| 181 |
if __name__ == '__main__':
|
| 182 |
app.run(debug=True, host='0.0.0.0', port=7860, threaded=True)
|