Skip to content

Commit

Permalink
Handle out of order blocks when parsing element tree (#7923)
Browse files Browse the repository at this point in the history
  • Loading branch information
AnOctopus committed Jan 10, 2024
1 parent 6458b27 commit b8a7c09
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/streamlit/testing/v1/element_tree.py
Expand Up @@ -1915,6 +1915,13 @@ def parse_tree_from_messages(messages: list[ForwardMsg]) -> ElementTree:
children[idx] = child
assert isinstance(child, Block)
current_node = child

# Handle a block when we already have a placeholder for that location
if isinstance(new_node, Block):
placeholder_block = current_node.children.get(delta_path[-1])
if placeholder_block is not None:
new_node.children = placeholder_block.children

current_node.children[delta_path[-1]] = new_node

return root
24 changes: 24 additions & 0 deletions lib/tests/streamlit/testing/app_test_test.py
Expand Up @@ -181,3 +181,27 @@ def script():
assert len(at.text) == 4
# querying elements via a block only returns the elements in that block
assert len(at.get("expandable")[0].text) == 2


def test_out_of_order_blocks() -> None:
# Regression test for #7711
def script():
import streamlit as st

container = st.container()
with container:
st.markdown("BarFoo")

def button_one_clicked(cont):
cont.info("Hi!")
cont.markdown("FooBar")

st.button("one", on_click=button_one_clicked, args=[container])

at = AppTest.from_function(script).run()

at.button[0].click().run()

assert at.markdown.len == 2
assert at.info[0].value == "Hi!"
assert at.markdown.values == ["FooBar", "BarFoo"]

0 comments on commit b8a7c09

Please sign in to comment.