简体   繁体   English

根据回调子级更改 Dash bootstrap dcc.tab 中的 label

[英]Changing the label in Dash bootstrap dcc.tab based on callback children

I am using dash core components to create a dash app that includes tabs that return tables based on callbacks.我正在使用 dash 核心组件创建一个 dash 应用程序,其中包含基于回调返回表的选项卡。 I'm trying to see if it's possible to change the label on the tab itself based on the callback, though it seems like the label will only accept a string, and not a callback with and id and children.我正在尝试查看是否可以根据回调更改选项卡本身上的 label,尽管 label 似乎只接受一个字符串,而不是带有 id 和孩子的回调。

Right now the label of the tab simply says 'Car Type' (this is just a snippet of the code):现在选项卡的 label 只显示“汽车类型”(这只是代码片段):

    dbc.Row([ 
        dbc.Col(dcc.Dropdown(id='car-types', multi=False,,
                    options=[{'label':x, 'value':x}
                    for x in sorted(car_list)]),
                    #width={'size': 5, "offset": 1, 'order': 1}
            ),        

    html.Div([
        dcc.Tabs([
            dcc.Tab(label='Car Type', children=

                    dbc.Col([
                        html.Div(id="table1"
                                )]
                    )
            )
     @app.callback(
     [Output('table1', 'children'),
     Output('car_label', 'children')],
     [Input('car-types', 'value')],
     [State('car_manuf', 'value')],

  def update_table1(a,b):

        code for table,

        a = code for car_label string

        return html.Div([dt.DataTable(),
            ),  a

But what if I wanted it to say "Car Type SUV" or "Car Type Sedan" based on what the output 'car_label' says, how can I change the label of the tab to say that?但是,如果我想让它根据 output 'car_label' 所说的内容说“汽车型 SUV”或“汽车型轿车”,我该如何更改选项卡的 label 来表示呢?

I tried something like:我试过类似的东西:

    html.Div([
        dcc.Tabs([
            dcc.Tab(label='Car Type ' + (children=[],id='car_label'), children=

                    dbc.Col([
                        html.Div(id="table1"
                                )]
                    )

But obviously that won't work.但显然那是行不通的。 Any suggestions?有什么建议么?

Maybe something like this with a dropdown and string formatting.也许像这样带有下拉和字符串格式的东西。

import dash
import dash_html_components as html
import dash_core_components as dcc

from dash.dependencies import Input, Output

external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

app = dash.Dash(__name__, external_stylesheets=external_stylesheets)

app.layout = html.Div([
    dcc.Tabs(id='tabs-example', value='tab-1', children=[
        dcc.Tab(label='', id='first-tab', value='tab-1'),
        dcc.Tab(label='', id='second-tab', value='tab-2'),
    ]),
    dcc.Dropdown(id='car-types', multi=False, value='honda',
                 options=[{'label':'honda', 'value':'honda'},
                          {'label':'toyota', 'value':'toyota'}]
                 ),
    dcc.Dropdown(id='car-types2', multi=False, value='tesla',
                 options=[{'label': 'jeep', 'value': 'jeep'},
                          {'label': 'tesla', 'value': 'tesla'}]
                 ),
    html.Div(id='tabs-example-content')
])

@app.callback(Output('tabs-example-content', 'children'),
              Input('tabs-example', 'value'))
def render_content(tab):
    if tab == 'tab-1':
        return html.Div([
            html.H3('Tab content 1...')
        ])
    elif tab == 'tab-2':
        return html.Div([
            html.H3('Tab content 2...')
        ])

@app.callback(
    Output('first-tab', 'label'),
    Input('car-types', 'value')
)
def update_label(name):
    return f"Car Type: {name}"


@app.callback(
    Output('second-tab', 'label'),
    Input('car-types2', 'value')
)
def update_label(name2):
    return f"Car Type: {name2}"

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM