Final_Project / utils.py
friedeggy's picture
Upload 14 files
a7bac1c verified
Raw History Blame Contribute Delete
4.73 kB
"""
Ashley Wu
Creates visualizations for app
"""
import gradio as gr
from datetime import datetime
import pandas as pd
def update_dropdown(df):
"""
Updates the dropdown with the columns of the dataframe in question
Args:
df: dataframe
the dataframe in question
Returns:
gr.update
Gradio update object to dropdown
"""
columns = df.columns.tolist()
return gr.update(choices=columns)
def update_filter_block(df, column_title):
"""
Chooses whick blocks to hide and show based off of column dtype
Args:
df: dataframe
dataframe in question
column_title: string
title of the column
returns:
gr.update: gr.update
an update to the visibility of the max number textbox
gr.update: gr.update
an update to the visibility of the min number textbox
gr.update: gr.update
an update to the visibility of the category checklist
gr.update: gr.update
an update to the visibility of the max date selector
gr.update: gr.update
an update to the visibility of the min date selector
string: string
a string update incase use selects a dtype that does not have a filter
"""
df = detect_categories(df)
df = detect_dates(df)
dtype = df[column_title].dtype
max_num = False
min_num = False
category = False
max_date = False
min_date = False
if dtype.kind == 'i' or dtype.kind == 'f' or dtype.kind == 'u' or dtype.kind == 'c':
max_num = True
min_num = True
elif dtype.name == 'category':
category = True
elif dtype.kind == 'M' or dtype.kinda == 'm':
max_date = True
min_date = True
else:
raise gr.Warning('selected column is not a filterable type!', duration=5)
return gr.update(visible=max_num), gr.update(visible=min_num), gr.update(visible=category), gr.update(visible=max_date), gr.update(visible=min_date)
def apply_filter(df):
"""
Docstring for apply_filter
:param df: Description
"""
return df
def detect_categories(df):
"""
Detects whether or not there are categories in a string column
Args:
df: dataframe
Returns:
df: dataframe
"""
for column in df.columns:
unique = df[column].nunique()
if unique < 100 and df[column].dtype == 'object':
df[column] = df[column].astype('category')
return df
def populate_categories(df, column):
"""
Populate categories of an element
Args:
df: dataframe
column: column to get categories for
Returns:
list of strings: list of categories in column
"""
list = df[column].unique().tolist()
return gr.update(choices=list)
def change_slider(df, col):
if df[col].dtype.name == 'object':
raise gr.Error("The column can not be a category!")
max_val = df[col].max()
min_val = df[col].min()
return gr.update(maximum=max_val, minimum=min_val, step=1)
def count_rows(df):
"""
Return the row count of the dataframe
Args:
df: dataframe
Returns:
int: # of the rows of the dataframe
"""
return df.shape[0]
def detect_dates(df):
"""
Checks every column for date values, if a column with dates is found, then sets that column dtype to a date value
Args:
df: dataframe
Returns:
df: dataframe
"""
formats = [
'%Y-%m-%d',
'%m/%d/%Y',
'%d/%m/%Y',
'%Y/%m/%d',
'%d-%m-%Y',
'%d.%m.%Y',
]
for column in df.columns:
for fmt in formats:
try:
datetime.strptime(df[column].iloc[0], fmt)
df[column] = pd.to_datetime(df[column])
break
except:
continue
return df
def download_chats(fig1, fig2, fig3, fig4):
"""
Downloads all figures, if figure hasn't been generated, doesn't download
Args:
fig1: bar chart
fig2: time series
fig3: histogram
fig4: scatter plot
"""
if fig1 is not None:
fig1.savefig("bar_chart.png")
if fig2 is not None:
fig2.savefig("time_series.png")
if fig3 is not None:
fig3.savefig("histogram.png")
if fig4 is not None:
fig4.savefig("scatter_plot.png")
pass