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"
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744965449a4603651.html
评论列表(0条)