Skip to content

fix(stapp): claim external container before first fragment write (closes #752) - #763

Open
Kailigithub wants to merge 1 commit into
lsdefine:mainfrom
Kailigithub:fix/issue-752-stapp-fragment-container-claim
Open

fix(stapp): claim external container before first fragment write (closes #752)#763
Kailigithub wants to merge 1 commit into
lsdefine:mainfrom
Kailigithub:fix/issue-752-stapp-fragment-container-claim

Conversation

@Kailigithub

Copy link
Copy Markdown
Contributor

On Streamlit >=1.60, _tick()'s first write into _stream_fh raises
"A fragment tried to write to a container created outside the fragment"
because the container position is never reserved by a full-app run. Claim
the container once on the first expander write via a single empty
st.markdown("") inside with _stream_fh:; gate subsequent writes with
a module-scope _stream_claimed flag so the claim is not re-emitted on
every 1s refresh.

Adds tests/test_stapp_fragment_container_claim.py with 4 cases that
exercise the live _tick via AST-source extraction plus a behavioural pin
of the old claim-less code.

Parent-commit proof gate:

$ cp frontends/stapp.py /tmp/stapp.py.fixed
$ git checkout main -- frontends/stapp.py
$ python3 -c "import sys; sys.path.insert(0,'tests');     import test_stapp_fragment_container_claim as m;     [getattr(m,n)() for n in dir(m) if n.startswith('test_')]"

# pre-fix result:
PASS test_pre_fix_code_would_emit_no_claim
FAIL test_tick_claims_stream_fh_exactly_once_across_multiple_ticks:
    first tick did not emit a container-claim markdown
FAIL test_tick_claims_stream_fh_on_first_expander_write:
    _tick did not emit a container-claim markdown via st.markdown("")
PASS test_tick_skips_claim_when_stream_fh_is_none
Pass: 2/4

$ cp /tmp/stapp.py.fixed frontends/stapp.py  # restore fix
$ python3 -c "..."
# post-fix result:
PASS (all 4)

The 2 failing cases on the pre-fix source are exactly the fix-detection
cases; the pre-fix pin and the skip-when-_stream_fh-is-None case pass
either way (the pin copies the OLD code body verbatim and confirms it
WOULD NOT have emitted the claim).

Fixes #752.

…efine#752)

On Streamlit >=1.60, _tick()'s first write into _stream_fh raises
"A fragment tried to write to a container created outside the
fragment" because the container position is never reserved by a
full-app run. Claim the container once on the first expander write
via a single empty st.markdown("") inside with _stream_fh:;
gate subsequent writes with a module-scope _stream_claimed flag so
the claim is not re-emitted on every 1s refresh.

Adds tests/test_stapp_fragment_container_claim.py with 4 cases that
exercise the live _tick via AST-source extraction plus a behavioural
pin of the old claim-less code. Pre-fix source: 2/4 fail (the fix
detection); post-fix: 4/4 pass.

Fixes lsdefine#752.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

stapp: "A fragment tried to write to a container created outside the fragment" on streaming (Streamlit 1.60)

1 participant