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

如何在Streamlit中将垂直输入改为动态水平列布局

调整Streamlit数字输入控件为动态水平列布局

原代码中数字输入控件为垂直排列,以下是修改后的代码,实现控件的水平动态适配布局,同时保留原有数据处理功能:

import numpy as np
import streamlit as st

st.header('Input Data')
p = np.array([[4,3,6,2],[1,4,3,5],[2,5,2,3],[5,2,4,1],[3,6,1,4]])
st.dataframe(p)

t = []
tb = len(p)
st.subheader('Enter the T value into row for multiplication')

# 创建与行数匹配的动态列,实现水平排列
cols = st.columns(tb, gap="small")
for i, col in enumerate(cols):
    with col:
        value = st.number_input(f'row{i}:', min_value=0, max_value=10, step=1)
        t.append(value)

# 原有数据处理逻辑保持不变
t = np.array(t)
t = np.tile(t, (p.shape[1], 1)).T 
p_mod = np.multiply(p, t)

st.header('Output Data')
st.dataframe(p_mod)

关键改动说明

  • 使用st.columns(tb, gap="small")创建与数据行数一致的列容器,gap参数控制列间距,优化布局美观度
  • 通过enumerate遍历列容器,在每个列内渲染对应数字输入控件,实现水平排列
  • Streamlit的列布局会自动适配页面宽度,页面缩窄时控件将自动换行,实现动态适配效果
  • 完全保留原有numpy数据处理逻辑,确保行值与原矩阵的逐元素乘法功能正常

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 05:55:17