You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Streamlit中st.selectbox()报错:意外关键字参数'hover_data'

解决Streamlit中st.selectbox()的TypeError参数错误问题

问题根源

st.selectbox()组件本身不支持hover_data和height这两个关键字参数——你大概率是把可视化组件(比如地图、散点图)的参数误用到了选择框上。另外,你还漏掉了selectbox必填的核心参数:可供选择的选项列表。

修正方案

直接移除无效参数,补充选择框必需的选项列表即可。以下是修正后的完整代码:

st.header("Top 5 dangerous streets by affected people")
# 修正:添加对应后续逻辑的选项列表,移除无效的hover_data、height参数
select = st.selectbox('Affected type of people', ['Pedestrians', 'Cyclists', 'Motorists'])

if select=='Pedestrians':
    st.write(original_data.query("injured_pedestrains>=1")[["on_street_name","injured_pedestrains"]].sort_values(by=['injured_pedestrains'],ascending=False).dropna(how='any')[:5])

elif select=='Cyclists':
    st.write(original_data.query("injured_cyclists>=1")[["on_street_name","injured_cyclists"]].sort_values(by=['injured_cyclists'],ascending=False).dropna(how='any')[:5])

else:
    st.write(original_data.query("injured_motorists>=1")[["on_street_name","injured_motorists"]].sort_values(by=['injured_motorists'],ascending=False).dropna(how='any')[:5])

if st.checkbox("Show raw data", False):
    st.subheader('Raw data')
    st.write(data)

额外需求实现

如果确实需要调整选择框的高度,可以通过自定义CSS实现,示例代码如下:

# 放在selectbox代码之前
st.markdown("""
<style>
div[data-testid="stSelectbox"] div[data-testid="stVerticalBlock"] {
    max-height: 400px;
    overflow-y: auto;
}
</style>
""", unsafe_allow_html=True)

内容的提问来源于stack exchange,提问作者Sandesh Ghimire

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 15:28:25