Python Dash:如何将回调期间生成的 HTML 代码添加到布局?

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

我当前的具体问题是我想将回调期间生成的 pandas DataFrame 渲染为表。它不必是可点击的、可排序的等等。

目前我使用 markdown 作为中间表示:

import dash
from dash import html, dcc
from dash.dependencies import Input, Output
import pandas as pd

app = dash.Dash(__name__)
app.layout = html.Div(
    [
        html.Button("Show table", id="button"),
        html.Div(children="Table should show up here", id="table-output"),
    ]
)

@app.callback(
    Output("table-output", "children"),
    Input("button", "n_clicks"),
    prevent_initial_call=True
)
def show_table(n_clicks):
    df = pd.DataFrame([[1, 2, 3],[2, 3, 4]], columns=['a', 'b', 'c'])
   
    return [html.Div([dcc.Markdown(children=df.to_markdown())])]

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

我知道DataTable,我在另一个场合使用过它。它肯定会解决问题,但在这种情况下似乎“过度设计”。由于 DataFrame 有一个

to_html()
方法,我希望会有一种更简单的方法。我对特定情况(即使用 pandas DataFrame)和一般情况(即由 Python 生成的 HTML 代码)都感兴趣。

python html dataframe plotly-dash
1个回答
0
投票

让 // 将这些变量替换为您的信息 组织=“你的组织”, 项目=“你的项目”, pat =“your_pat_token”,

// Encode the PAT for the authorization header
encodedPat = Text.FromBinary(Text.ToBinary(":" & pat)),
authorizationHeader = "Basic " & Binary.ToText(Text.ToBinary(encodedPat), BinaryEncoding.Base64),

// API URL for the WIQL query
url = "https://dev.azure.com/" & organization & "/" & project & "/_apis/wit/wiql?api-version=7.0",

// WIQL Query to fetch all bugs
wiqlQuery = "{ ""query"": ""SELECT [System.Id], [System.Title], [System.State] FROM workitems WHERE [System.WorkItemType] = 'Bug'"" }",

// Make the API request
response = Json.Document(Web.Contents(url, [
    Headers=[Authorization=authorizationHeader, #"Content-Type"="application/json"],
    Content=Text.ToBinary(wiqlQuery)
])),

// Extract work items
workItems = response[workItems],

// Convert work items list to table
workItemsTable = Table.FromList(workItems, Splitter.SplitByNothing(), null, null, ExtraValues.Error),

// Expand the columns to get details of each bug
expandedWorkItems = Table.ExpandRecordColumn(workItemsTable, "Column1", {"id"}, {"Bug ID"})

在 扩展工作项

© www.soinside.com 2019 - 2024. All rights reserved.