How to create dynamic form where dropdowns are shown/hidden based on previous dropdowns ?

I am new streamlit user. I am trying to create a form where the first field is a dropdown. Based upon what the user selects in this dropdown, I want other dropdowns to show up or get hidden. Till now, I was able to get it working only when I put these dropdowns outside the forms (as per docs, streamlit forms do not support dynamic content ?). How can I do this ?

    question_type = st.selectbox(
        "Question Type",
        ["General", "RAG", "Agent"],
        index=0,
        key="qa_question_type",
        help="Select 'Agent' for AIDA interactions, 'RAG' for Retrieval-Augmented Generation questions without involving an agent."
    )
    kb_value = None
    if question_type == "RAG":
        if kb_options:
            kb_value = st.selectbox("Knowledge Base", kb_options, index=0, key="qa_kb_value", help="Select the knowledge base for RAG questions.")
        else:
            st.warning("No knowledge bases found. Please add one in 'Add Agents & KB' page.")
    agent_value = None
    if question_type == "Agent":
        if agent_options:
            agent_value = st.selectbox("Agent", agent_options, index=0, key="aida", help="Select the appropriate agent.")
        else:
            st.warning("No agents found. Please add one in 'Add Agents & KB' page.")

    with st.form("add_qa_form", clear_on_submit=True):
        question = st.text_area("Question", value=st.session_state["question_input"], key="question_input")
        groundtruth = st.text_area("Ground Truth Answer", value=st.session_state["groundtruth_input"], key="groundtruth_input")

Basically, you don’t want a form here. If you want a change in one widget to affect others, the change has to happen outside of a form. So don’t use a form and instead just use a button to copy all the finalized values over to a set of variables or process them when the user is finished filling all of them out.

You might be interested in looking at fragments if you have some computationally intensive things you’re trying to isolate.