diff --git a/lmfdb/api/api.py b/lmfdb/api/api.py index 4800ca5f05..1b314ab748 100644 --- a/lmfdb/api/api.py +++ b/lmfdb/api/api.py @@ -33,6 +33,21 @@ def pretty_document(rec, sep=", ", id=True): return "{" + sep.join("'%s': %s" % attr for attr in attrs) + "}" +def schema_example(value, typ, max_len=100): + """ + String representation of one value from a random row, truncated + for display in the Example column of the schema table. + """ + if value is None: + return "" + if typ == "bytea" or isinstance(value, buffer): + return "[binary data]" + value = str(quote_string(value)) + if len(value) > max_len: + value = value[:max_len] + "..." + return value + + def hidden_collection(c): """ hide some collections from the main page (still available via direct requests) @@ -351,14 +366,26 @@ def apierror(msg, flash_extras=[], code=404, table=True): title = "Database - " + location bc = [("Database", url_for(".index")), (table,)] query_unquote = unquote(data["query"]) - description = coll.description() - if description: - title += " (%s)" % description + # The table description (the tables. knowl) is displayed by + # collection.html rather than being appended to the title + try: + # projection=3 selects every search and extra column (plus id), + # the same full mask the datapage query below uses, so the Example + # column can be populated for every row shown in the schema table. + example = coll.random(projection=3) + except Exception: + example = None + if example is None: + examples = {} + else: + examples = {table: {col: schema_example(example.get(col), typ) + for col, typ in coll.col_type.items()}} search_schema = [(col, coll.col_type[col]) for col in sorted(coll.search_cols)] return render_template("collection.html", title=title, search_schema={table: search_schema}, + examples=examples, single_object=single_object, query_unquote=query_unquote, url_args=url_args, diff --git a/lmfdb/api/templates/apidata.html b/lmfdb/api/templates/apidata.html index cbaf9ab1fb..cfebdb1795 100644 --- a/lmfdb/api/templates/apidata.html +++ b/lmfdb/api/templates/apidata.html @@ -4,14 +4,6 @@
diff --git a/lmfdb/api/templates/apischema.html b/lmfdb/api/templates/apischema.html index 8214401de6..79eab1f81c 100644 --- a/lmfdb/api/templates/apischema.html +++ b/lmfdb/api/templates/apischema.html @@ -1,15 +1,22 @@ Show schema -