Dash Python:使用链接激活不同的选项卡

问题描述 投票:0回答:1

我有一个多选项卡破折号应用程序。我想在一个选项卡上放置一个超链接,单击该超链接会将我带到另一个选项卡。我正在尝试做什么 -

tab1_content = dbc.Markdown('''
### This is Tab 1    
''')

tab2_content = dbc.Markdown('''
### This is Tab 2    
[Take me to Tab1](#Tab1)
''')
 dbc.Tabs([
    dbc.Tab([tab1_content],id = 'Tab1',label = 'Tab 1'),
    dbc.Tab([tab2_content],id = 'Tab2',label = 'Tab 2'),
    ])                        

我尝试在选项卡 1 上使用 html.A() 添加锚点。这也没有帮助。让我知道是否有任何方法可以完成此任务。

提前谢谢您。

python plotly-dash
1个回答
0
投票

您可以在基于 URI 片段的回调中管理组件的状态。您需要

dcc.Location
组件才能执行此操作。如果使用
dbc.Tabs
来管理
active_tab
,您必须为每个
tab_id
设置
dbc.Tab
属性。这是示例:

from dash import dcc, ctx, Input, Output, no_update

# location component to be included into the layout:
location = dcc.Location('url')

# Need all dependant components here to prevent an infinite callback loop
@dash.callback(
    Output('url', 'hash'),
    Output('tabs', 'active_tab'),
    Input('url', 'hash'),
    Input('tabs', 'active_tab'),
    config_prevent_initial_callbacks=True)
def handle_navigation(fragment, tab_id):

    triggered_by_location = (dash.ctx.triggered_id == 'url')

    if triggered_by_location:

        # TODO: Add necessary validations here or more complex unpacking
        new_tab_id = fragment[1:]

        return no_update, new_tab_id

    # TODO: Add more complex state encoding if required
    new_fragment = f"#{tab_id}"
    
    return new_fragment, no_update
© www.soinside.com 2019 - 2024. All rights reserved.