Skip to content

Fragments within container disappearing on rerender #12514

@JosephMarinier

Description

@JosephMarinier

Checklist

  • I have searched the existing issues for similar issues.
  • I added a very descriptive title to this issue.
  • I have provided sufficient information below to help reproduce this issue.

Summary

I have an issue with st.fragments within a container disappearing on rerender. The initial render works as expected, but after the first user interaction within the fragment, the container seems to act somewhat like an st.empty().

It only happens under very particular conditions. I isolated the issue by simplifying my example as much as possible. Of course, that example now looks silly, but in my app, these "particular conditions" have reasons to be.

Reproducible Code Example

import streamlit as st


@st.fragment  # Has to be a fragment.
def a():
    st.button("Rerender `a()`")
    container = st.container()
    with container:
        b(1)
    with container:  # Has to be a separate `with`.
        b(2)


@st.fragment  # Has to be a fragment.
def b(i: int):
    st.markdown(i)


a()

Steps To Reproduce

  1. Try the above code example.
  2. On the initial render, you should see "1" and "2".
  3. Click the "Rerender a()" button.
  4. Notice the "1" is now gone.
  5. Variations:
    • If you add anything after with container: b(2), say container.markdown(3), the "2" will also disappear, leaving only the "3".
    • If you replace st.container() with st.columns(1)[0], st.expander(""), st.popover(""), or st.tabs(("Tab",))[0], the same issue happens.
  6. If you remove any one of the three lines with a comment, it avoids the issue. These three conditions are necessary to reproduce the issue.

Expected Behavior

Always seeing "1" and "2", even after a rerender.

Current Behavior

Initial render:
Image
After clicking the "Rerender a()" button:
Image

Is this a regression?

  • Yes, this used to work in a previous version.

Debug info

  • Streamlit version: 1.49.1
  • Python version: 3.11.9
  • Operating System: MacOS
  • Browser: Chrome

Additional Information

See Steps To Reproduce #7 for workarounds. Personally, I am not blocked; I am fine with the second workaround. I just thought I'd share my findings since it took me a whiiile to identify the issue, and it's clearly unexpected.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions