teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

追記

2021/10/29 02:50

投稿

8524ba23
8524ba23

スコア38352

answer CHANGED
@@ -1,3 +1,27 @@
1
1
  [Button inside button is resetting the page #931](https://github.com/streamlit/streamlit/issues/931)に記載されているとおり、ボタンが押されると、**そのコード全体が最初から再実行**されます。
2
2
  再実行時には、以前にどのボタンが押されたかは記憶していないため、期待した動作はできません。
3
- ただし[Streamlitの備忘録 状態の保持](https://zenn.dev/canard0328/articles/streamlit_how_to_use#%E7%8A%B6%E6%85%8B%E3%81%AE%E4%BF%9D%E6%8C%81)に記載されている`SessionState`を利用するとできるかもしれません。
3
+ ただし[Streamlitの備忘録 状態の保持](https://zenn.dev/canard0328/articles/streamlit_how_to_use#%E7%8A%B6%E6%85%8B%E3%81%AE%E4%BF%9D%E6%8C%81)に記載されている`session_state`を利用するとできるかもしれません。
4
+
5
+ 追記。Version 1.1.0でできました。
6
+ ```Python
7
+ import streamlit as st
8
+
9
+ i = 1
10
+ if 'i' in st.session_state:
11
+ i = st.session_state['i']
12
+
13
+ states = [
14
+ (f"{i}st button",f"{i}st button is pushed."),
15
+ (f"{i}nd button",f"{i}nd button is pushed."),
16
+ (f"{i}rd button","GOAL")
17
+ ]
18
+ title, mes = states[i-1]
19
+
20
+ f=st.button(title)
21
+ if f:
22
+ st.write(mes)
23
+ i += 1
24
+ if i > len(states):
25
+ i = 1
26
+ st.session_state['i'] = i
27
+ ```