Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Miscellaneous cleanup #20

Merged
merged 1 commit into from
Nov 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion potato/create_task_cli.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import json
import yaml
from collections import OrderedDict
import os

Expand Down
688 changes: 256 additions & 432 deletions potato/flask_server.py

Large diffs are not rendered by default.

54 changes: 21 additions & 33 deletions potato/server_utils/front_end.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,15 @@ def generate_keybindings_sidebar(keybindings, horizontal=False):
Generate an HTML layout for the end-user of the keybindings for the current
task. The layout is intended to be displayed in a side bar
"""
if "horizontal_key_bindings" in config and config["horizontal_key_bindings"]:
if config.get("horizontal_key_bindings"):
horizontal = True

if not keybindings:
return ""
# keybindings.insert(0, ('key', 'description'))

if horizontal:
keybindings = [[it[0], it[1].split(":")[-1]] for it in keybindings]
lines = list(zip(*keybindings))
print(lines)
layout = '<table style="border:1px solid black;margin-left:auto;margin-right:auto;text-align: center;">'
for line in lines:
layout += (
Expand All @@ -94,7 +93,6 @@ def generate_statistics_sidebar(statistics):
Generate an HTML layout for the end-user of the statistics for the current
task. The layout is intended to be displayed in a side bar
"""

layout = "<table><tr><th> </th><th> </th></tr>"
for key in statistics:
desc = "{{statistics_nav['%s']}}" % statistics[key]
Expand Down Expand Up @@ -154,7 +152,7 @@ def generate_site(config):

html_template = html_template.replace("{{ HEADER }}", header)

if "jumping_to_id_disabled" in config and config["jumping_to_id_disabled"]:
if config.get("jumping_to_id_disabled"):
html_template = html_template.replace(
'<input type="submit" value="go">', '<input type="submit" value="go" hidden>'
)
Expand All @@ -163,7 +161,7 @@ def generate_site(config):
'<input type="number" name="go_to" id="go_to" value="" onfocusin="user_input()" onfocusout="user_input_leave()" max={{total_count}} min=0 required hidden>',
)

if "hide_navbar" in config and config["hide_navbar"]:
if config.get("hide_navbar"):
html_template = html_template.replace(
'<div class="navbar-nav">', '<div class="navbar-nav" hidden>'
)
Expand Down Expand Up @@ -200,8 +198,7 @@ def generate_site(config):

# Potato admin can specify a custom HTML layout that allows variable-named
# placement of task elements
if "custom_layout" in config and config["custom_layout"]:

if config.get("custom_layout"):
for annotation_scheme in annotation_schemes:
schema_layout, keybindings = generate_schematic(annotation_scheme)
all_keybindings.extend(keybindings)
Expand Down Expand Up @@ -235,7 +232,7 @@ def generate_site(config):

# Add in a codebook link if the admin specified one
codebook_html = ""
if "annotation_codebook_url" in config and len(config["annotation_codebook_url"]) > 0:
if len(config.get("annotation_codebook_url", "")) > 0:
annotation_codebook = config["annotation_codebook_url"]
codebook_html = '<a href="{{annotation_codebook_url}}" class="nav-item nav-link">Annotation Codebook</a>'
codebook_html = codebook_html.replace("{{annotation_codebook_url}}", annotation_codebook)
Expand All @@ -246,9 +243,7 @@ def generate_site(config):

# Swap in the task's layout
html_template = html_template.replace("{{ TASK_LAYOUT }}", task_html_layout)

html_template = html_template.replace("{{annotation_codebook}}", codebook_html)

html_template = html_template.replace(
"{{annotation_task_name}}", config["annotation_task_name"]
)
Expand Down Expand Up @@ -328,7 +323,7 @@ def generate_surveyflow_pages(config):

html_template = html_template.replace("{{ HEADER }}", header)

if "jumping_to_id_disabled" in config and config["jumping_to_id_disabled"]:
if config.get("jumping_to_id_disabled"):
html_template = html_template.replace(
'<input type="submit" value="go">', '<input type="submit" value="go" hidden>'
)
Expand All @@ -337,7 +332,7 @@ def generate_surveyflow_pages(config):
'<input type="number" name="go_to" id="go_to" value="" onfocusin="user_input()" onfocusout="user_input_leave()" max={{total_count}} min=0 required hidden>',
)

if "hide_navbar" in config and config["hide_navbar"]:
if config.get("hide_navbar"):
html_template = html_template.replace(
'<div class="navbar-nav">', '<div class="navbar-nav" hidden>'
)
Expand All @@ -355,8 +350,7 @@ def generate_surveyflow_pages(config):

if not os.path.exists(abs_html_layout_file):
raise FileNotFoundError("html_layout not found: %s" % html_layout_file)
else:
html_layout_file = abs_html_layout_file
html_layout_file = abs_html_layout_file

