Final2 / app.py
friedeggy's picture
Upload 14 files
6f6318e verified
Raw History Blame Contribute Delete
12.7 kB
"""
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", visible=False)
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, submit_dates]
)
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()