如何在单击按钮时浏览列表
How can I navigate through a list on button click
我正在研究 Streamlit 如何使用单击按钮浏览列表
例子
我有一个列表
import streamlit as st
lst =['a','b','c']
next = st.button('next',key='next')
for i in lst:
if next:
st.write(i)
如何遍历列表以在单击按钮时打印列表的每个元素?上面的代码只是一次给出了所有元素,但我想举个例子:我按下按钮,它应该打印 'a'
然后再点击它应该打印 'b'
等等。
您可以使用按钮的 on_click 参数,如下所述:https://docs.streamlit.io/library/api-reference/widgets/st.button。
单击按钮后如何执行任何操作的示例:
import streamlit as st
# this function is called when btn is clicked
def handleClick():
# do anything here
print("button clicked")
# create the button and set the on_click `callable` (aka function without ()).
btn = st.button('next', key='next', on_click=handleClick)
如何迭代并在点击时显示列表元素的示例:
import streamlit as st
lst = [1, 2, 3]
def handleClick():
for el in lst:
print(el)
btn = st.button('next', key='next', on_click=handleClick)
如果您想为每次点击显示列表的不同元素,您需要跟踪下一个要显示的元素。如本例所示:
import streamlit as st
cindex = 0 # tracks index to use
lst = [1, 2, 3]
def handleClick():
print(lst[cindex])
# update the index to use each click
cindex = (cindex + 1) % len(lst)
btn = st.button('next', key='next', on_click=handleClick)
您想使用st.session_state
来存储要显示的列表的当前索引!在 the docs 中了解更多信息。这是一段有效的代码:
import streamlit as st
my_list = ["a", "b", "c"]
show_next = st.button("next")
# Initialize the current index
if "current_index" not in st.session_state:
st.session_state.current_index = 0
# Whenever someone clicks on the button
if show_next:
# Show next element in list
st.write(my_list[st.session_state.current_index])
# Update and store the index
st.session_state.current_index += 1
我正在研究 Streamlit 如何使用单击按钮浏览列表
例子
我有一个列表
import streamlit as st
lst =['a','b','c']
next = st.button('next',key='next')
for i in lst:
if next:
st.write(i)
如何遍历列表以在单击按钮时打印列表的每个元素?上面的代码只是一次给出了所有元素,但我想举个例子:我按下按钮,它应该打印 'a'
然后再点击它应该打印 'b'
等等。
您可以使用按钮的 on_click 参数,如下所述:https://docs.streamlit.io/library/api-reference/widgets/st.button。
单击按钮后如何执行任何操作的示例:
import streamlit as st
# this function is called when btn is clicked
def handleClick():
# do anything here
print("button clicked")
# create the button and set the on_click `callable` (aka function without ()).
btn = st.button('next', key='next', on_click=handleClick)
如何迭代并在点击时显示列表元素的示例:
import streamlit as st
lst = [1, 2, 3]
def handleClick():
for el in lst:
print(el)
btn = st.button('next', key='next', on_click=handleClick)
如果您想为每次点击显示列表的不同元素,您需要跟踪下一个要显示的元素。如本例所示:
import streamlit as st
cindex = 0 # tracks index to use
lst = [1, 2, 3]
def handleClick():
print(lst[cindex])
# update the index to use each click
cindex = (cindex + 1) % len(lst)
btn = st.button('next', key='next', on_click=handleClick)
您想使用st.session_state
来存储要显示的列表的当前索引!在 the docs 中了解更多信息。这是一段有效的代码:
import streamlit as st
my_list = ["a", "b", "c"]
show_next = st.button("next")
# Initialize the current index
if "current_index" not in st.session_state:
st.session_state.current_index = 0
# Whenever someone clicks on the button
if show_next:
# Show next element in list
st.write(my_list[st.session_state.current_index])
# Update and store the index
st.session_state.current_index += 1