Issue working with streamlit fragments and forms

Hi there, hope you are good.
I am stuck in a situation where I have a structure like this.

Here I ave used text-input, selectbox and form_submit_button inside st.expermimental_fragment. And all of this is present inside a container.

Now when I am clicking this checked button it says “could not find fragment”. I dont know what i am missing, can anyone has any clue why this might be happening??
RuntimeError: Could not find fragment with id 33ceabc972d74023f8209621d7ce3d5e
Because of this error, my form is not getting form_submit_button and hence the following error.

# this is just an exact replica of the main problem that I am solving

import streamlit as st
def myfunc():   # this function is called from login page if login  is successful
    @st.experimental_fragment
    def fragment(k):
        with st.form(str(k)+"check"):
            status_update_cols = st.columns(3)
            comment = status_update_cols[0].text_input("Comment",key=str(k)+"comment")
            call_type = status_update_cols[1].selectbox(label="Call Type:",options=["A","B","C"],key=str(k)+"calltype")
            check = status_update_cols[2].form_submit_button("Checked")

    def keywords(s,e):
        count = 1
        for i in range(s,e):
            with st.container():
                st.write(f"This is {i} container")
                fragment(count)
            count+=1
    but = st.button("Click me",key="key")
    but2 = st.button("Click me",key="key2")
    if but:
        keywords(0,5)
    if but2:
        keywords(0,5)

streamlit # streamlitform #fragment #experimental_fragment

I tried your code and it worked just fine.

# your code here
# def myfunc():
#     blah

# Lines I added.
is_logged_in = True
if is_logged_in:
    myfunc()

experimental fragment has an interesting description.

Decorator to turn a function into a fragment which can rerun independently of the full script.

When a user interacts with an input widget created by a fragment, Streamlit only reruns the fragment instead of the full script. 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.

To trigger a full script rerun from inside a fragment, call st.rerun() directly. Any values from the fragment that need to be accessed from the wider app should generally be stored in Session State.

When Streamlit element commands are called directly in a fragment, the elements are cleared and redrawn on each fragment rerun, just like all elements are redrawn on each full-script rerun. The rest of the app is persisted during a fragment rerun. When a fragment renders elements into externally created containers, the elements will not be cleared with each fragment rerun. In this case, elements will accumulate in those containers with each fragment rerun, until the next full-script rerun.

Calling st.sidebar in a fragment is not supported. To write elements to the sidebar with a fragment, call your fragment funciton inside a with st.sidebar context manager.

Fragment code can interact with Session State, imported modules, and other Streamlit elements created outside the fragment. Note that these interactions are additive across multiple fragment reruns. You are responsible for handling any side effects of that behavior.

1 Like

yes, its working fine here in this sample which I shared. But similar structure is not giving me desired result in my another code I have lots of containers and text inside them.
So I just wanted to know if my flow of code is correct or not with this sample. Am I doing aything wrong with fragments? And also please let me know when do we get fragment id not found error? That error is still not resolved… I have gone through the fragments documentation.

You can post a minimal code that reproduces the issue.

Yes, even I encountered the issue of fragment id not found. It’s not always replicable but the error is thrown while passing run_every parameter to experimental_fragment decorator!