From b3585931d6bde98ffe1e98d3294a76747c256460 Mon Sep 17 00:00:00 2001 From: Jianjun Wang <2089966424@qq.com> Date: Fri, 21 Nov 2025 11:57:08 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=EF=BC=9A=E6=94=B9=E8=BF=9B?= =?UTF-8?q?=20Streamlit=20rerun=20=E6=96=B9=E6=B3=95=E7=9A=84=E5=85=BC?= =?UTF-8?q?=E5=AE=B9=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加 st.rerun() 和 st.experimental_rerun() 的版本兼容性 - 同时支持新旧版本 Streamlit - 使用 hasattr 处理方法可用性 - 解决 Streamlit 1.28.0+ 版本的 AttributeError 问题,高版本已弃用这个方法 --- code/chapter6/AutoGenDemo/output.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/code/chapter6/AutoGenDemo/output.py b/code/chapter6/AutoGenDemo/output.py index e5cc0f6..53d09db 100644 --- a/code/chapter6/AutoGenDemo/output.py +++ b/code/chapter6/AutoGenDemo/output.py @@ -23,7 +23,11 @@ def get_bitcoin_price(): # 添加刷新按钮 if st.button('刷新价格'): - st.experimental_rerun() + # 兼容新旧版本 Streamlit + if hasattr(st, 'rerun'): + st.rerun() + else: + st.experimental_rerun() # 显示加载状态 with st.spinner('加载中...'): @@ -36,4 +40,4 @@ def get_bitcoin_price(): if price_change_percentage is not None: st.metric(label="24小时变化 (%)", value=f"{price_change_percentage:.2f}%") else: - st.error("无法获取数据,请稍后重试。") \ No newline at end of file + st.error("无法获取数据,请稍后重试。")