skip to main content

@tonyfast s notebooks

site navigation
notebook summary
title
semantic representation of columns based on schema
description
schema may define the columnar properties of a table. that can be canonically presented using the colgroup and col elements that allow for bulk operations on column styles.
cells
3 total
2 code
state
executed out of order
kernel
pidgy [conda env:p311] *
language
markdown
name
conda-env-p311-pidgy
lines of code
34
outputs
3
table of contents
{"kernelspec": {"display_name": "pidgy [conda env:p311] *", "language": "markdown", "name": "conda-env-p311-pidgy"}, "language_info": {"codemirror_mode": {"name": "ipython", "version": 3}, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.3"}, "widgets": {"application/vnd.jupyter.widget-state+json": {"state": {}, "version_major": 2, "version_minor": 0}}, "title": "semantic representation of columns based on schema", "description": "schema may define the columnar properties of a table.\nthat can be canonically presented using the colgroup and col elements\nthat allow for bulk operations on column styles."}
notebook toolbar
Activate
cell ordering
1

semantic representation of columns based on schema

schema may define the columnar properties of a table. that can be canonically presented using the colgroup and col elements that allow for bulk operations on column styles.

2
make a schema with subschema

    (schema := tomli.loads("".join(

```toml
[[allOf]]
"$ref" = "#/$defs/A"
[[allOf]]
"$ref" = "#/$defs/B"
["$defs".A.properties.foo]
["$defs".A.properties.bar]
["$defs".B.properties.baz]
```

    .splitlines(1)[1:-1])))
2 outputs.
{'allOf': [{'$ref': '#/$defs/A'}, {'$ref': '#/$defs/B'}],
 '$defs': {'A': {'properties': {'foo': {}, 'bar': {}}},
  'B': {'properties': {'baz': {}}}}}

make a schema with subschema

(schema := tomli.loads("".join(
[[allOf]]
"$ref" = "#/$defs/A"
[[allOf]]
"$ref" = "#/$defs/B"
["$defs".A.properties.foo]
["$defs".A.properties.bar]
["$defs".B.properties.baz]
.splitlines(1)[1:-1])))
3
the form we create uses `colgroup` and `col` elements as demonstrated next

```html
{{table.prettify()}}
```

and it is generated directly from schema using the following code generate example `colgroup` and `col` elements

    import bs4, jsonpointer
    soup = bs4.BeautifulSoup(features="html5lib")
    soup.append(table := soup.new_tag("table"))
    table.append(colgroup := soup.new_tag("colgroup"))
    table.append(props := soup.new_tag("colgroup"))
    for s in schema["allOf"]:
        id = jsonpointer.JsonPointer.from_parts((ref:=s.get("$ref")).split("/")[1:])
        colgroup.append(col := soup.new_tag("col"))
        p = id.get(schema).get("properties")
        col.attrs.update(id=str(id), span=len(p))
        for k in p:
            props.append(x := soup.new_tag("col"))
            x.attrs.update(id=jsonpointer.JsonPointer.from_parts(ref.split("/")[1:] + ["properties", k]))
1 outputs.

the form we create uses colgroup and col elements as demonstrated next

<table>
 <colgroup>
  <col id="/$defs/A" span="2"/>
  <col id="/$defs/B" span="1"/>
 </colgroup>
 <colgroup>
  <col id="/$defs/A/properties/foo"/>
  <col id="/$defs/A/properties/bar"/>
  <col id="/$defs/B/properties/baz"/>
 </colgroup>
</table>

and it is generated directly from schema using the following code generate example colgroup and col elements

import bs4, jsonpointer
soup = bs4.BeautifulSoup(features="html5lib")
soup.append(table := soup.new_tag("table"))
table.append(colgroup := soup.new_tag("colgroup"))
table.append(props := soup.new_tag("colgroup"))
for s in schema["allOf"]:
    id = jsonpointer.JsonPointer.from_parts((ref:=s.get("$ref")).split("/")[1:])
    colgroup.append(col := soup.new_tag("col"))
    p = id.get(schema).get("properties")
    col.attrs.update(id=str(id), span=len(p))
    for k in p:
        props.append(x := soup.new_tag("col"))
        x.attrs.update(id=jsonpointer.JsonPointer.from_parts(ref.split("/")[1:] + ["properties", k]))