sweetbean.util.rating
Build-time HTML helpers for cardinal-rating displays.
These are Python-time helpers — they return plain HTML strings to be
embedded into a stimulus's pre-rendered HTML or returned from a
FunctionVariable callable that runs at trial render time. They are
intentionally tiny (no runtime JS, no transpilation) so they slot
into either of the patterns described in
vendor/sweetbean/src/sweetbean/stimulus/AUTHORING.md:
- Pattern A (pre-render in Python): build the row HTML at construct
time and ship it as a string
TimelineVariable. - Pattern B (literals-only
FunctionVariable): the helpers are pure expressions, so you can copy the body into your ownFunctionVariablecallable if the row needs to depend on per-trial values that aren't known at build time. (Don't import these helpers inside aFunctionVariablecallable — they're not Transcrypt-safe.)
What's here
rating_bar_html(value, max_value, ...)
Single horizontal bar with optional numeric label. Replaces the
"row of pips" idiom that breaks down past max_value=4 or so.
rating_row_html(label, value, max_value, ...)
A two-column row: left = label (e.g. "Expert 1 (75%)"), right =
rating bar. This is the unit you usually want for "expert ratings"
style displays.
rating_card_html(title, rows_html, ...)
Thin wrapper that titles a stack of rating_row_html outputs
with a card border (e.g. one card per option in a
forced-choice task).
Why a horizontal bar instead of pips
Pips work for binary or ~3-level features but become visual clutter
fast (a row of 7 circles is hard to read at a glance). A filled bar
plus a numeric label scales naturally to any max_value and reads
the same way at max_value=1 (full or empty bar) as at
max_value=10 (a 4/10 looks like 40% fill). Same primitive, every
rating_max.
All helpers escape user-supplied label / title strings via
html.escape to keep the embedded HTML well-formed.
rating_bar_html(value, max_value, *, width_px=110, height_px=14, fill_color='#4682B4', track_color='rgba(255,255,255,0.08)', border_color='#888', show_value=True, value_units='')
Return HTML for a single horizontal rating bar.
A fixed-width track (width_px × height_px) is filled to
value / max_value. value is clamped to [0, max_value]
so out-of-range values render gracefully instead of overflowing
the track. When show_value is True, an aligned numeric label
("<value>/<max_value>" plus optional value_units) is
rendered to the right of the bar.
All visual params have defaults that match the dark-mode jsPsych defaults; override per-call if your stimulus uses a different palette.
Source code in sweetbean/util/rating.py
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | |
rating_card_from_values_html(title, labels, values, max_value, *, bar_kwargs=None, card_kwargs=None)
Convenience: build a rating card straight from parallel
labels and values iterables.
Equivalent to::
rating_card_html(
title,
[rating_row_html(lbl, v, max_value, bar_kwargs=bar_kwargs)
for lbl, v in zip(labels, values)],
**(card_kwargs or {}),
)
but spelled in one line at the call site, which is useful when you have one card per option in a forced-choice display.
Source code in sweetbean/util/rating.py
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | |
rating_card_html(title, rows_html, *, title_color='#ffffff', border_color='#888', background='transparent', min_width_px=240)
Bordered card with a title and a stack of rating rows.
rows_html may be a single concatenated HTML string or any
iterable of row strings (which are joined with no separator). The
card has a fixed minimum width so two adjacent cards line up cleanly
in a flex row even when their longest labels differ.
Source code in sweetbean/util/rating.py
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | |
rating_row_html(label, value, max_value, *, label_color='#dddddd', label_min_width_px=110, bar_kwargs=None)
A two-column row: left label + right rating bar.
Use one of these per feature/expert; stack them inside a
rating_card_html to build a per-option panel. label is
HTML-escaped, so pass a plain string like "Expert 1 (75%)".
Source code in sweetbean/util/rating.py
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | |