skip to main content

@tonyfast s notebooks

site navigation
notebook summary
title
improved output semantic for python objects
description
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.
cells
33 total
16 code
state
executed in order
kernel
Python [conda env:p311] *
language
python
name
conda-env-p311-py
lines of code
59
outputs
27
table of contents
{"kernelspec": {"display_name": "Python [conda env:p311] *", "language": "python", "name": "conda-env-p311-py"}, "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": "improved output semantic for python objects", "description": "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."}
notebook toolbar
Activate
cell ordering
1

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.

2

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

3

itemscope indicates that representation was generated for known type

4
    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
1 outputs.
5

comparing the standard dict repr with the semantic repr

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

a
1
b
abc
d
True
e
False
f
<dict object at 0x7fdcc9302a40>
7

comparing the standard None repr with the semantic repr

8
    print(repr(None))
    display(None)
2 outputs.
None

None
9

comparing the standard false repr with the semantic repr

10
    print(repr(False))
    False
2 outputs.
False

False
11

comparing the standard true repr with the semantic repr

12
    print(repr(True))
    True
2 outputs.
True

True
13

comparing the standard int repr with the semantic repr

14
    print(repr(1000))
    1000
2 outputs.
1000

1000
15

comparing the standard float repr with the semantic repr

16
    print(repr(x := 3.14159))
    x
2 outputs.
3.14159

3.14159
17

comparing the standard list repr with the semantic repr

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

  1. 1
  2. 2
  3. 3
19

comparing the standard tuple repr with the semantic repr

20
    print(repr(x:= (1,2,3,)))
    x
2 outputs.
(1, 2, 3)

  1. 1
  2. 2
  3. 3
21
22
    print(repr(x:= {1,2,3,}))
    x
2 outputs.
{1, 2, 3}

  • 1
  • 2
  • 3
23

comparing the standard dict repr with the semantic repr with recursion

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

a
1
b
abc
c
True
z
<dict object at 0x7fdcc9355900>
25
    import pandas
26

comparing the standard dataframe repr with the semantic repr

27
    df = pandas.DataFrame(range(x, x+10) for x in range(10))
    display({"text/html": df._repr_html_()}, raw=True)
    df
2 outputs.
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
index 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
28

comparing the standard ndarray repr with the semantic repr

29
    print(repr(df.values))
    df.values
2 outputs.
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
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
30

customize table size through pandas options

31
    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)
1 outputs.
rows
200
columns
100
indexes
rows
1
columns
1
index 0 1 hidden 98 99
0 0 1 hidden 98 99
1 1 2 hidden 99 100
hidden hidden hidden hidden hidden hidden
198 198 199 hidden 296 297
199 199 200 hidden 297 298
32

custom ndarray size with pandas options

33
    with pandas.option_context("display.max_columns", 4, "display.max_rows", 4):
        display(df.values)
1 outputs.
rows
200
columns
100
indexes
rows
0
columns
0
0 1 hidden 98 99
1 2 hidden 99 100
hidden hidden hidden hidden hidden
198 199 hidden 296 297
199 200 hidden 297 298