with open(html_layout_file, "rt") as f:
task_html_layout = "".join(f.readlines())
Expand All @@ -374,7 +368,7 @@ def generate_surveyflow_pages(config):

# Add in a codebook link if the admin specified one
codebook_html = ""
if "annotation_codebook_url" in config and len(config["annotation_codebook_url"]) > 0:
if len(config.get("annotation_codebook_url", "")) > 0:
annotation_codebook = config["annotation_codebook_url"]
codebook_html = '<a href="{{annotation_codebook_url}}" class="nav-item nav-link">Annotation Codebook</a>'
codebook_html = codebook_html.replace("{{annotation_codebook_url}}", annotation_codebook)
Expand All @@ -398,13 +392,13 @@ def generate_surveyflow_pages(config):
surveyflow_list = []
for key in surveyflow["order"]:
surveyflow_list += surveyflow[key]
for file in surveyflow_list:
if file.split(".")[-1] == "jsonl":
with open(file, "r") as r:
for _file in surveyflow_list:
if _file.split(".")[-1] == "jsonl":
with open(_file, "r") as r:
for line in r:
line = json.loads(line.strip())
line["filename"] = file
line["pagename"] = file.split(".")[0].split("/")[-1]
line["filename"] = _file
line["pagename"] = _file.split(".")[0].split("/")[-1]
surveyflow_pages[line["pagename"]].append(line)

# Grab the annotation schemes
Expand All @@ -416,7 +410,7 @@ def generate_surveyflow_pages(config):

# Potato admin can specify a custom HTML layout that allows variable-named
# placement of task elements
if "custom_layout" in config and config["custom_layout"]:
if config.get("custom_layout"):

for annotation_scheme in annotation_schemes:
schema_layout, keybindings = generate_schematic(annotation_scheme)
Expand Down Expand Up @@ -448,24 +442,19 @@ def generate_surveyflow_pages(config):
annotation_scheme = {
"annotation_type": line["schema"],
# todo: pack select type in to a dict with the key 'schema'
# whether use predefined labels for select type, if so, define it, currently we support country, religion, ethnicity
"use_predefined_labels": line["use_predefined_labels"]
if "use_predefined_labels" in line
else None,
# whether use predefined labels for select type, if so,
# define it, currently we support country, religion, ethnicity
"use_predefined_labels": line.get("use_predefined_labels"),
"id": line["id"],
"name": line["text"],
"description": line["text"],
# If true, display the labels horizontally
"horizontal": False,
"labels": line["choices"] if "choices" in line else None,
"label_requirement": line["label_requirement"]
if "label_requirement" in line
else None,
"labels": line.get("choices"),
"label_requirement": line.get("label_requirement"),
"sequential_key_binding": False,
}
schema_layout, keybindings = generate_schematic(annotation_scheme)
schema_layouts += schema_layout + "<br>" + "\n"
# all_keybindings.extend(keybindings)

cur_task_html_layout = task_html_layout.replace(
"{{annotation_schematic}}", schema_layouts
Expand Down Expand Up @@ -519,4 +508,3 @@ def generate_surveyflow_pages(config):
for it in config["surveyflow"][key]
]
config["non_annotation_pages"] += config["%s_pages" % key]
# config['post_annotation_pages'] = [config['surveyflow_site_file'][it.split('.')[0].split('/')[-1]] for it in config['surveyflow']['post_annotation']]
43 changes: 12 additions & 31 deletions potato/server_utils/schemas/likert.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,11 @@ def generate_likert_layout(annotation_scheme):
if "labels" in annotation_scheme:
return generate_radio_layout(annotation_scheme, horizontal=False)

if "size" not in annotation_scheme:
raise Exception('Likert scale for "%s" did not include size' % annotation_scheme["name"])
if "min_label" not in annotation_scheme:
raise Exception(
'Likert scale for "%s" did not include min_label' % annotation_scheme["name"]
)
if "max_label" not in annotation_scheme:
raise Exception(
'Likert scale for "%s" did not include max_label' % annotation_scheme["name"]
)
for required in ["size", "min_label", "max_label"]:
if required not in annotation_scheme:
raise Exception(
'Likert scale for "%s" did not include %s' % (annotation_scheme["name"], required)
)

