【问题标题】:Why does my streamlit app reset on using the radio button?为什么我的 streamlit 应用程序在使用单选按钮时会重置?
【发布时间】:2022-08-19 01:20:55
【问题描述】:

所以我有一个侧边栏。 When \"Go\" in the sidebar is selected, the main dashboard should update.这行得通。

What doesn\'t work is when the radio button (marketing consent) within the main dashboard is selected, the entire dashboard resets.它将恢复到原始视图,而不是选择“开始”后的样子。当单击单选按钮时,就好像“开始”按钮重置并被取消选中......

\"\"\"
# My first app
Here\'s our first attempt at using data to create a table:
\"\"\"

##Packages
from unicodedata import numeric
import streamlit as st
import numpy as np
import pandas as pd
from PIL import Image
import pandasql as ps
import altair as alt
import plotly.express as px


st.set_page_config(page_title = \"Dashboard\")
st.title(\"Dashboard ????\")


##Dataframe
df = pd.DataFrame({
    \'first\': [1, 2, 3, 4],
    \'second\': [10, 20, 30, 40],
    \'third\': [\'apple\', \'banana\', \'grape\', \'grape\'],
    \'fourth\': [\'walter\', \'skyler\', \'hank\', \'marie\']

    })

####
###Sidebar things
####

###Logo
with st.sidebar.container():

st.sidebar.title(\"Sidebar\")


with st.sidebar.container():
    add_selectbox = st.sidebar.multiselect(
        \'Choose some properties\',
        (df.third.unique())
    )

    fourth_box = st.sidebar.selectbox(
        \'Choose your country\',
        (df.fourth.unique()),
        index = 1
    )



####Page 1
if st.sidebar.button(\'Go!\'):

    with st.container():

        status  = st.radio(
            \"Marketing Consent Status\",
            (\'Yes\', \'All\'))
       

        df_iris = px.data.iris()
 

        if marketing_status == \'Yes\':
           fig_1 = px.bar(df_iris, x=\"sepal_width\", y=\"sepal_length\", color=\"species\",
                   hover_data=[\'petal_width\'], barmode = \'stack\')

            st.plotly_chart(fig_1, use_container_width=True)
        
        elif marketing_status == \'All\':
            st.write(\'Hello, *World!\')


else:

    with st.container():

        df_map = px.data.gapminder().query(\"year==2007\")
        fig_map = px.choropleth(df_map, locations=\"iso_alpha\",
                    color=\"lifeExp\", # lifeExp is a column of gapminder
                    hover_name=\"country\", # column to add to hover information
                    color_continuous_scale=px.colors.sequential.Plasma)

        st.plotly_chart(fig_map, use_container_width=True)

如您所见,当您使用单选按钮(Marketing Consent Button)出现后,它将恢复到主地图视图。

标签: python streamlit


【解决方案1】:

为了防止小部件交互导致整个应用程序重新运行,您可以使用提交按钮使您的小部件成为form 的一部分,例如:

with st.form("my_form"):
    st.write("Inside the form")
    slider_val = st.slider("Form slider")
    checkbox_val = st.checkbox("Form checkbox")

    # Every form must have a submit button.
    submitted = st.form_submit_button("Submit")
    if submitted:
        st.write("slider", slider_val, "checkbox", checkbox_val)

st.write("Outside the form")

【讨论】:

  • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接答案可能会失效。 - From Review
猜你喜欢
  • 1970-01-01
  • 2020-03-06
  • 2020-10-26
  • 2014-06-27
  • 1970-01-01
  • 1970-01-01
  • 2012-07-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多