Plotly-Dash 应用布局的位置元素

Position Elements of Plotly-Dash App Layout

我正在创建一个 dash 应用程序,我可以在其中上传图像并对其执行图像处理。我已经阅读了 Dash 教程,但几乎没有关于如何在页面上定位各种元素的信息。

例如,我希望我上传的图片显示在标题的右侧和下方,“拖放...”按钮和其他滑块和复选框显示在左侧。我在下面附上了示例图片。

以下是我目前所拥有的:

style = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=style)


colors = {
    'title': '#FFFFFF',
    'text': '#AOAABA',
    'background': '#161A1D'
}

app.layout = html.Div([
    html.H2("Object Detection and Image Processing",
                style={'textAlign':'center',
                        'color': colors['text']}),
    html.Hr(),
    html.H3("Upload Image Below To Get Started",
                style={'textAlign':'center',
                       'color': colors['text']}),
    dcc.Upload(
        id='upload-image',
        children=html.Div([
            'Drag and Drop or ',
            html.A('Select Files')
        ]),
        style={
            'width': '100%',
            'height': '60px',
            'lineHeight': '60px',
            'borderWidth': '1px',
            'borderStyle': 'dashed',
            'borderRadius': '5px',
            'textAlign': 'center',
            'margin': '10px'
        },
        # Allow multiple files to be uploaded
        multiple=True
        ),
    html.Div(id='output-image-upload'),
])

def parse_contents(contents, filename, date):
    return html.Div([
        html.H5(filename),
        html.H6(datetime.datetime.fromtimestamp(date)),

        # HTML images accept base64 encoded strings in the same format as upload
        html.Img(src=contents)
    ])

@app.callback(Output('output-image-upload', 'children'),
              Input('upload-image', 'contents'),
              State('upload-image', 'filename'),
              State('upload-image', 'last_modified'))

def update_output(list_of_contents, list_of_names, list_of_dates):
    if list_of_contents is not None:
        children = [
            parse_contents(c, n, d) for c, n, d in
            zip(list_of_contents, list_of_names, list_of_dates)]
        return children

if __name__ == '__main__':
    app.run_server(debug=True)

下面是我希望的样子:

您可以使用以下样式将两列的内容包装在两个单独的 html.Div() 中:

style = {
    'display': 'inline-block',
    'vertical-align': 'top'
}

这将确保两列水平堆叠并在顶部对齐。

import datetime
import dash
import dash_html_components as html
import dash_core_components as dcc
from dash.dependencies import Input, Output, State

style = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=style)

colors = {
    'title': '#FFFFFF',
    'text': '#AOAABA',
    'background': '#161A1D'
}

app.layout = html.Div([

    # Header.
    html.H2(
        children='Object Detection and Image Processing',
        style={'textAlign': 'center', 'color': colors['text']}
    ),

    html.Hr(),

    # First column.
    html.Div(
        
        children=[
            
            html.H3(
                children='Upload Image Below To Get Started',
                style={'textAlign': 'center', 'color': colors['text']}
            ),

            dcc.Upload(
                id='upload-image',
                children=html.Div([
                    'Drag and Drop or ',
                    html.A('Select Files')
                ]),
                style={
                    'width': '100%',
                    'height': '60px',
                    'lineHeight': '60px',
                    'borderWidth': '1px',
                    'borderStyle': 'dashed',
                    'borderRadius': '5px',
                    'textAlign': 'center',
                    'margin': '10px'
                },
                multiple=True
            ),
            
        ],
        
        style={
            'display': 'inline-block',
            'vertical-align': 'top'
        }
        
    ),

    # Second column.
    html.Div(

        children=[
            html.Div(id='output-image-upload'),
        ],

        style={
            'display': 'inline-block',
            'vertical-align': 'top',
            'margin-left': '100px'
        }
        
    )

])

def parse_contents(contents, filename, date):

    return html.Div([
        html.H5(filename),
        html.H6(datetime.datetime.fromtimestamp(date)),
        html.Img(src=contents)
    ])

@app.callback(Output('output-image-upload', 'children'),
              Input('upload-image', 'contents'),
              State('upload-image', 'filename'),
              State('upload-image', 'last_modified'))
def update_output(list_of_contents, list_of_names, list_of_dates):
    if list_of_contents is not None:
        children = [
            parse_contents(c, n, d) for c, n, d in
            zip(list_of_contents, list_of_names, list_of_dates)
        ]
        return children

if __name__ == '__main__':
    app.run_server(debug=True, host='127.0.0.1')