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

时间:2019-01-13 08:08:38

标签: 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而不使用CSS或JS脚本来生成良好的基本html输出。

我编写了以下代码。

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编码生成它。

1 个答案:

答案 0 :(得分:1)

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')])