Python dash - 如何突出显示/加粗连续的特定单词

Python dash - How to highlight / bold specific words in a row

我创建了一个 table,每一行都有一个特定的词需要突出显示。

因此,作为示例,<b> 中的单词应为粗体(示例加下划线):

我创建 table 并尝试将特定单词加粗的代码如下:

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

remove_common_words_list = ["iraq", "america"]
words = { "iraq": { "occurrences":63, 
                    "documents":{ "doc3,txt", "doc2.txt", "doc1.txt", "doc5.txt",
                    "sentences":["And when all else fails, when Katrina happens, or the death toll in Iraq mounts, we've been told that our crises are somebody else's fault.", 'But all of this cannot come to pass until we bring an end to this war in Iraq.'] },
          "america": { "occurrences":46, 
                    "documents":{ "doc3,txt", "doc1.txt", "doc5.txt",
                    "sentences":['And I accepted the job, sight unseen, motivated then by a single, simple, powerful idea that I might play a small part in building a better America.', 'It was here, in Springfield, where I saw all that is America converge farmers and teachers, businessmen and laborers, all of them with a story to tell, all of them seeking a seat at the table, all of them clamoring to be heard.'] }

remove_common_words = { "Word (Total Occurrences)":[], "Documents":[],  "Sentences containing the word":[] }
for index, word in enumerate(remove_common_words_list[0:1]):
    remove_common_words["Word (Total Occurrences)"].append(f"{word} ({words[word]['occurrences']})")
    remove_common_words["Documents"].append(", ".join(words[word]["documents"]))
    remove_common_words["Sentences containing the word"].append(re.sub(r"(" + word + ")", r"<b></b>", "\n\n".join(words[word]["sentences"]), flags=re.IGNORECASE))

data = pd.DataFrame(remove_common_words)

app = dash.Dash(__name__)
app.layout = html.Div([
    dash_table.DataTable(
        id='table',
        columns=[{"name": i, "id": i} for i in data.columns],
        data=data.to_dict('records'),
        style_cell={
            'whiteSpace': 'pre-line',
            'textAlign': 'left',
            'vertical-align':'top'
        },
        style_data_conditional=[
            {
                'if': {'row_index': 'odd'},
                'backgroundColor': 'rgb(248, 248, 248)'
            }
        ],
        style_header={
            'fontWeight': 'bold'
        }
    )
])

app.run_server(debug=True)

HTML 似乎没有被通过,我可以看到 markdown 是一个选项,但我一直无法让它工作并且不确定如何(如果可以)实施。

所以我的问题是如何将特定的文本片段加粗或突出显示?

此答案使用 Markdown in Dash, as I don't think HTML rendering is supported (see this Github Issue).

首先,将示例中的第 23 行更改为

remove_common_words["Sentences containing the word"].append(re.sub(r"(" + word + ")", r"****", "\n\n".join(words[word]["sentences"]), flags=re.IGNORECASE))

遵循加粗的 Markdown 语法。

接下来,指定 Table 应该通过更新第 31 行

将文本解析为 Markdown
columns=[{"name": i, "id": i, "type": 'text', "presentation": 'markdown'} for i in data.columns]

那些指定的词应该加粗