我想出的唯一解决方案是使用一组唯一的元素定义行。这有点像黑客,但效果很好,并且是一种解决方案,直到 Streamlit 社区提出更好的方法。
在此示例中,我将有一行 4 列,这对于我的侧边栏来说是唯一的。
col1, col2, col3, col4 = st.sidebar.columns([1, 1, 1, 1])
按钮:
with col1:
st.button("????", on_click=style_button_row, kwargs={
'clicked_button_ix': 1, 'n_buttons': 4
})
with col2:
st.button("????", on_click=style_button_row, kwargs={
'clicked_button_ix': 2, 'n_buttons': 4
})
with col3:
st.button("◀", on_click=style_button_row, kwargs={
'clicked_button_ix': 3, 'n_buttons': 4
})
with col4:
st.button("????", on_click=style_button_row, kwargs={
'clicked_button_ix': 4, 'n_buttons': 4
})
造型方式的灵感源自CSS 可以检测一个元素的子元素数量吗? https://stackoverflow.com/questions/8720931/can-css-detect-the-number-of-children-an-element-has:
div[data-testid*="stHorizontalBlock"] > div:nth-child(%(nth_child)s):nth-last-child(%(nth_last_child)s) button
造型功能:
def style_button_row(clicked_button_ix, n_buttons):
def get_button_indices(button_ix):
return {
'nth_child': button_ix,
'nth_last_child': n_buttons - button_ix + 1
}
clicked_style = """
div[data-testid*="stHorizontalBlock"] > div:nth-child(%(nth_child)s):nth-last-child(%(nth_last_child)s) button {
border-color: rgb(255, 75, 75);
color: rgb(255, 75, 75);
box-shadow: rgba(255, 75, 75, 0.5) 0px 0px 0px 0.2rem;
outline: currentcolor none medium;
}
"""
unclicked_style = """
div[data-testid*="stHorizontalBlock"] > div:nth-child(%(nth_child)s):nth-last-child(%(nth_last_child)s) button {
pointer-events: none;
cursor: not-allowed;
opacity: 0.65;
filter: alpha(opacity=65);
-webkit-box-shadow: none;
box-shadow: none;
}
"""
style = ""
for ix in range(n_buttons):
ix += 1
if ix == clicked_button_ix:
style += clicked_style % get_button_indices(ix)
else:
style += unclicked_style % get_button_indices(ix)
st.markdown(f"<style>{style}</style>", unsafe_allow_html=True)
Result: