中心流线型按钮

ybm*_*bml 8 python streamlit

如何使用 Streamlit 将按钮居中,以便该按钮仍可点击?这是一个返回随机数的按钮的小例子:

 import streamlit as st 
 import numpy as np

 if st.button('Click'): 
     rand = np.random.standard_normal()
     st.write(str(rand))
Run Code Online (Sandbox Code Playgroud)

我已经看到了标题降价的解决方案,但没有看到诸如按钮之类的交互式元素的解决方案。

Hen*_*uni 9

目前 Streamlit 不支持该功能,但我们正在努力(有关更多信息,请参阅我们的路线图)。随意将您的想法添加到用于 Streamlit讨论的可定制布局

并感谢您使用 Streamlit!


小智 9

col1, col2, col3 = st.beta_columns(3)
if col2.button('Click'):
    st.write('hello')
Run Code Online (Sandbox Code Playgroud)


小智 5

据我所知,没有标准方法可以将按钮对齐在中心。不过我找到了一个快速解决办法。这不是正确的方法,但可以完成工作。

您可以尝试一下:

col1, col2, col3 , col4, col5 = st.beta_columns(5)

with col1:
    pass
with col2:
    pass
with col4:
    pass
with col5:
    pass
with col3 :
    center_button = st.button('Button')
Run Code Online (Sandbox Code Playgroud)

下面将创建 5 列,您可以将按钮放在第三列的中心,而其他 4 列保留为空。这只是一个快速修复,不是正确的方法,但它为我完成了工作。希望你喜欢它。


Con*_*tov 5

TL;博士;

import streamlit as st

st.markdown("----", unsafe_allow_html=True)
columns = st.columns((2, 1, 2))
button_pressed = columns[1].button('Click Me!')
st.markdown("----", unsafe_allow_html=True)
Run Code Online (Sandbox Code Playgroud)

您将得到以下结果(如果默认:窄流化页面设置): 在此输入图像描述

流光柱

Streamlit 文档通过以下方式定义columns() :

columns = st.columns(spec)
Run Code Online (Sandbox Code Playgroud)

其中spec是 或inta list(或元组)。例如,st.columns([3, 1, 2])创建 3 列,其中第一列的宽度是第二列宽度的 3 倍,最后一列是该宽度的 2 倍。因此,您可以调整它们的相对宽度。

更有用的是,您将获得一个列列表,因此您可以将它们作为后续数字进行处理。如果列数灵活,您可以使用它:

import streamlit as st
import random

column_qty = random.randint(1, 10)  # random number of columns on each run
buttons_pressed = []  # here we will collect widgets
st.markdown("----", unsafe_allow_html=True)

#  Create all columns with random relative widths
columns = st.columns([random.randint(1, 3) for _ in range(column_qty)]) 

# Show widgets in them
for x, col in enumerate(columns):
    # if you need a static widget, just use: `col.text("some text")`
    buttons_pressed.append(col.checkbox(f'{x}')) # add widgets to the list to be able checking their state later
st.markdown("----", unsafe_allow_html=True)
st.text(f'Total columns: {column_qty}')

# Check each widget for its state and display the index of the checked squares.
for x, btn in enumerate(buttons_pressed):
    if btn:
        st.write(f"{x}")  # NOTE: some columns may be dropped next run! 
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

对于弹性输入表单来说非常有用。