Skip to content Skip to sidebar Skip to footer

How To Get Highlighted Text With Legends As Html Output?

I have the below input from which I need to generate html output with highlighted text with legends for each color. inp = [('Python', 'language'), ('is', 'others'), ('a', 'others')

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

Post a Comment for "How To Get Highlighted Text With Legends As Html Output?"