0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

stliteを使って社内向けデータ分析アプリを配布する際の検証

Posted at

はじめに

社内向けのデータ分析アプリを配布する方法として、stliteを検証しました。stliteはStreamlitアプリをブラウザで動作させることができるツールです。

stliteのメリット

  • Streamlitの書き方でそのままWebアプリが作れる
  • Python環境のインストールが不要
  • 静的ファイルとして配布可能

サンプルコード

stliteバージョン

import streamlit as st
import pandas as pd

st.title("データ分析アプリ")

uploaded_file = st.file_uploader("CSVファイルを選択", type=['csv'])

if uploaded_file is not None:
    try:
        df = pd.read_csv(uploaded_file)
        st.write("データプレビュー")
        st.dataframe(df.head())
        
        numeric_cols = df.select_dtypes(include=['float64', 'int64']).columns
        if len(numeric_cols) > 0:
            selected_col = st.selectbox("分析する列を選択:", numeric_cols)
            if not df[selected_col].empty:
                st.line_chart(df[selected_col])
    except Exception as e:
        st.error(f"エラーが発生しました: {str(e)}")

HTMLファイル(stlite用)

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>データ分析アプリ</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@stlite/mountable@0.39.0/build/stlite.css">
</head>
<body>
    <div id="root"></div>
    <script src="https://cdn.jsdelivr.net/npm/@stlite/mountable@0.39.0/build/stlite.js"></script>
    <script>
        // Pythonコードを文字列として埋め込み
        const pythonCode = `...` // 上記のPythonコード

        stlite.mount({
            requirements: ['streamlit', 'pandas'],
            entrypoint: "app.py",
            files: {
                "app.py": pythonCode
            }
        }, document.getElementById("root"));
    </script>
</body>
</html>

検証で見つかった課題

1. 起動時間の問題

実際に計測したところ、以下のような結果となりました:

  • stliteバージョン: 10.03秒
  • 同等機能のJavaScriptバージョン: 0.01秒

この差は以下の要因によるものです:

  • stliteはブラウザ内でPython環境(Pyodide)を初期化
  • 必要なPythonパッケージのロード時間
  • Streamlitアプリケーションの初期化時間

2. ローカルファイルの扱い

  • 基本的にはhtmlファイルに書き込む
  • モジュール化したコードの管理が難しい
  • Chromeの設定でローカルファイルの読み込みを許可すれば、読み込めなくはない
    (ただし、ユーザーの手間が増える)
    bash --allow-file-access-from-files

3. 代替案としてのJavaScript実装

純粋なJavaScriptで実装した場合のメリット:

  • 高速な起動時間
  • ローカルファイルの取り扱いが容易
  • ブラウザのネイティブ機能を活用可能

結論

stliteは以下のような場合に適しています:

  • Streamlitの知識を活かしたい場合
  • 起動時間が重要でない場合
  • シンプルな構成で完結する場合

一方で、以下の場合は純粋なJavaScript実装を検討すべきです:

  • 高速な起動が必要な場合
  • ローカルファイルを多用する場合
  • 複雑なモジュール構成が必要な場合
0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?