pythonstreamlit

My modal doesn't appear after an action in Streamlit


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?


Solution

  • 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:

    enter image description here