Skip to content

Instantly share code, notes, and snippets.

@asehmi
Created November 29, 2021 00:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asehmi/35db009466a010bd1c89d5346a71aea2 to your computer and use it in GitHub Desktop.
Save asehmi/35db009466a010bd1c89d5346a71aea2 to your computer and use it in GitHub Desktop.
Using pandas profiling with streamlit_pandas_profiling component in Streamlit
import streamlit as st
import pandas_profiling
from streamlit_pandas_profiling import st_profile_report
import pandas as pd
st.set_page_config(
page_title='Data Profiler',
layout='wide',
page_icon='🔍'
)
state = st.session_state
if 'profile_report' not in state:
state['profile_report'] = None
def generate_profile_report(data_file, delimiter, minimal):
file_info = {"Filename": data_file.name, "FileType": data_file.type, "FileSize": data_file.size}
st.write(file_info)
df = pd.read_csv(data_file, sep=delimiter, engine="python")
pr = df.profile_report(lazy=True, minimal=minimal)
state.profile_report = {'data': df, 'pr': pr}
def file_handler():
seperators = {" ": " ", "pipe (|)": "|", r"tab (\t)": "\t", "comma (,)":",", "semicolon (;)":";"}
file_upload_form = st.form(key="file_upload")
with file_upload_form:
data_file = st.file_uploader("Upload CSV File", type=['csv'], key="upload")
delimiter = seperators[st.selectbox("Select delimiter", seperators.keys(), key="delims")]
minimal = st.checkbox('Minimal report', value=True)
if file_upload_form.form_submit_button(label='Submit') and data_file:
generate_profile_report(data_file, delimiter, minimal)
if (data_file and delimiter and state.profile_report):
data = st.session_state.profile_report['data']
pr = st.session_state.profile_report['pr']
col = st.selectbox("Select column for summary stats", options=['']+list(data.columns))
if col != '':
st.write(pd.DataFrame(data[col].unique(), columns=[col]).T)
st_profile_report(pr)
else:
st.info('Please upload a CSV data file, select a delimiter and hit submit.')
state.profile_report = None
if __name__ =="__main__":
file_handler()
@asehmi
Copy link
Author

asehmi commented Nov 29, 2021

Demo
pandas_profiling_demo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment