Skip to content

improved output semantic for python objectsยค

when we provide the right semantics to the outputs we increase the tactile representation on assistive techonology. these improvements make it easier for screen reader users to surface lists, tables, and links created from python.

we use the itemscope and itemtype properties as the proper semantic attributes to provide type information. these types extend to css selectors that can tailor specific type representations.

list of lists, sets, tuples found on this page by the orca screen reader list of tables found on this page by the orca screen reader

itemscope indicates that representation was generated for known type

    import nbconvert_a11y.tables
    from nbconvert_a11y.outputs import repr_html, repr_semantic
    import pandas
    %reload_ext nbconvert_a11y.tables
    %reload_ext nbconvert_a11y.outputs

comparing the standard dict repr with the semantic repr

    data = {'a': 1, 'b': 'abc', 'd': True, 'e': False}
    data["f"] = data
    print(repr(data))
    data
{'a': 1, 'b': 'abc', 'd': True, 'e': False, 'f': {...}}

comparing the standard None repr with the semantic repr

    print(repr(None))
    display(None)
None
None

comparing the standard false repr with the semantic repr

    print(repr(False))
    False
False
False

comparing the standard true repr with the semantic repr

    print(repr(True))
    True
True
True

comparing the standard int repr with the semantic repr

    print(repr(1000))
    1000
1000
1000

comparing the standard float repr with the semantic repr

    print(repr(x := 3.14159))
    x
3.14159
3.14159

comparing the standard list repr with the semantic repr

    print(repr(x:= [1,2,3,]))
    x
[1, 2, 3]
  1. 1
  2. 2
  3. 3

comparing the standard tuple repr with the semantic repr

    print(repr(x:= (1,2,3,)))
    x
(1, 2, 3)
  1. 1
  2. 2
  3. 3
    print(repr(x:= {1,2,3,}))
    x
{1, 2, 3}
  • 1
  • 2
  • 3

comparing the standard dict repr with the semantic repr with recursion

    x = dict(a=1, b="abc", c=True)
    x.update(z=x)
    print(repr(x))
    x
{'a': 1, 'b': 'abc', 'c': True, 'z': {...}}
    import pandas

comparing the standard dataframe repr with the semantic repr

    df = pandas.DataFrame(range(x, x+10) for x in range(10))
    display({"text/html": df._repr_html_()}, raw=True)
    df
0 1 2 3 4 5 6 7 8 9
0 0 1 2 3 4 5 6 7 8 9
1 1 2 3 4 5 6 7 8 9 10
2 2 3 4 5 6 7 8 9 10 11
3 3 4 5 6 7 8 9 10 11 12
4 4 5 6 7 8 9 10 11 12 13
5 5 6 7 8 9 10 11 12 13 14
6 6 7 8 9 10 11 12 13 14 15
7 7 8 9 10 11 12 13 14 15 16
8 8 9 10 11 12 13 14 15 16 17
9 9 10 11 12 13 14 15 16 17 18
rows
10
columns
10
indexes
rows
1
columns
1
index0123456789
00123456789
112345678910
2234567891011
33456789101112
445678910111213
5567891011121314
66789101112131415
778910111213141516
8891011121314151617
99101112131415161718

comparing the standard ndarray repr with the semantic repr

    print(repr(df.values))
    df.values
array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9],
       [ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10],
       [ 2,  3,  4,  5,  6,  7,  8,  9, 10, 11],
       [ 3,  4,  5,  6,  7,  8,  9, 10, 11, 12],
       [ 4,  5,  6,  7,  8,  9, 10, 11, 12, 13],
       [ 5,  6,  7,  8,  9, 10, 11, 12, 13, 14],
       [ 6,  7,  8,  9, 10, 11, 12, 13, 14, 15],
       [ 7,  8,  9, 10, 11, 12, 13, 14, 15, 16],
       [ 8,  9, 10, 11, 12, 13, 14, 15, 16, 17],
       [ 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]])
rows
10
columns
10
indexes
rows
0
columns
0
0123456789
12345678910
234567891011
3456789101112
45678910111213
567891011121314
6789101112131415
78910111213141516
891011121314151617
9101112131415161718

customize table size through pandas options

    df = pandas.DataFrame(range(x, x+100) for x in range(200))
    with pandas.option_context("display.max_columns", 4, "display.max_rows", 4):
        display(df)
rows
200
columns
100
indexes
rows
1
columns
1
index01hidden9899
001hidden9899
112hidden99100
hiddenhiddenhiddenhiddenhiddenhidden
198198199hidden296297
199199200hidden297298

custom ndarray size with pandas options

    with pandas.option_context("display.max_columns", 4, "display.max_rows", 4):
        display(df.values)
rows
200
columns
100
indexes
rows
0
columns
0
01hidden9899
12hidden99100
hiddenhiddenhiddenhiddenhidden
198199hidden296297
199200hidden297298