0
1

More than 1 year has passed since last update.

StreamlitでEDAライブラリを複数つなげてみた

Last updated at Posted at 2023-09-15

要約

業務でEDAを利用するケースが多いのですが、jupyter実行して確認して、というのが煩わしいという声もあったので、ブラウザ上で複数確認できるようStreamlitを利用して作りました。

事前準備

必要なライブラリはpandas streamlit sweetviz pyGWalkerです。
pip install pandas streamlit sweetviz pygwalker をしてください。

ソースコード

import pandas as pd
import pygwalker as pyg
import streamlit as st
import streamlit.components.v1 as components
import sweetviz as sv
import os
import codecs
import getpass

st.set_page_config(layout="wide")

def load_data(uploaded_file):
    df = pd.read_csv(uploaded_file)
    return df

def st_display_sweetviz(report_html, height:int=1200):
    report_file = codecs.open(report_html, 'r', encoding='utf8')
    page = report_file.read()
    components.html(page, width=None, height=height, scrolling=True)

st.subheader("Data Analysis")
train_df = None
test_df = None
pyg_html = None

tab_titles = ["pyGWalker", "sweet_viz"]
tabs = st.tabs(tab_titles)
with st.sidebar:
    uploaded_files = st.file_uploader("Choose a CSV file")
    if uploaded_files is not None:
        train_df = load_data(uploaded_files)
    uploaded_compare = st.file_uploader("Choose a CSV file to compare(sweetviz only)")
    if uploaded_compare is not None:
        test_df = load_data(uploaded_compare)

with tabs[0]:
    try:
        if train_df is not None:
            pyg_html = pyg.walk(train_df, return_html=True)
            components.html(pyg_html, height=1200, scrolling=True)
    except Exception as e:
        st.write(e)

with tabs[1]:
    try:
        usrname = getpass.getuser()
        target_path = os.path.join(".", usrname)
        sv.config_parser.read_string("[General]\nuse_cjk_font=1")

        # 1つのCSV(train)データのみの場合
        if train_df is not None and test_df is None:
            out_file = os.path.join(target_path, "SWEETVIZ_REPORT.html")
            if not os.path.exists(target_path):
                os.makedirs(target_path)
            analysis = sv.analyze(train_df)
            analysis.show_html(filepath=out_file, open_browser=False, layout='vertical', scale=1.0)
            st.display_sweetviz(out_file)
        # 2つのCSV(ex. trainとtest、二値分類を分けた結果)の場合
        elif (train_df is not None) and (test_df is not None):
            out_file = os.path.join(target_path, "SWEETVIZ_REPORT_COMPARE.html")
            if not os.path.exists(target_path):
                os.makedirs(target_path)
            analysis_com = sv.compare([train_df, "train"], [test_df, "test"])
            analysis_com.show_html(filepath=out_file, open_browser=False, layout='vertical', scale=1.0)
            st_display_sweetviz(out_file)
    except Exception as e:
        st.write(e)

実行

Python ファイルを以下のように実行してください。
streamlit run ファイル名.py

補足

  • 動かすとサイドバーに2種類のファイルアップローダー画面があります。
    上がいわゆるEDAしたいメインファイルやtrainデータです。

  • もし2つのファイルを比較したい場合は下のアップローダにもファイルをアップロードしてください。
    sweetviz側で2つのファイルを比較した結果を出力します。

  • EDAはタブで切り替えられます。
    sweetvizのほうが読み込みに時間がかかるので、まずはpyGWalkerを表示させるようにしました。

  • 実行結果

    • pygwalker: UI上にExportボタンがあります。
      * sweetviz: ファイル保存場所にユーザー名のフォルダを自動生成し、その中にhtmlを生成します。
  • 最低限の機能しか入れてないため、ここから拡張する前提のソースコードです。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1