【发布时间】: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_list和words的示例
标签: python python-3.x plotly-dash