schematic = (
'<div><form action="/action_page.php">'
Expand All @@ -32,14 +27,11 @@ def generate_likert_layout(annotation_scheme):
label2key = {}
key_bindings = []

# setting up label validation for each label, if "required" is True, the annotators will be asked to finish the current instance to proceed
# Setting up label validation for each label, if "required" is True,
# the annotators will be asked to finish the current instance to proceed
validation = ""
label_requirement = (
annotation_scheme["label_requirement"]
if "label_requirement" in annotation_scheme
else None
)
if label_requirement and ("required" in label_requirement) and label_requirement["required"]:
label_requirement = annotation_scheme.get("label_requirement")
if label_requirement and label_requirement.get("required"):
validation = "required"

for i in range(1, annotation_scheme["size"] + 1):
Expand All @@ -51,21 +43,13 @@ def generate_likert_layout(annotation_scheme):
key_value = str(i % 10)

# if the user wants us to add in easy key bindings
if (
"sequential_key_binding" in annotation_scheme
and annotation_scheme["sequential_key_binding"]
and annotation_scheme["size"] <= 10
):
if annotation_scheme.get("sequential_key_binding") and annotation_scheme["size"] <= 10:
key2label[key_value] = label
label2key[label] = key_value
key_bindings.append((key_value, class_name + ": " + key_value))

# In the collapsed version of the likert scale, no label is shown.
label_content = (
str(i)
if ("displaying_score" in annotation_scheme and annotation_scheme["displaying_score"])
else ""
)
label_content = str(i) if annotation_scheme.get("displaying_score") else ""
tooltip = ""

# displaying the label content in a different line if it is not empty
Expand Down Expand Up @@ -97,10 +81,7 @@ def generate_likert_layout(annotation_scheme):

# allow annotators to choose bad_text label
bad_text_schematic = ""
if (
"bad_text_label" in annotation_scheme
and "label_content" in annotation_scheme["bad_text_label"]
):
if "label_content" in annotation_scheme.get("bad_text_label", {}):
name = annotation_scheme["name"] + ":::" + "bad_text"
bad_text_schematic = (
(
Expand Down
4 changes: 1 addition & 3 deletions potato/server_utils/schemas/multiselect.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ def generate_multiselect_layout(annotation_scheme):
# setting up label validation for each label, if "required" is True, the annotators will be asked to finish the current instance to proceed
validation = ""
label_requirement = (
annotation_scheme["label_requirement"]
if "label_requirement" in annotation_scheme
else None
annotation_scheme["label_requirement"] if "label_requirement" in annotation_scheme else None
)
if label_requirement and label_requirement["required"]:
validation = "required"
Expand Down
4 changes: 1 addition & 3 deletions potato/server_utils/schemas/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,7 @@ def generate_number_layout(annotation_scheme):
# setting up label validation for each label, if "required" is True, the annotators will be asked to finish the current instance to proceed
validation = ""
label_requirement = (
annotation_scheme["label_requirement"]
if "label_requirement" in annotation_scheme
else None
annotation_scheme["label_requirement"] if "label_requirement" in annotation_scheme else None
)
if label_requirement and "required" in label_requirement and label_requirement["required"]:
validation = "required"
Expand Down
4 changes: 1 addition & 3 deletions potato/server_utils/schemas/radio.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ def generate_radio_layout(annotation_scheme, horizontal=False):
# annotators will be asked to finish the current instance to proceed
validation = ""
label_requirement = (
annotation_scheme["label_requirement"]
if "label_requirement" in annotation_scheme
else None
annotation_scheme["label_requirement"] if "label_requirement" in annotation_scheme else None
)
if label_requirement and ("required" in label_requirement) and label_requirement["required"]:
validation = "required"
Expand Down
8 changes: 2 additions & 6 deletions potato/server_utils/schemas/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ def generate_select_layout(annotation_scheme):
# setting up label validation for each label, if "required" is True, the annotators will be asked to finish the current instance to proceed
validation = ""
label_requirement = (
annotation_scheme["label_requirement"]
if "label_requirement" in annotation_scheme
else None
annotation_scheme["label_requirement"] if "label_requirement" in annotation_scheme else None
)
if label_requirement and ("required" in label_requirement) and label_requirement["required"]:
validation = "required"
Expand Down Expand Up @@ -49,9 +47,7 @@ def generate_select_layout(annotation_scheme):

else:
# if annotation_scheme['labels'] is defined as a path
if type(annotation_scheme["labels"]) == str and os.path.exists(
annotation_scheme["labels"]
):
if type(annotation_scheme["labels"]) == str and os.path.exists(annotation_scheme["labels"]):
with open(annotation_scheme["labels"], "r") as r:
labels = [it.strip() for it in r.readlines()]
else:
Expand Down
4 changes: 1 addition & 3 deletions potato/server_utils/schemas/span.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,7 @@ def generate_span_layout(annotation_scheme, horizontal=False):
# setting up label validation for each label, if "required" is True, the annotators will be asked to finish the current instance to proceed
validation = ""
label_requirement = (
annotation_scheme["label_requirement"]
if "label_requirement" in annotation_scheme
else None
annotation_scheme["label_requirement"] if "label_requirement" in annotation_scheme else None
)
if label_requirement and ("required" in label_requirement) and label_requirement["required"]:
validation = "required"
Expand Down