【问题标题】:Python dash - How to highlight / bold specific words in a rowPython dash - 如何突出显示/加粗一行中的特定单词
【发布时间】:2020-11-19 21:15:37
【问题描述】:

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

例如,<b> 中的单词应该是粗体(示例下划线):

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

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>\1</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 似乎没有通过,我可以看到降价是一个选项,但我无法让它工作并且不确定如何(如果可以的话)实现。

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

【问题讨论】:

  • 什么是remove_common_words_list
  • 添加了remove_common_words_listwords的示例

标签: python python-3.x plotly-dash


【解决方案1】:

此答案使用Markdown in Dash,因为我认为不支持 HTML 呈现(请参阅此Github Issue)。

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

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

按照 Markdown 语法进行加粗。

接下来,通过更新第 31 行,指定 Table 应将文本解析为 Markdown

columns=[{"name": i, "id": i, "type": 'text', "presentation": 'markdown'} for i in data.columns]

并且那些指定的单词应该加粗

【讨论】:

    猜你喜欢
    • 2020-10-11
    • 1970-01-01
    • 2019-06-24
    • 2014-03-08
    • 2019-12-06
    • 2012-01-29
    • 2018-02-08
    • 2015-01-10
    • 2015-10-28
    相关资源
    最近更新 更多