python - 如何获取带有图例的突出显示文本作为 html 输出?

标签 python html

我有以下输入,我需要从中生成 html 输出,其中突出显示的文本带有每种颜色的图例。

inp = [('Python', 'language'),
('is', 'others'),
('a', 'others'),
('programming', 'others'),
('language', 'others'),
('.', 'others'),
('John', 'Person'),
('is', 'others'),
('an', 'others'),
('excellent', 'Modifier'),
('coder', 'others'),
('based', 'Action'),
('out', 'others'),
('of', 'others'),
('California', 'location'),
('.', 'others')]

我想使用 Python 生成良好的基本 html 输出,而不使用 CSS 或 JS 脚本。

我写了下面的代码。

def nercolor(out):
import webbrowser
from random import shuffle

htmlcolor = ['#00FFFF', '#FF0000', '#ADD8E6', '#00FF00', '#FF00FF', '#FFA500', '#008000', '#808000', '#736AFF', '#368BC1', '#95B9C7', '#7FFFD4', '#728C00', '#FFA62F', '#827839', '#C36241', '#F75D59', '#7E354D']

shuffle(htmlcolor)
clt = []
for i,j in out:
    if j != 'others':
        clt.append(j)

COLOR = htmlcolor[:len(clt)]
d = dict(zip(clt, COLOR))

strh = "<html>"
for i,j in out:
    if j == 'others':
        strh += i
        strh += " "
    if j != 'others':
        strh += "<strong><span style='background-color:%s'>" % d[j]
        strh += i
        strh += "</span></strong>"
        strh += " "
strh += "</html>"

#Legends

COLOR = COLOR[:len(clt)]
lang = ['language', 'Person', 'location']
cl = list(zip(COLOR, lang))

stri = ''
stri += "<table>"
for j, i in d.items():
    stri += '<tr>'
    stri += "<td <span style='background-color: %s'>__________</span>&nbsp;</td>" %i
    stri += "<td>%s</td>" %j
    stri += '</tr>'
stri += "</table>"

new = stri+strh
log_file = open('/home/nms2kor/Documents/graphs/testcasecc3.html', 'w')
log_file.write(new)
return webbrowser.open('/home/nms2kor/Documents/graphs/testcasecc3.html')

nercolor(result)

我从我的代码中得到了以下输出 html。

screenshot of image

但我想生成更好的 html 输出,以适当的方式在文本之间设置行间距,将突出显示的文本向左对齐,将图例向右对齐。

使用 html 设计它可能很容易,但我的要求是使用 python 编码生成它。

最佳答案

def nercolor(out):
    import webbrowser
    from random import shuffle

    htmlcolor = ['#00FFFF', '#FF0000', '#ADD8E6', '#00FF00', '#FF00FF', '#FFA500', '#008000', '#808000', '#736AFF', '#368BC1', '#95B9C7', '#7FFFD4', '#728C00', '#FFA62F', '#827839', '#C36241', '#F75D59', '#7E354D']

    shuffle(htmlcolor)
    clt = []
    for i,j in out:
        if j != 'others':
            clt.append(j)

    COLOR = htmlcolor[:len(clt)]
    d = dict(zip(clt, COLOR))

    strh = "<html><div style='width:70%;display:inline-block'>"
    for i,j in out:
        if j == 'others':
            strh += i
            strh += " "
        if j != 'others':
            strh += "<strong><span style='background-color:%s'>" % d[j]
            strh += i
            strh += "</span></strong>"
            strh += " "
    strh += "</div></html>"

    #Legends

    COLOR = COLOR[:len(clt)]
    lang = ['language', 'Person', 'location']
    cl = list(zip(COLOR, lang))

    stri = '<div style="width:20%;display:inline-block">'
    stri += "<table>"
    for j, i in d.items():
        stri += '<tr>'
        stri += "<td><span style='background-color: %s'>__________</span>&nbsp;</td>" %i
        stri += "<td>%s</td>" %j
        stri += '</tr>'
    stri += "</table></div>"

    new = stri+strh
    log_file = open('path/testcasecc3.html', 'w')
    log_file.write(new)
    return webbrowser.open('path/testcasecc3.html')

nercolor([('Python', 'language'),
('is', 'others'),
('a', 'others'),
('programming', 'others'),
('language', 'others'),
('.', 'others'),
('John', 'Person'),
('is', 'others'),
('an', 'others'),
('excellent', 'Modifier'),
('coder', 'others'),
('based', 'Action'),
('out', 'others'),
('of', 'others'),
('California', 'location'),
('.', 'others')])

关于python - 如何获取带有图例的突出显示文本作为 html 输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54167136/

相关文章:

python - Numpy---如何同时替换数组中的某些多个元素?

python - 无法使用 numpy 从数组 python 中删除列

python - 等待使用 elasticsearch DSL 完成 UpdateByQuery

javascript - 从 tinymce 容器中单击输入文本时,有什么方法可以防止失去焦点?

javascript - 当您访问其中包含特定 ID 的 URL 时添加 CSS

python - 如何使用 oauth2 访问 StackExchange API?

Python 的引用传递

html - 如何水平对齐文本?

javascript - 如何将 IFrame 高度更改为其内容的 100%?

html - CSS 背景不透明度