index
execution_count
cell_type
toolbar
started_at
completed_at
source
loc
metadata
outputs
1
unexecuted
In
[
]
markdown
# using tables to structure tracebacks
tracebacks are critical to improving code, but they must something that can be explored. this is not the case for AT the moment. exceptions are represented as long strings with no structure.
this approach uses to tables to structure tracebacks with improve the quality of experience for screen readers.
metadata
5
tracebacks are critical to improving code, but they must something that can be explored. this is not the case for AT the moment. exceptions are represented as long strings with no structure.
this approach uses to tables to structure tracebacks with improve the quality of experience for screen readers.
2
unexecuted
In
[
]
markdown
we still need to surface the code hints provided by the new traceback module works
https://github.com/python/cpython/blob/main/Lib/traceback.py#L544
metadata
3
we still need to surface the code hints provided by the new traceback module works
https://github.com/python/cpython/blob/main/Lib/traceback.py#L544
3
executed
In
[
1
]
code
import sysconfig , operator , pathlib
import traceback , contextlib , pandas , nbconvert_a11y
% reload_ext nbconvert_a11y . outputs
% reload_ext nbconvert_a11y . tables
from nbconvert_a11y import tables
metadata
5
2 outputs.
Out
[
1
]
/tmp/ipykernel_53456/3040624387.py:2: DeprecationWarning:
Pyarrow will become a required dependency of pandas in the next major release of pandas (pandas 3.0),
(to allow more performant data types, such as the Arrow string type, and better interoperability with other libraries)
but was not found to be installed on your system.
If this would cause problems for you,
please provide us feedback at https://github.com/pandas-dev/pandas/issues/54466
import traceback, contextlib, pandas, nbconvert_a11y
4
executed
In
[
2
]
code
def multiply_wrong ( x ):
"a function that wil raise an error"
x [: - 1 ] . T @x
metadata
3
0 outputs.
Out
[
2
]
5
unexecuted
In
[
]
markdown
metadata
1
create an exception
6
executed
In
[
3
]
code
try :
pandas . Series ( range ( 10 )) . to_frame () . pipe ( multiply_wrong )
except ValueError as e :
exception = e
metadata
4
0 outputs.
Out
[
3
]
7
executed
In
[
4
]
code
traceback . print_exception ( exception )
metadata
1
1 outputs.
Out
[
4
]
Traceback (most recent call last):
File "/tmp/ipykernel_53456/1648588067.py", line 2, in <module>
pandas.Series(range(10)).to_frame().pipe(multiply_wrong)
File "/home/tbone/mambaforge/envs/p311/lib/python3.11/site-packages/pandas/core/generic.py", line 6225, in pipe
return common.pipe(self, func, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/tbone/mambaforge/envs/p311/lib/python3.11/site-packages/pandas/core/common.py", line 500, in pipe
return func(obj, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/tmp/ipykernel_53456/3574275760.py", line 3, in multiply_wrong
x[:-1].T@x
~~~~~~~~^~
File "/home/tbone/mambaforge/envs/p311/lib/python3.11/site-packages/pandas/core/frame.py", line 1775, in __matmul__
return self.dot(other)
^^^^^^^^^^^^^^^
File "/home/tbone/mambaforge/envs/p311/lib/python3.11/site-packages/pandas/core/frame.py", line 1725, in dot
raise ValueError("matrices are not aligned")
ValueError: matrices are not aligned
8
unexecuted
In
[
]
markdown
turn the exceptions traceback into an array of arrays
metadata
1
turn the exceptions traceback into an array of arrays
9
executed
In
[
5
]
code
def get_exception_array ( exception ):
return [
list ( frame ) + [ frame . colno , frame . end_colno ][: 0 ] for frame in traceback . extract_tb ( exception . __traceback__ )
]
metadata
4
0 outputs.
Out
[
5
]
10
unexecuted
In
[
]
markdown
format the exception as a dataframe
metadata
1
format the exception as a dataframe
11
executed
In
[
6
]
code
df = pandas . DataFrame (
get_exception_array ( exception ),
columns = [ "file" , "line" , "method" , "source" ]
)[[ "line" , "method" , "source" , "file" ]]
metadata
4
0 outputs.
Out
[
6
]
12
unexecuted
In
[
]
markdown
the filename makes it really hard to reason with what is being reported.
it would be good to extract module name instead even though it is not part of the current repr
metadata
2
the filename makes it really hard to reason with what is being reported.
it would be good to extract module name instead even though it is not part of the current repr
13
unexecuted
In
[
]
markdown
the path locations that we can use to shorten the file representation
metadata
1
the path locations that we can use to shorten the file representation
14
executed
In
[
7
]
code
def get_paths ():
import ipykernel
sys = sysconfig . get_paths ()
sys . update ( __main__ = ipykernel . compiler . get_tmp_directory ())
return sys
metadata
5
0 outputs.
Out
[
7
]
15
executed
In
[
8
]
code
def shorten_path ( paths ):
sys = get_paths ()
visited = set ()
relative = [ None ] * len ( paths )
for lib in ( "__main__" , "purelib" , "platlib" , "stdlib" , "platstdlib" ):
value = pathlib . Path ( sys [ lib ])
if value not in visited :
for i , path in enumerate ( paths ):
if relative [ i ] is not None :
continue
try :
relative [ i ] = F " { lib } : { pathlib . Path ( path ) . relative_to ( value ) } "
except ValueError :
pass
visited . add ( value )
return relative
metadata
17
0 outputs.
Out
[
8
]
16
unexecuted
In
[
]
markdown
the dataframe indexes by shorted filenames making it easier to listen to
metadata
1
the dataframe indexes by shorted filenames making it easier to listen to
17
unexecuted
In
[
]
markdown
the traceback table is nested inside a figcaption that captures externals messages contained on the exception.
metadata
1
the traceback table is nested inside a figcaption that captures externals messages contained on the exception.
18
executed
In
[
9
]
code
pandas . DataFrame ( get_exception_array ( exception ), columns = [ "file" , "line" , "method" , "source" ])
metadata
1
1 outputs.
Out
[
9
]
rows
6
columns
4
indexes
rows
1
columns
1
index
file
line
method
source
0
/tmp/ipykernel_53456/1648588067.py
2
<module>
pandas.Series(range(10)).to_frame().pipe(multiply_wrong)
1
/home/tbone/mambaforge/envs/p311/lib/python3.11/site-packages/pandas/core/generic.py
6225
pipe
return common.pipe(self, func, *args, **kwargs)
2
/home/tbone/mambaforge/envs/p311/lib/python3.11/site-packages/pandas/core/common.py
500
pipe
return func(obj, *args, **kwargs)
3
/tmp/ipykernel_53456/3574275760.py
3
multiply_wrong
x[:-1].T@x
4
/home/tbone/mambaforge/envs/p311/lib/python3.11/site-packages/pandas/core/frame.py
1775
__matmul__
return self.dot(other)
5
/home/tbone/mambaforge/envs/p311/lib/python3.11/site-packages/pandas/core/frame.py
1725
dot
raise ValueError("matrices are not aligned")
19
executed
In
[
10
]
code
def repr_traceback_frame ( exception ):
df = pandas . DataFrame ( get_exception_array ( exception ), columns = [ "file" , "line" , "method" , "source" ])
df = df . set_index ([ pandas . Index (
df . file . pipe ( shorten_path ),
name = "module"
)])
return df
def repr_traceback ( exception ):
figure = nbconvert_a11y . outputs . new (
"figure" ,
tables . get_table ( repr_traceback_frame ( exception ), type_ = type ( exception ), COL_INDEX = tables . SHOW_INDEX . nonvisual , ROW_INDEX = tables . SHOW_INDEX . nonvisual , SEMANTIC = False ),
nbconvert_a11y . outputs . new ( "figcaption" , F " { type ( exception ) . __name__ } : { exception } " )
)
return str ( figure )
metadata
15
0 outputs.
Out
[
10
]
20
unexecuted
In
[
]
markdown
an accessible figure/table for the traceback
metadata
1
an accessible figure/table for the traceback
21
executed
In
[
11
]
code
display ({ "text/html" : repr_traceback ( exception )}, raw = True )
metadata
1
1 outputs.
Out
[
11
]
rows
6
columns
4
indexes
rows
1
columns
1
module
file
line
method
source
__main__:1648588067.py
/tmp/ipykernel_53456/1648588067.py
2
<module>
pandas.Series(range(10)).to_frame().pipe(multiply_wrong)
purelib:pandas/core/generic.py
/home/tbone/mambaforge/envs/p311/lib/python3.11/site-packages/pandas/core/generic.py
6225
pipe
return common.pipe(self, func, *args, **kwargs)
purelib:pandas/core/common.py
/home/tbone/mambaforge/envs/p311/lib/python3.11/site-packages/pandas/core/common.py
500
pipe
return func(obj, *args, **kwargs)
__main__:3574275760.py
/tmp/ipykernel_53456/3574275760.py
3
multiply_wrong
x[:-1].T@x
purelib:pandas/core/frame.py
/home/tbone/mambaforge/envs/p311/lib/python3.11/site-packages/pandas/core/frame.py
1775
__matmul__
return self.dot(other)
purelib:pandas/core/frame.py
/home/tbone/mambaforge/envs/p311/lib/python3.11/site-packages/pandas/core/frame.py
1725
dot
raise ValueError("matrices are not aligned")
ValueError: matrices are not aligned
22
unexecuted
In
[
]
markdown
custom css to make the table appear similar to native tracebacks
metadata
1
custom css to make the table appear similar to native tracebacks
23
executed
In
[
12
]
code
%% html
< style >
table [ itemscope ]:: before {
content : "Traceback (most recent call last):" ;
}
table [ itemscope ] {
td data { color : unset ;}
tr : first - child , tr > th : first - child {
clip : rect ( 0 0 0 0 );
clip - path : inset ( 50 % );
height : 1 px ;
overflow : hidden ;
position : absolute ;
white - space : nowrap ;
width : 1 px ;
}
tr {
display : block ;
position : relative ;
td {
display : inline ;
}
td : nth - of - type ( 1 ):: before { content : 'File "' ;}
td : nth - of - type ( 1 ):: after { content : '", ' ;}
td : nth - of - type ( 2 ):: before { content : 'line ' ;}
td : nth - of - type ( 2 ):: after { content : ', ' ;}
td : nth - of - type ( 3 ):: before { content : 'in ' ;}
td {
left : 2 em ;
position : relative ;
}
td : nth - of - type ( 4 ) {
display : block ;
left : 4 em ;
}
}
}
figure {
figcaption {
font - family : monospace ;
}
}
</ style >
metadata
45
1 outputs.
Out
[
12
]
24
unexecuted
In
[
None
]
code
metadata
0
Out
[
None
]