Streamlit 0.62.0调用st.metric报AttributeError无对应属性求解决
问题原因
st.metric 是 Streamlit 从 1.12.0 版本才正式新增的组件,你当前使用的 0.62.0 版本远低于该版本,不存在这个属性,因此触发报错。
可行解决方案
方案1:升级Streamlit版本(需适配环境)
该方案可以直接使用原生st.metric组件,但需要注意你的现有环境限制:32位Windows系统 + Python3.7.1 不支持1.12.0及以上版本的Streamlit,需要先将操作系统更换为64位、Python版本升级到3.8及以上,再执行升级命令:
pip install --upgrade streamlit
方案2:低版本环境下模拟metric展示效果
如果无法更换现有运行环境,可以用st.markdown配合自定义CSS实现和原生metric接近的展示效果,替换你代码中st.metric(label='Temperature', value=temp)的部分即可,示例代码如下:
# 先注入自定义样式,只需要在页面代码最前面执行一次即可 st.markdown(""" <style> .custom-metric { padding: 12px 16px; border-radius: 8px; background-color: #f7f8fa; } .custom-metric-label { font-size: 14px; color: #6b7280; margin-bottom: 4px; } .custom-metric-value { font-size: 32px; font-weight: 700; color: #111827; } </style> """, unsafe_allow_html=True) # 调用展示 temp_display = f"{round(temp)}{sign}C" st.markdown(f""" <div class="custom-metric"> <div class="custom-metric-label">Temperature</div> <div class="custom-metric-value">{temp_display}</div> </div> """, unsafe_allow_html=True)
内容的提问来源于stack exchange,提问作者Akash Das
相关产品推荐
相关产品推荐

