File size: 12,681 Bytes
a7bac1c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
"""

Ashley Wu 

Main application 

"""
import gradio as gr
import utils
import insights 
import data_processor as dp
import visualizations as vis

def create_gradio_interface():
    """

    Create a Gradio interface for the mosaic generator.

    """


    # Create Gradio interface
    with gr.Blocks(title="Buisiness Dashboard") as demo:
        gr.Markdown("# 🎨 Buisiness Dashboard")
        gr.Markdown("Upload a csv or txt file (in the format of a csv file)!")


        with gr.Tab("Data Upload"):
            # File upload and preview
            with gr.Row():
                file_input = gr.File(
                    label="Upload CSV File",
                    file_types=[".csv", ".txt"],
                    type="filepath"
                )
                file_upload_textbox = gr.Textbox(
                    label="CSV Quick Data",
                    lines=25
                )  
            with gr.Row():
                output = gr.Dataframe(
                    label="CSV Data - Edit or Delete Rows",
                    interactive=True
                )
            
            """Download button"""
            with gr.Row():
                download_btn = gr.DownloadButton(
                    label="Download Modified CSV",
                    visible=True
                )
        
        with gr.Tab("Filter & Explore"):
            # Interactive filtering
            with gr.Row():
                with gr.Column():
                    column_dropdown = gr.Dropdown(
                        label="Select Column to Filter",
                        choices=[],
                        interactive=True
                    )
                    with gr.Row():
                        apply_button = gr.Button("Apply Filter")
                        row_count = gr.Number(
                            label="Rows in dataframe",
                            visible=True,
                            interactive=False
                        )
                
                max_num_textbox = gr.Number(
                    label="Set max value of column",
                    visible = False
                )
                min_num_textbox = gr.Number(
                    label="Set min value of column",
                    visible = False
                )
                category_multiselect = gr.Dropdown(
                    label="Select elements you want to filter out",
                    choices=[],
                    multiselect=True,
                    interactive=True,
                    visible=False
                )
                with gr.Column():
                    with gr.Row():
                        max_date_textbox = gr.Textbox(
                            label="Set max date of the column (YYYY-MM-DD)",
                            visible = False
                        )
                        min_date_textbox = gr.Textbox(
                            label="Set min date of column (YYYY-MM-DD)",
                            visible = False
                        )
                    submit_dates = gr.Button("Submit Dates")

            with gr.Row():
                preview_df = gr.Dataframe(
                    label="Preview of filtered dataframe",
                    interactive=False
                )



        with gr.Tab("Statistics"):
            # Summary statistics and profiling
            with gr.Column():
                numerical_data = gr.Dataframe(
                    label="Numerical Column Statistics",
                    interactive=False
                )
                categorical_data = gr.Dataframe(
                    label="Categorical Column Statistics",
                    interactive=False
                )
                correlation_matrix= gr.Dataframe(
                    label="Correlation matrix of dataframe",
                    interactive=False
                )
        
        with gr.Tab("Visualizations"):
            # Charts and graphs
            with gr.Column():
                time_series = gr.Plot(
                        label="Time Series"
                        ) 
                with gr.Row():
                    x_time = gr.Dropdown(
                        label = "X axis column",
                        choices=[],
                        interactive=True,
                        )
                    y_time = gr.Dropdown(
                        label ="Y axis column",
                        choices=[],
                        interactive=True,
                        )
                    generate_time = gr.Button("Generate Time Series Graph")


            #Histogram
            with gr.Column():
                histogram = gr.Plot(
                        label="Histogram"
                        ) 
                with gr.Row():
                    hist_col = gr.Dropdown(
                        label = "Histogram variable",
                        choices=[],
                        interactive=True,
                        )
                    hist_bin = gr.Slider(
                        label ="Bins",
                        maximum=1,
                        minimum=0,
                        step=1,
                        interactive=True,
                        )
                    generate_hist = gr.Button("Generate Graph")

            #Bar chart 
            with gr.Column():
                bar_chart = gr.Plot(
                        label="Bar chart"
                        ) 
                with gr.Row():
                    bar_x = gr.Dropdown(
                        label = "X axis variable",
                        choices=[],
                        interactive=True,
                        )
                    bar_y = gr.Dropdown(
                        label ="Y axis variable",
                        choices=[],
                        interactive=True,
                        )
                
                    with gr.Column():
                        gr.Markdown("Count does not require a column in the Y axis")

                        agg_method = gr.Dropdown(
                            label ="How you want categories to be aggregated",
                            choices=['sum', 'mean', 'median', 'count'],
                            interactive=True,
                            )
                
                    generate_bar = gr.Button("Generate Graph")        

            #Scatter plot
            with gr.Column():
                scatter_plot = gr.Plot(
                        label="Scatter plot"
                        ) 
                with gr.Row():
                    scatter_x = gr.Dropdown(
                        label = "X axis variable",
                        choices=[],
                        interactive=True,
                        )
                    scatter_y = gr.Dropdown(
                        label ="Y axis variable",
                        choices=[],
                        interactive=True,
                        )
                    generate_scatter = gr.Button("Generate Graph")

            download_plots = gr.DownloadButton(
                    label="Download all graphs",
                    visible=True
                )

        
        with gr.Tab("Insights"):
            # Automated insights
            max_min_df = gr.DataFrame(
                label='Data Insights',
                interactive=False
            )
            std_df = gr.DataFrame(
                label='Standard Devation of Values',
                interactive=False
            )    
            var_df = gr.DataFrame(
                label='Variance of Values',
                interactive=False
            ) 

        

        # Connect the upload to the processing function 
        file_input.change(
            fn=dp.process_csv,
            inputs=file_input,
            outputs=[output, file_upload_textbox] 
        )
    
        """Quick Info""" 
        #Automatically update statistics calculations when dataframe is modified
        
        output.change(
            fn=insights.calculate_stats,
            inputs=output,
            outputs=[numerical_data, categorical_data]
        )
        output.change(
            fn=insights.correlation_matrix,
            inputs=output,
            outputs=correlation_matrix
        )

        """Filter Elements"""

        # update the dropdown menu whenever the dataframe is changed
        output.change(
            fn=utils.update_dropdown,
            inputs=output,
            outputs=column_dropdown
        )

        # Create visibility changes when item in dropdown is selected
        column_dropdown.change(
            fn=utils.update_filter_block,
            inputs=[output, column_dropdown],
            outputs=[max_num_textbox, min_num_textbox, category_multiselect, max_date_textbox, min_date_textbox] 
        )

        max_num_textbox.change(
            fn=dp.set_max,
            inputs=[max_num_textbox, output, column_dropdown],
            outputs=[preview_df]
        )

        min_num_textbox.change(
            fn=dp.set_min,
            inputs=[min_num_textbox, output, column_dropdown],
            outputs=[preview_df]
        )

        column_dropdown.change(
            fn=utils.populate_categories,
            inputs=[output, column_dropdown],
            outputs=[category_multiselect]
        )
        
        category_multiselect.change(
            fn=dp.set_categories,
            inputs=[category_multiselect, output, column_dropdown],
            outputs=[preview_df]
        )

        preview_df.change(
            fn=utils.count_rows,
            inputs=preview_df,
            outputs=row_count
        )

        submit_dates.click(
            fn=dp.submit_dates,
            inputs=[output, max_date_textbox, min_date_textbox, column_dropdown],
            outputs=preview_df
        )

        apply_button.click(
            fn=utils.apply_filter,
            inputs=preview_df,
            outputs=output
        )

        """Visualization Elements"""

        dropdown_pairs = [
            (x_time, y_time),
            (bar_x, bar_y),
            (scatter_x, scatter_y)
        ]

        for drop_x, drop_y in dropdown_pairs:
            output.change(
                fn=utils.update_dropdown,
                inputs=output,
                outputs=drop_x
            )

            output.change(
                fn=utils.update_dropdown,
                inputs=output,
                outputs=drop_y
            )
            

        output.change(
            fn=utils.update_dropdown,
            inputs=output,
            outputs=hist_col
        )

        hist_col.change(
            fn=utils.change_slider,
            inputs=[output, hist_col],
            outputs=hist_bin
        )

        button_plot_pairs = [(generate_time, time_series, vis.time_series, x_time, y_time), (generate_hist, histogram, vis.histogram, hist_col, hist_bin), (generate_scatter, scatter_plot, vis.scatter, scatter_x, scatter_y)]

        for button, plot, graph_func, x, y in button_plot_pairs:
            button.click(
                fn=graph_func,
                inputs=[output, x, y], 
                outputs=plot
            )

        generate_bar.click(
                fn=vis.bar_chart,
                inputs=[output, bar_x, bar_y, agg_method], 
                outputs=bar_chart
            )
        
        download_plots.click(
            fn=utils.download_chats,
            inputs=[bar_chart, time_series, histogram, scatter_plot]
        )
        
        """Insight Generation"""
        output.change(
            fn=insights.max_min_df,
            inputs=output,
            outputs=max_min_df
        )
        output.change(
            fn=insights.std,
            inputs=output,
            outputs=std_df
        )
        output.change(
            fn=insights.var,
            inputs=output,
            outputs=var_df
        )

        # Connect the download button to save function
        output.change(
            fn=dp.save_csv,
            inputs=output,
            outputs=download_btn
        )


    return demo

if __name__ == "__main__":
    # Create and launch the Gradio interface
    ui = create_gradio_interface()
    ui.launch()