admin管理员组文章数量:1406040
I have this Streamlit app:
import streamlit as st
st.title("Simulator")
tab_names = ["tab1", "tab2"]
tab1, tab2= st.tabs(tab_names)
@st.dialog("Edit your relationships")
def edit_relationships(result):
edit_options = tuple(result)
selection = st.selectbox(
"Select an entity relationship",
edit_options
)
st.write(f"This is a dialog {selection}")
if st.button("Submit"):
st.session_state.vote = 'pear'
st.rerun()
with tab1:
st.write("This is the first tab")
with tab2:
query = st.text_input("Enter the entity", key='t2tinput')
if st.button('send', key='t2button'):
try:
result = ['banana', 'apple', 'pear']
if st.button("Edit Relationships"):
edit_relationships(result)
except Exception as e:
st.error(f"Error: {e}")
And I want that after the result
list is received (from an API), that a button 'Edit relationships" appear so I can click on it and a modal appears. I tried this code but after clicking on 'Edit relationships', the modal doesn't appear.
Please, could you point out what I am doing wrong?
I have this Streamlit app:
import streamlit as st
st.title("Simulator")
tab_names = ["tab1", "tab2"]
tab1, tab2= st.tabs(tab_names)
@st.dialog("Edit your relationships")
def edit_relationships(result):
edit_options = tuple(result)
selection = st.selectbox(
"Select an entity relationship",
edit_options
)
st.write(f"This is a dialog {selection}")
if st.button("Submit"):
st.session_state.vote = 'pear'
st.rerun()
with tab1:
st.write("This is the first tab")
with tab2:
query = st.text_input("Enter the entity", key='t2tinput')
if st.button('send', key='t2button'):
try:
result = ['banana', 'apple', 'pear']
if st.button("Edit Relationships"):
edit_relationships(result)
except Exception as e:
st.error(f"Error: {e}")
And I want that after the result
list is received (from an API), that a button 'Edit relationships" appear so I can click on it and a modal appears. I tried this code but after clicking on 'Edit relationships', the modal doesn't appear.
Please, could you point out what I am doing wrong?
Share Improve this question edited Mar 13 at 8:30 user5127 16512 bronze badges asked Mar 6 at 15:36 AlexisAlexis 2,2662 gold badges25 silver badges51 bronze badges2 Answers
Reset to default 3 +50It seems you need st.fragment:
When a user interacts with an input widget created inside a fragment, Streamlit only reruns the fragment instead of the full app. If run_every is set, Streamlit will also rerun the fragment at the specified interval while the session is active, even if the user is not interacting with your app.
Working with fragments:
Reruns are a central part of every Streamlit app.When users interact with widgets, your script reruns from top to bottom, and your app's frontend is updated. Streamlit provides several features to help you develop your app within this execution model. Streamlit version 1.37.0 introduced fragments to allow rerunning a portion of your code instead of your full script. As your app grows larger and more complex, these fragment reruns help your app be efficient and performant. Fragments give you finer, easy-to-understand control over your app's execution flow.
Full code:
import streamlit as st
st.title("Simulator")
tab_names = ["tab1", "tab2"]
tab1, tab2= st.tabs(tab_names)
@st.dialog("Edit your relationships")
def edit_relationships(result):
edit_options = tuple(result)
selection = st.selectbox(
"Select an entity relationship",
edit_options
)
st.write(f"This is a dialog {selection}")
if st.button("Submit"):
st.session_state.vote = 'pear'
st.rerun()
@st.fragment
def edit_btn():
if st.button("Edit Relationships"):
edit_relationships(result)
with tab1:
st.write("This is the first tab")
with tab2:
query = st.text_input("Enter the entity", key='t2tinput')
if st.button('send', key='t2button'):
try:
result = ['banana', 'apple', 'pear']
edit_btn()
except Exception as e:
st.error(f"Error: {e}")
Output:
Use a while
to check if the button is pressed. The if
statement wil evaluate to False
, so it will just skip over it and continue. Your code should be like this:
import streamlit as st
btn = st.button("Click here")
while not btn:
pass
# modal code here
"modal"
本文标签: pythonMy modal doesn39t appear after an action in StreamlitStack Overflow
版权声明:本文标题:python - My modal doesn't appear after an action in Streamlit - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744965448a2634919.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论