diff options
author | Pjotr Prins | 2020-04-19 12:19:29 -0500 |
---|---|---|
committer | GitHub | 2020-04-19 12:19:29 -0500 |
commit | c4ba1c7b868b2b9b2b7c7b4f87e2856409b1767d (patch) | |
tree | eda50e35b2f3d65c7d71e11f8d02973075f0f498 | |
parent | f9d65865cf06bdd55ba40c932267b371484d5029 (diff) | |
parent | 528186eea8a70ecab6359a07aef3be87177d5144 (diff) | |
download | bh20-seq-resource-c4ba1c7b868b2b9b2b7c7b4f87e2856409b1767d.tar.gz bh20-seq-resource-c4ba1c7b868b2b9b2b7c7b4f87e2856409b1767d.tar.lz bh20-seq-resource-c4ba1c7b868b2b9b2b7c7b4f87e2856409b1767d.zip |
Merge pull request #24 from adamnovak/dropdowns
Add dropdown support to web uploader metadata form
-rw-r--r-- | bh20sequploader/bh20seq-options.yml | 17 | ||||
-rw-r--r-- | bh20sequploader/bh20seq-schema.yml | 2 | ||||
-rw-r--r-- | bh20simplewebuploader/main.py | 86 | ||||
-rw-r--r-- | bh20simplewebuploader/templates/form.html | 19 | ||||
-rw-r--r-- | setup.py | 2 |
5 files changed, 103 insertions, 23 deletions
diff --git a/bh20sequploader/bh20seq-options.yml b/bh20sequploader/bh20seq-options.yml new file mode 100644 index 0000000..d05be5a --- /dev/null +++ b/bh20sequploader/bh20seq-options.yml @@ -0,0 +1,17 @@ +# Contains suggested human-readable field values and their corresponding IRIs. +# Keyed on the field names in the types in the schema. Relies on field names +# being unique or at least using the same options in different containing +# types. + +host_age_unit: + Years: http://purl.obolibrary.org/obo/UO_0000036 + Months: http://purl.obolibrary.org/obo/UO_0000035 + Weeks: http://purl.obolibrary.org/obo/UO_0000034 + Days: http://purl.obolibrary.org/obo/UO_0000033 + Hours: http://purl.obolibrary.org/obo/UO_0000032 + +host_sex: + Male: http://purl.obolibrary.org/obo/NCIT_C20197 + Female: http://purl.obolibrary.org/obo/NCIT_C27993 + Intersex: http://purl.obolibrary.org/obo/NCIT_C45908 + Unknown: http://purl.obolibrary.org/obo/NCIT_C17998 diff --git a/bh20sequploader/bh20seq-schema.yml b/bh20sequploader/bh20seq-schema.yml index 7ffc15b..4cd0865 100644 --- a/bh20sequploader/bh20seq-schema.yml +++ b/bh20sequploader/bh20seq-schema.yml @@ -30,7 +30,7 @@ $graph: # jsonldPredicate: # _id: http://purl.obolibrary.org/obo/NOMEN_0000037 host_sex: - doc: Sex of the host as define in NCIT, IRI expected (http://purl.obolibrary.org/obo/C20197 (Male), http://purl.obolibrary.org/obo/NCIT_C27993 (Female) or unkown (http://purl.obolibrary.org/obo/NCIT_C17998)) + doc: Sex of the host as defined in NCIT, IRI expected (http://purl.obolibrary.org/obo/NCIT_C20197 (Male), http://purl.obolibrary.org/obo/NCIT_C27993 (Female), http://purl.obolibrary.org/obo/NCIT_C45908 (Intersex), or http://purl.obolibrary.org/obo/NCIT_C17998 (Unknown)) type: string jsonldPredicate: _id: http://purl.obolibrary.org/obo/PATO_0000047 diff --git a/bh20simplewebuploader/main.py b/bh20simplewebuploader/main.py index f5324a5..8c5c18c 100644 --- a/bh20simplewebuploader/main.py +++ b/bh20simplewebuploader/main.py @@ -7,7 +7,7 @@ import sys import re import string import yaml -import urllib.request +import pkg_resources from flask import Flask, request, redirect, send_file, send_from_directory, render_template import os.path @@ -25,7 +25,7 @@ app.config['MAX_CONTENT_LENGTH'] = 50 * 1024 * 1024 @app.errorhandler(413) def handle_large_file(e): return (render_template('error.html', - error_message="One of your files is too large. The maximum file size is 1 megabyte."), 413) + error_message="One of your files is too large. The maximum file size is 50 megabytes."), 413) def type_to_heading(type_name): @@ -49,12 +49,32 @@ def name_to_label(field_name): return string.capwords(field_name.replace('_', ' ')) -def generate_form(schema): +def is_iri(string): """ - Linearize the schema and send a bunch of dicts. + Return True if the given string looks like an IRI, and False otherwise. + + Used for finding type IRIs in the schema. + + Right now only supports http(s) URLs because that's all we have in our schema. + """ + + return string.startswith('http') + +def generate_form(schema, options): + """ + Linearize the schema into a list of dicts. + Each dict either has a 'heading' (in which case we put a heading for a form section in the template) or an 'id', 'label', 'type', and 'required' - (in which case we make a form field in the template). + (in which case we make a form field in the template). Non-heading dicts + with type 'select' will have an 'options' field, with a list of (name, + value) tuples, and represent a form dropdown element. Non-heading dicts may + have a human-readable 'docstring' field describing them. + + Takes the deserialized metadata schema YAML, and also a deserialized YAML + of option values. The option values are keyed on (unscoped) field name in + the schema, and each is a dict of human readable option -> corresponding + IRI. """ # Get the list of form components, one of which is the root @@ -90,16 +110,35 @@ def generate_form(schema): for field_name, field_type in by_name.get(type_name, {}).get('fields', {}).items(): # For each field - ref_url = None + ref_iri = None + docstring = None if not isinstance(field_type, str): # If the type isn't a string + + # It may have documentation + docstring = field_type.get('doc', None) + # See if it has a more info/what goes here URL predicate = field_type.get('jsonldPredicate', {}) - if not isinstance(predicate, str): - ref_url = predicate.get('_id', None) + # Predicate may be a URL, a dict with a URL in _id, maybe a + # dict with a URL in _type, or a dict with _id and _type but no + # URLs anywhere. Some of these may not technically be allowed + # by the format, but if they occur, we might as well try to + # handle them. + if isinstance(predicate, str): + if is_iri(predicate): + ref_iri = predicate else: - ref_url = predicate # not sure this is correct - # Grab out its type field + # Assume it's a dict. Look at the fields we know about. + for field in ['_id', 'type']: + field_value = predicate.get(field, None) + if isinstance(field_value, str) and is_iri(field_value) and ref_iri is None: + # Take the first URL-looking thing we find + ref_iri = field_value + break + + + # Now overwrite the field type with the actual type string field_type = field_type.get('type', '') # Decide if the field is optional (type ends in ?) @@ -115,14 +154,26 @@ def generate_form(schema): for item in walk_fields(field_type, parent_keys + [field_name], subtree_optional or optional): yield item else: - # We know how to make a string input + # This is a leaf field. We need an input for it. record = {} record['id'] = '.'.join(parent_keys + [field_name]) record['label'] = name_to_label(field_name) record['required'] = not optional and not subtree_optional - if ref_url: - record['ref_url'] = ref_url - if field_type == 'string': + if ref_iri: + record['ref_iri'] = ref_iri + if docstring: + record['docstring'] = docstring + + if field_name in options: + # The field will be a 'select' type no matter what its real + # data type is. + record['type'] = 'select' # Not a real HTML input type. It's its own tag. + # We have a set of values to present + record['options'] = [] + for name, value in options[field_name].items(): + # Make a tuple for each one + record['options'].append((name, value)) + elif field_type == 'string': record['type'] = 'text' # HTML input type elif field_type == 'int': record['type'] = 'number' @@ -133,9 +184,10 @@ def generate_form(schema): return list(walk_fields(root_name)) -# At startup, we need to load the current metadata schema so we can make a form for it -METADATA_SCHEMA = yaml.safe_load(urllib.request.urlopen('https://raw.githubusercontent.com/arvados/bh20-seq-resource/master/bh20sequploader/bh20seq-schema.yml')) -FORM_ITEMS = generate_form(METADATA_SCHEMA) +# At startup, we need to load the metadata schema from the uploader module, so we can make a form for it +METADATA_SCHEMA = yaml.safe_load(pkg_resources.resource_stream("bh20sequploader", "bh20seq-schema.yml")) +METADATA_OPTION_DEFINITIONS = yaml.safe_load(pkg_resources.resource_stream("bh20sequploader", "bh20seq-options.yml")) +FORM_ITEMS = generate_form(METADATA_SCHEMA, METADATA_OPTION_DEFINITIONS) @app.route('/') def send_form(): diff --git a/bh20simplewebuploader/templates/form.html b/bh20simplewebuploader/templates/form.html index df66e8c..6993cf5 100644 --- a/bh20simplewebuploader/templates/form.html +++ b/bh20simplewebuploader/templates/form.html @@ -211,7 +211,6 @@ <div id="metadata_fill_form_spot"> <div id="metadata_fill_form"> - {{ record }} {% for record in fields %} {% if 'heading' in record %} @@ -221,15 +220,27 @@ <div class="record"> <h4>{{ record['heading'] }}</h4> {% else %} - <label for="{{ record['id'] }}"> + <label for="{{ record['id'] }}" title="{{ record.get('docstring', '') }}"> {{ record['label'] }} {{ "*" if record['required'] else "" }} - {% if 'ref_url' in record %} - <a href="{{ record['ref_url'] }}" title="More Info" target="_blank">?</a> + {% if 'docstring' in record %} + <a href='javascript:alert({{ record['docstring'] | tojson }})'>❓</a> + {% endif %} + {% if 'ref_iri' in record %} + <a href="{{ record['ref_iri'] }}" target="_blank" title="Ontology Link">🔗</a> {% endif %} </label> + {% if record['type'] == 'select' %} + <select id="{{ record['id'] }}" name="{{ record['id'] }}" {{ "required" if record['required'] else "" }}> + <option value="" selected>Choose one...</option> + {% for option in record['options'] %} + <option value="{{ option[1] }}">{{ option[0] }}</option> + {% endfor %} + </select> + {% else %} <input type="{{ record['type'] }}" id="{{ record['id'] }}" name="{{ record['id'] }}" {{ "required" if record['required'] else "" }}> {% endif %} + {% endif %} {% if loop.index == loop.length %} </div> {% endif %} @@ -31,7 +31,7 @@ setup( author_email="peter.amstutz@curii.com", license="Apache 2.0", packages=["bh20sequploader", "bh20seqanalyzer", "bh20simplewebuploader"], - package_data={"bh20sequploader": ["bh20seq-schema.yml", "validation/formats"], + package_data={"bh20sequploader": ["bh20seq-schema.yml", "bh20seq-options.yml", "validation/formats"], }, install_requires=install_requires, extras_require={ |