-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoverview_parameters.py
108 lines (96 loc) · 4.13 KB
/
overview_parameters.py
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
import streamlit as st
import pandas as pd
def filter_string(df, column, selected_list):
final = []
df = df[df[column].notna()]
for idx, row in df.iterrows():
if row[column] in selected_list:
final.append(row)
res = pd.DataFrame(final)
return res
def number_widget(df, column, ss_name):
df = df[df[column].notna()]
max = float(df[column].max())
min = float(df[column].min())
temp_input = st.sidebar.slider(f"{column.title()}", min, max, (min, max), key=ss_name)
all_widgets.append((ss_name, "number", column))
def number_widget_int(df, column, ss_name):
df = df[df[column].notna()]
max = int(df[column].max())
min = int(df[column].min())
temp_input = st.sidebar.slider(f"{column.title()}", min, max, (min, max), key=ss_name)
all_widgets.append((ss_name, "number", column))
def create_select(df, column, ss_name, multi=False):
df = df[df[column].notna()]
options = df[column].unique()
options.sort()
if multi==False:
temp_input = st.sidebar.selectbox(f"{column.title()}", options, key=ss_name)
all_widgets.append((ss_name, "select", column))
else:
temp_input = st.sidebar.multiselect(f"{column.title()}", options, key=ss_name)
all_widgets.append((ss_name, "multiselect", column))
def text_widget(df, column, ss_name):
temp_input = st.sidebar.text_input(f"{column.title()}", key=ss_name)
all_widgets.append((ss_name, "text", column))
def create_widgets(df, create_data={}, ignore_columns=[]):
"""
This function will create all the widgets from your Pandas DataFrame and return them.
df => a Pandas DataFrame
create_data => Optional dictionary whose keys are the Pandas DataFrame columns
and whose values are the type of widget you wish to make.
supported: - multiselect, select, text
ignore_columns => columns to entirely ignore when creating the widgets.
"""
for column in ignore_columns:
df = df.drop(column, axis=1)
global all_widgets
all_widgets = []
for ctype, column in zip(df.dtypes, df.columns):
if column in create_data:
if create_data[column] == "text":
text_widget(df, column, column.lower())
elif create_data[column] == "select":
create_select(df, column, column.lower(), multi=False)
elif create_data[column] == "multiselect":
create_select(df, column, column.lower(), multi=True)
# ## Customer date filter
# elif create_data[column] == "date":
# start_date, end_date = date_widget(df, column, column.lower())
else:
if ctype == "float64":
number_widget(df, column, column.lower())
elif ctype == "int64":
number_widget_int(df, column, column.lower())
elif ctype == "object":
if str(type(df[column].tolist()[0])) == "<class 'str'>":
text_widget(df, column, column.lower())
return all_widgets
def filter_df(df, all_widgets):
"""
This function will take the input dataframe and all the widgets generated from
Streamlit Pandas. It will then return a filtered DataFrame based on the changes
to the input widgets.
df => the original Pandas DataFrame
all_widgets => the widgets created by the function create_widgets().
"""
res = df
for widget in all_widgets:
ss_name, ctype, column = widget
data = st.session_state[ss_name]
if data:
if ctype == "text":
if data != "":
res = res.loc[res[column].str.contains(data)]
elif ctype == "select":
res = filter_string(res, column, data)
elif ctype == "multiselect":
res = filter_string(res, column, data)
elif ctype == "number":
min, max = data
res = res.loc[(res[column] >= min) & (res[column] <= max)]
# ## Customer date filter
# elif ctype == "date":
# start_date, end_date = data
# res = filter_date_range(res, column, start_date, end_date)
return res