Skip to content

Commit

Permalink
Add docstring for classification_label
Browse files Browse the repository at this point in the history
  • Loading branch information
wch committed Jul 17, 2023
1 parent d1a953e commit 96772f7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
3 changes: 2 additions & 1 deletion js/ml/classification-label.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export class ShinyClassificationLabel extends LitElement {
displayWinner: number = 0;
@property({ type: Number, attribute: "max-items" })
maxItems: number | null = null;
@property({ type: String, attribute: "suffix" }) suffix: string = "%";

render() {
let entries = Object.entries(this.value);
Expand Down Expand Up @@ -81,7 +82,7 @@ export class ShinyClassificationLabel extends LitElement {

const valuesHtml = entries.map(([k, v]) => {
return html`<div class="item">
<div class="bar" style="width: ${v}%;"></div>
<div class="bar" style="width: ${v}${this.suffix};"></div>
<div class="label">
<div>${k}</div>
<div class="dashed-line"></div>
Expand Down
39 changes: 32 additions & 7 deletions shiny/ui/ml.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,57 @@

import json

from htmltools import HTML, HTMLDependency, Tag, TagAttrValue, html_escape
from htmltools import HTML, HTMLDependency, Tag, html_escape

from .. import __version__

# from .._docstring import add_example
# from .._namespaces import resolve_id


def classification_label(
value: dict[str, float],
*,
sort: Optional[bool] = None,
sort: bool = True,
display_winner: Optional[bool] = None,
max_items: Optional[int] = None,
_add_ws: bool = True,
**kwargs: TagAttrValue,
suffix: str = "%",
) -> Tag:
"""
Create a classification label with confidence scores.
This is meant to be used to display classification results from a model. The
component itself is static, so to display dynamic values, it would typically be used
with a :func:`~shiny.ui.ouput_ui` and :func:`~shiny.render.ui`.
Parameters
----------
value
A dictionary with class names as keys and and confidence scores as values.
sort
Should the values be sorted? Defaults to ``True``.
display_winner:
If ``True`` (the default), then the name of the winner will be displayed above
the values, in larger text.
max_items:
The maximum number of items to display. Defaults to ``None``, which means all
items will be displayed.
suffix:
A string to place after each value. Defaults to ``"%"``.
_add_ws:
Returns
-------
:
A UI element.
"""
return Tag(
"shiny-classification-label",
ml_dep(),
value=attr_to_escaped_json(value),
sort=bool_to_num(sort),
display_winner=bool_to_num(display_winner),
max_items=max_items,
_add_ws=_add_ws,
**kwargs,
suffix=suffix,
)


Expand Down

0 comments on commit 96772f7

Please sign in to comment.