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?

streamlitで作成した表の特定列だけ書き込み禁止処理

Posted at

Webページに作成した表の任意列だけ書き込みできないようにしたい

下に示すような表をpandasデータフレームで作成して,Webブラウザ上にstreamlitを用いて表示させた時に,特定の列(この場合:名前,年齢,職業)をプルダウンメニューで選択して,編集できないようにしたい.

スクリーンショット 2025-04-04 6.10.16.jpg

以下がそのサンプルプログラム

edit_column_checkbox.py
# -*- coding: utf-8 -*-
import streamlit as st
import pandas as pd

# サンプルデータフレームの作成
data = {'名前': ['山田', '田中', '佐藤'],
        '年齢': [25, 30, 28],
        '職業': ['エンジニア', 'デザイナー', '営業']}
df = pd.DataFrame(data)

# 編集不可にする列を選択するチェックボックス
disabled_cols = st.multiselect('編集不可にする列を選択してください', df.columns)

# 表の表示と編集
edited_df = st.data_editor(df, disabled=disabled_cols)

# 編集結果の表示
st.write('編集結果:')
st.write(edited_df)

steramlitの実行方法: コマンドプロンプト(ターミナル)で,
  streamlit run edit_column_checkbox.py
を実行(入力してリターンキー)するとWebブラウザが起動し,上記のような編集を禁止したい選択プルダウンメニューが表示される.
選択したものは,編集できなくなる.

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?