Streamlit 中的 Web 应用程序,每页有多个问题

Web app in Streamlit with multiple questions per page

我正在使用 Streamlit 开发一个网络应用程序,我正在寻找一种方法让每页有多个问题(见下文)。目前,如果用户回答第一个问题 回答第二个问题,应用程序会自动加载下一组问题。我该如何更改以便仅当用户对第一个问题 对第二个问题做出回复时才加载下一组问题?

我目前使用的逻辑如下:

st.table(Text_lines)
col1, col2 = st.columns([1,1])
with col3:
    if st.button('Option 1'):
        st.session_state.option1 = 1
with col4:
    if st.button('Option 2'):
        pass

st.table(Other_text_lines)
col3, col4 = st.columns([1,1])
with col3:
    if st.button('Sensible (Q)'):
        st.session_state.sensibility = 1
with col4:
    if st.button('Not sensible (W)'):
        pass

一种方法是通过表单来控制问题的加载。完成后用户将按下提交按钮。

例子

import streamlit as st


if 'num' not in st.session_state:
    st.session_state.num = 0


choices1 = ['no answer', 'manila', 'tokyo', 'bangkok']
choices2 = ['no answer', 'thailand', 'japan', 'philippines']

qs1 = [('What is the capital of Japan', choices1),
    ('What is the capital of Philippines', choices1),
    ('What is the capital of Thailand', choices1)]
qs2 = [('What country has the highest life expectancy?', choices2),
    ('What country has the highest population?', choices2),
    ('What country has the highest oil deposits?', choices2)]


def main():
    for _, _ in zip(qs1, qs2): 
        placeholder = st.empty()
        num = st.session_state.num
        with placeholder.form(key=str(num)):
            st.radio(qs1[num][0], key=num+1, options=qs1[num][1])
            st.radio(qs2[num][0], key=num+1, options=qs2[num][1])          
                      
            if st.form_submit_button():
                st.session_state.num += 1
                if st.session_state.num >= 3:
                    st.session_state.num = 0 
                placeholder.empty()
            else:
                st.stop()


main()

输出

按提交,将加载新的问题集。