Return Python Dash 中单击事件的“href”值

Return ‘href’ value on a click event in Python Dash

我一直在尝试 return python-Dash 应用程序中单击事件的“href”值。下面是我的代码片段:

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


app = dash.Dash(
    __name__,
    meta_tags=[{"name": "viewport", "content": "width=device-width, initial-scale=1"}],
)


def make_table(df, val):
    table = []

    for index, row in df.iterrows():
        rows = []
        html.Td([
                    html.Div([row["col1"]]),
                    html.A(id = 'link',href=row["file-link"], children=row["link-name"], target="_blank"),
                  ])
        table.append(html.Tr(rows))

    return table


app.layout = html.Div([
                        html.Table(
                            id="table-element",
                            className="table__container",
                        )
                        ],
                        className="six columns",
                        ),


@app.callback(
    Output("link", 'pathname'),
    [Input('link', 'n_clicks')],
    [State('link', 'href')]
    )
def open_link(n_clicks, href):
    enable_open_link(href) #enable_open_link function takes in the href string value (a local filesystem link) and opens it up in a new window.


@app.callback(
    Output("table-element", 'children'),
    [Input("input-1-submit", 'n_submit')],
     [State('input-1-submit', 'value')]
)
def update_output(ns1,val):
    table = make_table(df,val)
    
    return table

此代码在某种程度上有效,即它 return 是一个 href 值,但不是我点击的那个。它总是 returns 存储在 html table.

中的最后一个 href 值

有没有办法在我点击 link 时获取 href 值? 我知道我可以使用 Jquery 来获取正确的 href 值……但是我没有找到在回调函数中集成 javascript 的方法。

我认为这是因为您创建的所有链接都具有相同的 id ='link'

你需要想办法解决这个问题。一种可能性是在根据 df 的索引创建 id 时生成它,但随后您还必须创建相应的回调。该线程告诉您如何完成。

从@Rudertier 提供的线程中得到线索,我得到了解决方案。 以下是更新后的代码片段:

def make_table(df, val):
    table = []

    for index, row in df.iterrows():
        rows = []
        html.Td([
                    html.Div([row["col1"]]),
                    html.A(id = 'link'+str(index),href=row["file-link"], children=row["link-name"], target="_blank"),
                  ])
        table.append(html.Tr(rows))

    return table


app.layout = html.Div([
                        html.Table(
                            id="table-element",
                            className="table__container",
                        )
                        ],
                        className="six columns",
                        ),


links = ['link1','link2','link3','link4','link5','link6','link7','link8','link9','link10']   
for link in links: 
    @app.callback(
        Output('{}'.format(link), 'pathname'),
        [Input('{}'.format(link), 'n_clicks')],
        [State('{}'.format(link), 'href')]
        )
    def open_link(n_clicks, href):
        enable_open_link(href) #enable_open_link function takes in the href string value (a local filesystem link) and opens it up in a new window.


@app.callback(
    Output("table-element", 'children'),
    [Input("input-1-submit", 'n_submit')],
     [State('input-1-submit', 'value')]
)
def update_output(ns1,val):
    table = make_table(df,val)
    
    return table