Spaces:
Sleeping
Sleeping
File size: 4,729 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 | """
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 |