/usr/lib/python3/dist-packages/rpy2/ipython/html.py is in python3-rpy2 2.8.5-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 | import jinja2
from IPython.display import HTML
css = """
<style>
.rpy2_table {
border: none;
border-right: solid 1px rgb(180, 180, 180);
border-bottom: solid 1px rgb(180, 180, 180);
border-radius: 4px;
-moz-border-radius: 4px;
}
.rpy2_names {
background-color: rgb(215, 215, 215);
font-weight: bold;
}
.rpy2_table th:first-child {
background-color: rgb(215, 215, 215);
border-top: none;
}
.rpy2_table tbody tr td {
text-align: right;
font-family: monospace;
}
.rpy_table tbody tr td:first_child {
border-left: solid 1px rgb(180, 180, 180);
}
.rpy2_table tbody tr td.rpy2_rowname {
border-left: none;
color: rgb(180, 180, 180);
}
</style>
"""
template_list = jinja2.Template("""
<p><emph>{{ clsname }}</emph> with {{ rlist | length }} elements:</p>
<dl class="rpy2">
{%- for elt_i in range(display_neltmax) %}
<dt>{{ rlist.names[elt_i] }}</dt>
<dd>{{ rlist[elt_i] }}</dd>
{%- endfor %}
{%- if display_neltmax < (rlist | length) %}
<dt>...</dt>
<dd></dd>
{%- endif %}
</dl>
""")
template_vector_horizontal = jinja2.Template("""
<emph>{{ clsname }}</emph> with {{ vector | length }} elements:
<table class="{{ table_class }}">
<thead>
</thead>
<tbody>
<tr>
{%- for elt_i in range(display_ncolmax - size_tail) %}
<td>{{ vector[elt_i] }}</td>
{%- endfor %}
{%- if display_ncolmax < (vector | length) %}
<td>...</td>
{%- endif %}
{%- for elt_i in elt_i_tail %}
<td>{{ vector[elt_i] }}</td>
{%- endfor %}
</tr>
</tbody>
</table>
""")
template_vector_vertical = jinja2.Template("""
<emph>{{ clsname }}</emph> with {{ vector | length }} elements:
<table class="{{ table_class }}">
<thead>
</thead>
<tbody>
{%- for elt_i in range(display_nrowmax - size_tail) %}
<tr>
<td class="rpy2_rowname">{{ elt_i }}</td>
{%- if has_vector_names %}
<td class="rpy2_names">{{ vector.names[elt_i] }}</td>
{%- endif %}
<td>{{ vector[elt_i]}}</td>
</tr>
{%- endfor %}
{%- if display_nrowmax < (vector | length) %}
<tr>
<td class="rpy2_rowname">...</td>
{%- if has_vector_names %}
<td class="rpy2_names">...</td>
{%- endif %}
<td>...</td>
</tr>
{%- endif %}
{%- for elt_i in elt_i_tail %}
<tr>
<td class="rpy2_rowname">{{ elt_i }}</td>
{%- if has_vector_names %}
<td class="rpy2_names">{{ vector.names[elt_i] }}</td>
{%- endif %}
<td>{{ vector[elt_i] }}</td>
</tr>
{%- endfor %}
</tr>
</tbody>
</table>
""")
template_dataframe = jinja2.Template("""
<emph>{{ clsname }}</emph> with {{ dataf.nrow }} rows and {{ dataf | length }} columns:
<table class="{{ table_class }}">
<thead>
<tr class="rpy2_names">
<th></th>
{%- if has_rownames %}
<th></th>
{%- endif %}
{%- for col_i in range(display_ncolmax - size_coltail) %}
<th>{{ dataf.names[col_i] }}</th>
{%- endfor %}
{%- if display_ncolmax < dataf.ncol %}
<th>...</th>
{%- endif %}
{%- for col_i in col_i_tail %}
<th>{{ dataf.names[col_i] }}</th>
{%- endfor %}
</tr>
</thead>
<tbody>
{%- for row_i in range(display_nrowmax - size_rowtail) %}
<tr>
<td class="rpy2_rowname">{{ row_i }}</td>
{%- if has_rownames %}
<td class="rpy2_names">{{ dataf.rownames[row_i] }}</td>
{%- endif %}
{%- for col_i in range(display_ncolmax - size_coltail) %}
<td>{{ dataf[col_i][row_i] }}</td>
{%- endfor %}
{%- if display_ncolmax < dataf.ncol %}
<td>...</td>
{%- endif %}
{%- for col_i in col_i_tail %}
<td>{{ dataf[col_i][row_i] }}</td>
{%- endfor %}
</tr>
{%- endfor %}
{%- if dataf.nrow > display_nrowmax %}
<tr>
<td class="rpy2_rowname">...</td>
{%- if has_rownames %}
<td class="rpy2_names">...</td>
{%- endif %}
{%- for col_i in range(display_ncolmax - size_coltail) %}
<td>...</td>
{%- endfor %}
{%- if display_ncolmax < dataf.ncol %}
<td>...</td>
{%- endif %}
{%- for col_i in range(2) %}
<td>...</td>
{%- endfor %}
</tr>
{%- endif %}
{%- for row_i in row_i_tail %}
<tr>
<td class="rpy2_rowname">{{ row_i }}</td>
{%- if has_rownames %}
<td class="rpy2_names">{{ dataf.rownames[row_i] }}</td>
{%- endif %}
{%- for col_i in range(display_ncolmax - size_coltail) %}
<td>{{ dataf[col_i][row_i] }}</td>
{%- endfor %}
{%- if display_ncolmax < dataf.ncol %}
<td>...</td>
{%- endif %}
{%- for col_i in col_i_tail %}
<td>{{ dataf[col_i][row_i] }}</td>
{%- endfor %}
</tr>
{%- endfor %}
</tbody>
</table>
""")
template_ridentifiedobject = jinja2.Template("""
<ul style="list-style-type: none;">
<li>{{ clsname }} object</li>
<li>Origin in R: {{ origin }}</li>
<li>Class(es) in R:
<ul>
{%- for rclsname in obj.rclass %}
<li>{{ rclsname }}</li>
{%- endfor %}
</ul>
</li>
</ul>
""")
template_rs4 = jinja2.Template("""
<table class="{{ table_class }}">
<tr>
<th colspan="2">{{ clsname }} object</th>
</tr>
<tr>
<th>Origin in R:</th>
<td> {{ origin }}</td>
</tr>
<tr>
<th>Class(es) in R:</th>
<td>
<ul>
{%- for rclsname in obj.rclass %}
<li>{{ rclsname }}</li>
{%- endfor %}
</ul>
</td>
</tr>
<tr>
<th>Attributes:</th>
<td>
<ul>
{%- for sln in obj.slotnames() %}
<li>{{ sln }}</li>
{%- endfor %}
</ul>
</td>
</tr>
</table>
""")
template_sourcecode = jinja2.Template("""
<p>
R source code:
</p>
<style>
{{ syntax_highlighting }}
</style>
{{ sourcecode }}
""")
from rpy2.robjects import (vectors,
RObject,
SignatureTranslatedFunction,
RS4,
SourceCode)
class StrFactorVector(vectors.FactorVector):
def __getitem__(self, item):
integer = super(StrFactorVector, self).__getitem__(item)
# R is one-offset, Python is zero-offset
return self.levels[integer-1]
class StrDataFrame(vectors.DataFrame):
def __getitem__(self, item):
obj = super(StrDataFrame, self).__getitem__(item)
if isinstance(obj, vectors.FactorVector):
obj = StrFactorVector(obj)
return obj
def html_vector_horizontal(vector,
display_ncolmax=10,
size_tail=2,
table_class='rpy2_table'):
if isinstance(vector, vectors.FactorVector):
vector = StrFactorVector(vector)
html = template_vector_horizontal.render({
'table_class': table_class,
'clsname': type(vector).__name__,
'vector': vector,
'display_ncolmax': min(display_ncolmax, len(vector)),
'size_tail': size_tail,
'elt_i_tail': range(max(0, len(vector)-size_tail), len(vector))})
return html
def html_rlist(vector,
display_nrowmax=10,
size_tail=2,
table_class='rpy2_table'):
html = template_vector_vertical.render({
'table_class': table_class,
'clsname': type(vector).__name__,
'vector': vector,
'has_vector_names': vector.names is not rinterface.NULL,
'display_nrowmax': min(display_nrowmax, len(vector)),
'size_tail': size_tail,
'elt_i_tail': range(max(0, len(vector)-size_tail), len(vector))})
return html
def html_rdataframe(dataf,
display_nrowmax=10,
display_ncolmax=6,
size_coltail=2, size_rowtail=2,
table_class='rpy2_table'):
html = template_dataframe.render(
{'dataf': StrDataFrame(dataf),
'table_class': table_class,
'has_rownames': dataf.rownames is not None,
'clsname': type(dataf).__name__,
'display_nrowmax': min(display_nrowmax, dataf.nrow),
'display_ncolmax': min(display_ncolmax, dataf.ncol),
'col_i_tail': range(max(0, dataf.ncol-size_coltail), dataf.ncol),
'row_i_tail': range(max(0, dataf.nrow-size_rowtail), dataf.nrow),
'size_coltail': size_coltail,
'size_rowtail': size_rowtail
})
return html
def html_sourcecode(sourcecode):
from pygments import highlight
from pygments.lexers import SLexer
from pygments.formatters import HtmlFormatter
formatter = HtmlFormatter()
htmlcode = highlight(sourcecode, SLexer(), formatter)
d = {'sourcecode': htmlcode,
'syntax_highlighting': formatter.get_style_defs()}
html = template_sourcecode.render(d)
return html
# FIXME: wherefrom() is taken from the rpy2 documentation
# May be it should become part of the rpy2 API
from rpy2 import rinterface
def wherefrom(name, startenv=rinterface.globalenv):
""" when calling 'get', where the R object is coming from. """
env = startenv
obj = None
retry = True
while retry:
try:
obj = env[name]
retry = False
except LookupError:
env = env.enclos()
if env.rsame(rinterface.emptyenv):
retry = False
else:
retry = True
return env
def _dict_ridentifiedobject(obj):
if hasattr(obj, '__rname__') and obj.__rname__ is not None:
env = wherefrom(obj.__rname__)
try:
origin = env.do_slot('name')[0]
except LookupError:
origin = 'package:base ?'
else:
origin = '???'
d = {'clsname': type(obj).__name__,
'origin': origin,
'obj': obj}
return d
def html_ridentifiedobject(obj):
d = _dict_ridentifiedobject(obj)
html = template_ridentifiedobject.render(d)
return html
def html_rs4(obj, table_class='rpy2_table'):
d = _dict_ridentifiedobject(obj)
d['table_class']=table_class
html = template_rs4.render(d)
return html
def init_printing():
ip = get_ipython()
html_f = ip.display_formatter.formatters['text/html']
html_f.for_type(vectors.Vector, html_vector_horizontal)
html_f.for_type(vectors.ListVector, html_rlist)
html_f.for_type(vectors.DataFrame, html_rdataframe)
html_f.for_type(RObject, html_ridentifiedobject)
html_f.for_type(RS4, html_rs4)
html_f.for_type(SignatureTranslatedFunction, html_ridentifiedobject)
html_f.for_type(SourceCode, html_sourcecode)
from IPython.display import HTML
HTML(css)
|