Skip to content

Instantly share code, notes, and snippets.

@justinhchae
Created January 25, 2021 14:47
Show Gist options
  • Save justinhchae/f2a1c1869d75b7861a6e30ef3f7cb7c8 to your computer and use it in GitHub Desktop.
Save justinhchae/f2a1c1869d75b7861a6e30ef3f7cb7c8 to your computer and use it in GitHub Desktop.
import pandas as pd
import streamlit as st
from collections import defaultdict
from streamlit.report_thread import get_report_ctx
# implementation
# https://share.streamlit.io/justinhchae/app_helper/main/app.py
class SomeClass():
def __init__(self):
pass
def standardizer(self, df):
if df is not None:
types = df.dtypes
test = list(zip(types, types.index))
target_cols = []
for type_id, col_name in test:
if 'category' in str(type_id):
target_cols.append(col_name)
if target_cols:
select_cols = st.selectbox('Standardize Columns', target_cols)
col1, col2, col3 = st.beta_columns(3)
with col1:
st.write('Unique values in:', str(select_cols))
st.markdown(
"<h4 style='text-align: center; color: black;font-family:courier;'> all unique values in the column </h4>",
unsafe_allow_html=True)
st.write('')
categories = list(df[select_cols].dropna().astype('object').unique())
checkers = {}
for index, value in enumerate(categories):
checker = st.checkbox(value, value=True, key=str('std_' + str(index)))
checkers.update({value: checker})
with col2:
st.write('Standardize:', str(select_cols))
st.markdown(
"<h4 style='text-align: center; color: black;font-family:courier;'> enter new values -> click Save Changes </h4>",
unsafe_allow_html=True)
display_value = [key for key, value in checkers.items() if value is True]
self.changers = {}
for index, value in enumerate(display_value):
changer = st.text_input(value, value=value)
self.changers.update({value: changer})
with col3:
st.write('Actions:')
st.markdown(
"<h4 style='text-align: center; color: black;font-family:courier;'> save, clear, view changes </h4>",
unsafe_allow_html=True)
@st.cache(allow_output_mutation=True, persist=True)
def persist_list():
return []
@st.cache(allow_output_mutation=True, persist=True, ttl=60*5)
def persist_dict():
return defaultdict(list)
standardize_values_dict = persist_dict()
if self.changers:
if st.button('Save Changes'):
try:
records = tuple((str(select_cols), self.changers))
curr_cols = [str(val[0]) for _, val in enumerate(standardize_values_dict[get_report_ctx().session_id])]
if curr_cols and any(x == str(select_cols) for x in curr_cols):
st.write('Saved over previous values')
new_list = [i for i in standardize_values_dict[get_report_ctx().session_id] if
i[0] != str(select_cols)]
standardize_values_dict[get_report_ctx().session_id] = new_list
standardize_values_dict[get_report_ctx().session_id].append(records)
st.write('Added new values for', str(select_cols))
except:
st.write('commit error, refresh and try again')
if st.button('Clear All Changes', key='clear_cache2'):
try:
standardize_values_dict.pop(get_report_ctx().session_id)
st.write('Cleared Changes in Current Session')
except:
pass
if st.button('View Current Changes', key='view_changes'):
if standardize_values_dict:
st.write(standardize_values_dict[get_report_ctx().session_id])
else:
st.write('No Changes Yet')
self.standardize_values = standardize_values_dict
else:
st.write('No columns to standardize, skip to next section.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment