Spaces:
Sleeping
Sleeping
File size: 1,194 Bytes
ad36ee4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | ## Utility Functions
# Score to colors
function score_to_color(score)
if score ≤ 0.5
# Interpolate from blue (#0000FF) to gray (#888888)
t = score / 0.5
r = round(Int, (1 - t) * 0 + t * 136)
g = round(Int, (1 - t) * 0 + t * 136)
b = round(Int, (1 - t) * 255 + t * 136)
else
# Interpolate from gray (#888888) to red (#FF0000)
t = (score - 0.5) / 0.5
r = round(Int, (1 - t) * 136 + t * 255)
g = round(Int, (1 - t) * 136 + t * 0)
b = round(Int, (1 - t) * 136 + t * 0)
end
return "rgb($r,$g,$b)"
end
# Creates a paragraph with colored text based on scores from a dataframe
function scored_text_paragraph(df::DataFrame)
fragments = String[]
for row in eachrow(df)
color = score_to_color(row[:score])
text = row[:text]
push!(fragments, """<span style="color: $color;">$text</span>""")
end
html = "<p style='font-family: system-ui, sans-serif; font-size: 1.1rem;'>" * join(fragments, " ") * "</p>"
return HTML(html)
end
df = DataFrame(score = [0.1, 0.3, 0.6, 0.9],
text = ["This", "is", "a", "test."])
scored_text_paragraph(df)
|