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

如何调整Python Dash布局使Checklist与标题同行并适配长标签?

Dash布局调整方案:实现Checklist与标题同行+长标签自动换行

核心修改思路

  • 用Flex布局让标题和Checklist横向排列:给包含标题与Checklist的内层容器添加弹性布局属性,实现元素同行且垂直居中;
  • 配置Checklist样式支持长标签换行:通过CSS强制选项标签允许自动换行,并让Checklist占据容器剩余空间;
  • 优化容器内边距与间距:避免内容紧贴边框,同时给模块间添加间隔提升可读性。

修改后的完整代码

app.layout = html.Div([
    # 页面标题
    html.Div(html.H1('Data Search', style={'textAlign':'center'})),
    # 搜索框区域
    html.Div(
        [html.B("Search:"), dcc.Input(
            placeholder="Enter a value...",
            type='text',
            id='input-on-submit',
            style={"margin-left": "25px"}
        )]
    ),
    html.Br(),
    html.Div(html.P("Additional Search Criteria")),
    html.Br(),
    # Study Setting 模块
    html.Div(
        html.Div(
            [
                html.B("Study Setting:", style={"margin-left": "15px", "margin-right": "15px"}),
                dcc.Checklist(
                    id='study-setting',
                    options=[
                        {"label":"in vitro","value":"in vitro"},
                        {"label":"ex vivo","value":"ex vivo"},
                        {"label":"in vivo","value":"in vivo"}
                    ],
                    inline=True,
                    style={
                        'flex': 1,
                        'label': {'white-space': 'normal'},
                        'margin': '10px 0'
                    }
                )
            ],
            style={'display': 'flex', 'align-items': 'center'}
        ),
        style={"border-style": "solid", "border-width": "1px", "padding": "10px 0"}
    ),
    # Study Type 模块
    html.Div(
        html.Div(
            [
                html.B("Study Type:", style={"margin-left": "15px", "margin-right": "15px"}),
                dcc.Checklist(
                    id='study-type',
                    options=[
                        {"label":"pharmacodynamics","value":"pharmacodynamics"},
                        {"label":"safety pharmacology","value":"safety pharmacology"},
                        {"label":"pharmacokinectics(PK)","value":"pharmacokinectics"}
                    ],
                    inline=True,
                    style={
                        'flex': 1,
                        'label': {'white-space': 'normal'},
                        'margin': '10px 0'
                    }
                )
            ],
            style={'display': 'flex', 'align-items': 'center'}
        ),
        style={"border-style": "solid", "border-width": "1px", "padding": "10px 0", "margin-top": "10px"}
    ),
    # Tox Study Type 模块
    html.Div(
        html.Div(
            [
                html.B("Tox Study Type:", style={"margin-left": "15px", "margin-right": "15px"}),
                dcc.Checklist(
                    id='tox-study-type',
                    options=[
                        {"label":"single dose","value":"single dose"},
                        {"label":"repeat dose","value":"repeat dose"},
                        {"label":"genotoxicity)","value":"genotoxicity"},
                        {"label":"fertility & early embryonic development (segment |||)","value":"fertility & early embryonic development (segment |||)"},
                        {"label":"embryo-fetal development (segment ||)","value":"embryo-fetal development (segment ||)"},
                        {"label":"local tolerance","value":"local tolerance"},
                        {"label":"hemocompatibility","value":"hemocompatibility"}
                    ],
                    inline=True,
                    style={
                        'flex': 1,
                        'label': {'white-space': 'normal'},
                        'margin': '10px 0'
                    }
                )
            ],
            style={'display': 'flex', 'align-items': 'center'}
        ),
        style={"border-style": "solid", "border-width": "1px", "padding": "10px 0", "margin-top": "10px"}
    ),
])

关键细节说明

  • display: flex让标题与Checklist处于同一水平容器,align-items: center保证元素垂直居中对齐;
  • flex: 1让Checklist自动占据容器剩余宽度,避免内容溢出边框;
  • white-space: normal强制长标签自动换行,确保所有选项内容保持在边框区域内;
  • 给带边框的容器添加padding和margin-top,优化模块间的间距与内容边距。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.25 05:24:10