Spaces:
Sleeping
Sleeping
| ## 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 = | |
| text = ["This", "is", "a", "test."]) | |
| scored_text_paragraph(df) | |