Issues with using @st.experimental_fragment to build a dynamic form

Hello everyone,

I’m experiencing intermittent rendering issues with a dynamic form in my Streamlit app, running on the latest version (1.34.0). The form is supposed to display a series of questions with radio buttons and conditional text inputs. However, sometimes it only renders partially, such as only showing the last question (Q16), half of the form, or sometimes none at all, after selecting the “Not Applicable” option and entering text multiple times. This issue occurs both locally and on a sandbox.

Here is the simplified code snippet: Please feel free to copy it and test it in the sandbox or run it locally by saving it as the App.py file.

import streamlit as st

@st.experimental_fragment
def load_survey():
    for i in range(16):
        answer = st.radio(label=f"Q{i+1}", options=["Yes", "No", "Not Applicable"], horizontal=True, key=f"{1}_{i}")
        if answer == "Not Applicable":
            res = st.text_input("Please add your reasoning for selecting not applicable", value='', key=f"{1}_{i}_text")

load_survey()

Observed Behavior after clicking several Not applicable and answer explanations:

  • Randomly, only Q16 is rendered.
  • Sometimes only half of the form appears from the question that you answered last
  • Occasionally, no form appears at all after several interactions.

Could anyone please help identify why these rendering inconsistencies are occurring and suggest any potential fixes? Thank you in advance!

I tried it in sandbox, I have not encountered the issues you mentioned.

The bug happens intermittently. You probably have to try to answer all questions, select several not-applicable options, and fill in the explanations to reproduce the bug. I have attached a video showing how the bug looks on my end. Could you give it one more try by following the methods that I used in the video?

example via selecting a lot of not applicable

another example via frequently modifying answers

I ran the code in the sandbox too and after a few interactions, involving one or two “not applicable”, I got a blank screen. I tried several more times and everything worked as expected.

I wonder if the particular execution environment of the sandbox is related. It would be good if you can reproduce the problem by deploying the code as a standard app in in streamlit cloud.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.