-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathpretty_annotate.py
283 lines (243 loc) · 9.32 KB
/
pretty_annotate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
"""
This module implements code highlighting of numba function annotations.
"""
from warnings import warn
warn("The pretty_annotate functionality is experimental and might change API",
FutureWarning)
def hllines(code, style):
try:
from pygments import highlight
from pygments.lexers import PythonLexer
from pygments.formatters import HtmlFormatter
except ImportError:
raise ImportError("please install the 'pygments' package")
pylex = PythonLexer()
"Given a code string, return a list of html-highlighted lines"
hf = HtmlFormatter(noclasses=True, style=style, nowrap=True)
res = highlight(code, pylex, hf)
return res.splitlines()
def htlines(code, style):
try:
from pygments import highlight
from pygments.lexers import PythonLexer
# TerminalFormatter does not support themes, Terminal256 should,
# but seem to not work.
from pygments.formatters import TerminalFormatter
except ImportError:
raise ImportError("please install the 'pygments' package")
pylex = PythonLexer()
"Given a code string, return a list of ANSI-highlighted lines"
hf = TerminalFormatter(style=style)
res = highlight(code, pylex, hf)
return res.splitlines()
def get_ansi_template():
try:
from jinja2 import Template
except ImportError:
raise ImportError("please install the 'jinja2' package")
return Template("""
{%- for func_key in func_data.keys() -%}
Function name: \x1b[34m{{func_data[func_key]['funcname']}}\x1b[39;49;00m
{%- if func_data[func_key]['filename'] -%}
{{'\n'}}In file: \x1b[34m{{func_data[func_key]['filename'] -}}\x1b[39;49;00m
{%- endif -%}
{{'\n'}}With signature: \x1b[34m{{func_key[1]}}\x1b[39;49;00m
{{- "\n" -}}
{%- for num, line, hl, hc in func_data[func_key]['pygments_lines'] -%}
{{-'\n'}}{{ num}}: {{hc-}}
{%- if func_data[func_key]['ir_lines'][num] -%}
{%- for ir_line, ir_line_type in func_data[func_key]['ir_lines'][num] %}
{{-'\n'}}--{{- ' '*func_data[func_key]['python_indent'][num]}}
{{- ' '*(func_data[func_key]['ir_indent'][num][loop.index0]+4)
}}{{ir_line }}\x1b[41m{{ir_line_type-}}\x1b[39;49;00m
{%- endfor -%}
{%- endif -%}
{%- endfor -%}
{%- endfor -%}
""")
return ansi_template
def get_html_template():
try:
from jinja2 import Template
except ImportError:
raise ImportError("please install the 'jinja2' package")
return Template("""
<html>
<head>
<style>
.annotation_table {
color: #000000;
font-family: monospace;
margin: 5px;
width: 100%;
}
/* override JupyterLab style */
.annotation_table td {
text-align: left;
background-color: transparent;
padding: 1px;
}
.annotation_table tbody tr:nth-child(even) {
background: white;
}
.annotation_table code
{
background-color: transparent;
white-space: normal;
}
/* End override JupyterLab style */
tr:hover {
background-color: rgba(92, 200, 249, 0.25);
}
td.object_tag summary ,
td.lifted_tag summary{
font-weight: bold;
display: list-item;
}
span.lifted_tag {
color: #00cc33;
}
span.object_tag {
color: #cc3300;
}
td.lifted_tag {
background-color: #cdf7d8;
}
td.object_tag {
background-color: #fef5c8;
}
code.ir_code {
color: grey;
font-style: italic;
}
.metadata {
border-bottom: medium solid black;
display: inline-block;
padding: 5px;
width: 100%;
}
.annotations {
padding: 5px;
}
.hidden {
display: none;
}
.buttons {
padding: 10px;
cursor: pointer;
}
</style>
</head>
<body>
{% for func_key in func_data.keys() %}
<div class="metadata">
Function name: {{func_data[func_key]['funcname']}}<br />
{% if func_data[func_key]['filename'] %}
in file: {{func_data[func_key]['filename']|escape}}<br />
{% endif %}
with signature: {{func_key[1]|e}}
</div>
<div class="annotations">
<table class="annotation_table tex2jax_ignore">
{%- for num, line, hl, hc in func_data[func_key]['pygments_lines'] -%}
{%- if func_data[func_key]['ir_lines'][num] %}
<tr><td style="text-align:left;" class="{{func_data[func_key]['python_tags'][num]}}">
<details>
<summary>
<code>
{{num}}:
{{' '*func_data[func_key]['python_indent'][num]}}{{hl}}
</code>
</summary>
<table class="annotation_table">
<tbody>
{%- for ir_line, ir_line_type in func_data[func_key]['ir_lines'][num] %}
<tr class="ir_code">
<td style="text-align: left;"><code>
{{- ' '*func_data[func_key]['python_indent'][num]}}
{{ ' '*func_data[func_key]['ir_indent'][num][loop.index0]}}{{ir_line|e -}}
<span class="object_tag">{{ir_line_type}}</span>
</code>
</td>
</tr>
{%- endfor -%}
</tbody>
</table>
</details>
</td></tr>
{% else -%}
<tr><td style="text-align:left; padding-left: 22px;" class="{{func_data[func_key]['python_tags'][num]}}">
<code>
{{num}}:
{{' '*func_data[func_key]['python_indent'][num]}}{{hl}}
</code>
</td></tr>
{%- endif -%}
{%- endfor -%}
</table>
</div>
{% endfor %}
</body>
</html>
""")
def reform_code(annotation):
"""
Extract the code from the Numba annotation datastructure.
Pygments can only highlight full multi-line strings, the Numba
annotation is list of single lines, with indentation removed.
"""
ident_dict = annotation['python_indent']
s= ''
for n,l in annotation['python_lines']:
s = s+' '*ident_dict[n]+l+'\n'
return s
class Annotate:
"""
Construct syntax highlighted annotation for a given jitted function:
Example:
>>> import numba
>>> from numba.pretty_annotate import Annotate
>>> @numba.jit
... def test(q):
... res = 0
... for i in range(q):
... res += i
... return res
...
>>> test(10)
45
>>> Annotate(test)
The last line will return an HTML and/or ANSI representation that will be
displayed accordingly in Jupyter/IPython.
Function annotations persist across compilation for newly encountered
type signatures and as a result annotations are shown for all signatures
by default.
Annotations for a specific signature can be shown by using the
``signature`` parameter.
>>> @numba.jit
... def add(x, y):
... return x + y
...
>>> add(1, 2)
3
>>> add(1.3, 5.7)
7.0
>>> add.signatures
[(int64, int64), (float64, float64)]
>>> Annotate(add, signature=add.signatures[1]) # annotation for (float64, float64)
"""
def __init__(self, function, signature=None, **kwargs):
style = kwargs.get('style', 'default')
if not function.signatures:
raise ValueError('function need to be jitted for at least one signature')
ann = function.get_annotation_info(signature=signature)
self.ann = ann
for k,v in ann.items():
res = hllines(reform_code(v), style)
rest = htlines(reform_code(v), style)
v['pygments_lines'] = [(a,b,c, d) for (a,b),c, d in zip(v['python_lines'], res, rest)]
def _repr_html_(self):
return get_html_template().render(func_data=self.ann)
def __repr__(self):
return get_ansi_template().render(func_data=self.ann)