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 badges
Add a comment  | 

2 Answers 2

Reset to default 3 +50

It 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