diyclassics Claude Opus 4.8 (1M context) commited on
Commit
24a1bb1
·
1 Parent(s): 41b6ddb

fix: render parse table from session_state, not inside if st.button

Browse files

Root cause of the blank table on HF: the table (and download button) were
rendered inside 'if st.button("Analyze")', which is True only on the single
rerun right after the click. The download button triggers a follow-up rerun
where st.button is False, tearing down the async st.dataframe grid before it
paints — so the summary text showed but the grid stayed blank. The lexicon
demo renders its dataframe from st.session_state (outside the button block)
and works on HF for exactly this reason.

Refactor to match: the button stores the result in session_state; the table
renders from session_state on every stable rerun. Restores the interactive
grid (use_container_width + column widths) and makes the download button
non-destructive (table survives the rerun), so the old 'resets the app'
warning is gone.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

Files changed (1) hide show
  1. pages/1_parsing_demo.py +16 -19
pages/1_parsing_demo.py CHANGED
@@ -86,14 +86,21 @@ with tab1:
86
  "Enter some text to analyze (max 500 tokens)", value=default_text, height=200
87
  )
88
  if st.button("Analyze"):
89
- df = analyze_text(text)
 
 
 
 
 
 
 
 
 
 
90
  sent_count = df["sent_id"].nunique()
91
- st.text(f"Analyzed {len(df)} tokens in {sent_count} sentences with {model_name} model.")
92
- # Use use_container_width instead of a fixed width=1200: the fixed
93
- # pixel width made st.dataframe render blank behind HF Spaces' proxy
94
- # (the summary line showed but the grid never painted). The lexicon
95
- # demo already renders fine this way. Column widths tuned so the wide
96
- # feats/lemma columns get room and the numeric columns stay narrow.
97
  st.dataframe(
98
  df,
99
  use_container_width=True,
@@ -112,21 +119,11 @@ with tab1:
112
  },
113
  )
114
 
115
- @st.cache_data
116
- def convert_df(df):
117
- return df.to_csv(index=False, sep="\t").encode("utf-8")
118
-
119
- csv = convert_df(df)
120
-
121
- def create_timestamp():
122
- return datetime.datetime.now().strftime("%Y%m%d%H%M%S")
123
-
124
- # nb: clicking this button resets app! Open streamlit issue, as of 4.15.2023; cf. https://github.com/streamlit/streamlit/issues/4382
125
- st.markdown("*NB: Clicking the download button will reset the app after download!*")
126
  st.download_button(
127
  "Press to Download",
128
  csv,
129
- f"latincy-analysis-{create_timestamp()}.tsv",
130
  "text/csv",
131
  key="download-csv",
132
  )
 
86
  "Enter some text to analyze (max 500 tokens)", value=default_text, height=200
87
  )
88
  if st.button("Analyze"):
89
+ st.session_state["parse_df"] = analyze_text(text)
90
+
91
+ # Render the results from session_state, NOT inside the `if st.button` block.
92
+ # st.button is True only on the single rerun right after the click, so
93
+ # rendering the table there means the async st.dataframe grid gets torn down
94
+ # by the next rerun (e.g. the download button) before it paints — that was
95
+ # the blank-table-on-HF bug. Rendering from session_state paints on every
96
+ # stable rerun, matching the lexicon demo (which works on HF). It also makes
97
+ # the download button non-destructive: the table survives the rerun.
98
+ if "parse_df" in st.session_state:
99
+ df = st.session_state["parse_df"]
100
  sent_count = df["sent_id"].nunique()
101
+ st.text(
102
+ f"Analyzed {len(df)} tokens in {sent_count} sentences with {model_name} model."
103
+ )
 
 
 
104
  st.dataframe(
105
  df,
106
  use_container_width=True,
 
119
  },
120
  )
121
 
122
+ csv = df.to_csv(index=False, sep="\t").encode("utf-8")
 
 
 
 
 
 
 
 
 
 
123
  st.download_button(
124
  "Press to Download",
125
  csv,
126
+ f"latincy-analysis-{datetime.datetime.now().strftime('%Y%m%d%H%M%S')}.tsv",
127
  "text/csv",
128
  key="download-csv",
129
  )