I have a follow-up question for this issue. The following code works well in other recipes I have written, but for some reason the checkbox state persists as "checked" with this recipe. I've tried clearing the cache, using an incognito browser, etc. I would like to use "view_id": "blocks",
in the return statement.
If I reload the page, the checkbox shows up unchecked as unloading.
import json
from typing import Dict, Generator
import prodigy
from prodigy.components.loaders import JSONL
from annotator.recipes.utils import EUP
UNLIMITED_ROWS = [
{"view_id": "html", "html_template":
"<div style=\"padding: 0 10px; border: 1px solid #ddd; border-radius: 4px; text-align: left;\">" +
"<label style=\"font-size: 14px; opacity: 0.90; margin-bottom: 10px;\">eup</label>" +
"<div style=\"max-height: 300px; overflow-y: scroll; margin-bottom: 10px;\">{{eup}}" +
"<br>" +
"<br>" +
"<label style=\"font-size: 14px; opacity: 0.90; margin-bottom: 10px;\">utterance</label>" +
"<div style=\"max-height: 300px; overflow-y: scroll; margin-bottom: 10px;\">{{utterance}}"
"</div>"
},
{"view_id": "choice"},
{"view_id": "html", "html_template": "<div style='float:left;'>" +
"<input type='checkbox' id='newname' name='new_name' value='New Name' style='margin-right:10px;' data-id='{{chat_id}}' onchange='update()'" +
"<label onclick='update()'>New Name</label>" +
"</div>"
},
{"view_id": "text_input", "field_label": "user input field"}
]
def add_options(
stream, label_field="label", choices=EUP
) -> Generator[Dict, None, None]:
"""
Convert each line in the ``stream`` to a ``task`` with a text and an
options field
:param stream: the input stream
:param label_field: key; defaults to "label"
:param choices: the different choices
:yield: a task Dict with text and options
"""
for line in stream:
if label_field in line:
options = json.loads(line[label_field])
else:
options = []
for word in (options + choices):
if word not in options:
options.append(word)
task = {
"eup": line["eup"],
"utterance": line["utterance"],
"newname": False,
"options": [
{"id": o, "deployment": o, "prompt": o,
"text": o} for o in options
]
}
yield task
@prodigy.recipe(
"eup",
dataset=("The dataset to save to", "positional", None, str),
file_path=("Path to texts", "positional", None, str),
label_field=("Label to use for the accept task", "option", "f", str),
choice_field=("Choices to add to the input", "option", "c", str)
)
def custom_labels(dataset, file_path, label_field, choice_field):
"""
Annotate the text with labels from the list from the ``label_field`` in
the input file. Augmented with choices from ``choice_field``.
"""
blocks = UNLIMITED_ROWS
stream = JSONL(file_path)
stream = add_options(stream) # add options to each task
javascript = """
function update(){
let checked = document.querySelector(".checkbox").checked;
prodigy.update({"checked": checked})
console.log("update ran");
}
document.addEventListener('prodigyanswer', event => {
const {answer, task} = event.detail;
console.log(answer);
console.log(task);
})
"""
return {
"dataset": dataset,
"view_id": "blocks",
"stream": list(stream),
"config": {
"blocks": blocks,
"javascript": javascript
}
}