##// END OF EJS Templates
Renaming the special methods of the formatters....
Brian Granger -
Show More
@@ -0,0 +1,26 b''
1 """Code that shows off the IPython display logic.
2 """
3
4 from IPython.core.display import (
5 display, display_pretty, display_html,
6 display_svg, display_json
7 )
8
9 class Circle(object):
10
11 def __init__(self, radius):
12 self.radius = radius
13
14 def _repr_pretty_(self, p, cycle):
15 p.text(u"\u25CB")
16
17 __pretty__ = _repr_pretty_
18
19 def _repr_html_(self):
20 return "<h1>Cirle: radius=%s</h1>" % self.radius
21
22 def _repr_svg_(self):
23 return """<svg>
24 <circle cx="100" cy="50" r="40" stroke="black" stroke-width="2" fill="red"/>
25 </svg>"""
26
@@ -119,4 +119,12 b' def display_json(*objs):'
119 119 display(*objs, include=['text/plain','application/json'])
120 120
121 121
122 def display_javascript(*objs):
123 """Display the Javascript representation of an object.
122 124
125 Parameters
126 ----------
127 objs : tuple of objects
128 The Python objects to display.
129 """
130 display(*objs, include=['text/plain','application/javascript'])
@@ -52,7 +52,8 b' class DisplayFormatter(Configurable):'
52 52 SVGFormatter,
53 53 PNGFormatter,
54 54 LatexFormatter,
55 JSONFormatter
55 JSONFormatter,
56 JavascriptFormatter
56 57 ]
57 58 d = {}
58 59 for cls in formatter_classes:
@@ -220,15 +221,14 b' class BaseFormatter(Configurable):'
220 221 obj_id = id(obj)
221 222 try:
222 223 obj_class = getattr(obj, '__class__', None) or type(obj)
223 if hasattr(obj_class, self.print_method):
224 printer = getattr(obj_class, self.print_method)
225 return printer(obj)
224 # First try to find registered singleton printers for the type.
226 225 try:
227 226 printer = self.singleton_printers[obj_id]
228 227 except (TypeError, KeyError):
229 228 pass
230 229 else:
231 230 return printer(obj)
231 # Next look for type_printers.
232 232 for cls in pretty._get_mro(obj_class):
233 233 if cls in self.type_printers:
234 234 return self.type_printers[cls](obj)
@@ -236,6 +236,10 b' class BaseFormatter(Configurable):'
236 236 printer = self._in_deferred_types(cls)
237 237 if printer is not None:
238 238 return printer(obj)
239 # Finally look for special method names.
240 if hasattr(obj_class, self.print_method):
241 printer = getattr(obj_class, self.print_method)
242 return printer(obj)
239 243 return None
240 244 except Exception:
241 245 pass
@@ -442,66 +446,80 b' class HTMLFormatter(BaseFormatter):'
442 446 """An HTML formatter.
443 447
444 448 To define the callables that compute the HTML representation of your
445 objects, define a :meth:`__html__` method or use the :meth:`for_type`
449 objects, define a :meth:`_repr_html_` method or use the :meth:`for_type`
446 450 or :meth:`for_type_by_name` methods to register functions that handle
447 451 this.
448 452 """
449 453 format_type = Str('text/html')
450 454
451 print_method = Str('__html__')
455 print_method = Str('_repr_html_')
452 456
453 457
454 458 class SVGFormatter(BaseFormatter):
455 459 """An SVG formatter.
456 460
457 461 To define the callables that compute the SVG representation of your
458 objects, define a :meth:`__svg__` method or use the :meth:`for_type`
462 objects, define a :meth:`_repr_svg_` method or use the :meth:`for_type`
459 463 or :meth:`for_type_by_name` methods to register functions that handle
460 464 this.
461 465 """
462 466 format_type = Str('image/svg+xml')
463 467
464 print_method = Str('__svg__')
468 print_method = Str('_repr_svg_')
465 469
466 470
467 471 class PNGFormatter(BaseFormatter):
468 472 """A PNG formatter.
469 473
470 474 To define the callables that compute the PNG representation of your
471 objects, define a :meth:`__png__` method or use the :meth:`for_type`
475 objects, define a :meth:`_repr_png_` method or use the :meth:`for_type`
472 476 or :meth:`for_type_by_name` methods to register functions that handle
473 this. The raw data should be the base64 encoded raw png data.
477 this.
478
479 The raw data should be the base64 encoded raw png data.
474 480 """
475 481 format_type = Str('image/png')
476 482
477 print_method = Str('__png__')
483 print_method = Str('_repr_png_')
478 484
479 485
480 486 class LatexFormatter(BaseFormatter):
481 487 """A LaTeX formatter.
482 488
483 489 To define the callables that compute the LaTeX representation of your
484 objects, define a :meth:`__latex__` method or use the :meth:`for_type`
490 objects, define a :meth:`_repr_latex_` method or use the :meth:`for_type`
485 491 or :meth:`for_type_by_name` methods to register functions that handle
486 492 this.
487 493 """
488 494 format_type = Str('text/latex')
489 495
490 print_method = Str('__latex__')
496 print_method = Str('_repr_latex_')
491 497
492 498
493 499 class JSONFormatter(BaseFormatter):
494 500 """A JSON string formatter.
495 501
496 502 To define the callables that compute the JSON string representation of
497 your objects, define a :meth:`__json__` method or use the :meth:`for_type`
503 your objects, define a :meth:`_repr_json_` method or use the :meth:`for_type`
498 504 or :meth:`for_type_by_name` methods to register functions that handle
499 505 this.
500 506 """
501 507 format_type = Str('application/json')
502 508
503 print_method = Str('__json__')
509 print_method = Str('_repr_json_')
510
511
512 class JavascriptFormatter(BaseFormatter):
513 """A Javascript formatter.
514
515 To define the callables that compute the Javascript representation of
516 your objects, define a :meth:`_repr_javascript_` method or use the
517 :meth:`for_type` or :meth:`for_type_by_name` methods to register functions
518 that handle this.
519 """
520 format_type = Str('application/javascript')
504 521
522 print_method = Str('_repr_javascript_')
505 523
506 524 FormatterABC.register(BaseFormatter)
507 525 FormatterABC.register(PlainTextFormatter)
@@ -510,6 +528,7 b' FormatterABC.register(SVGFormatter)'
510 528 FormatterABC.register(PNGFormatter)
511 529 FormatterABC.register(LatexFormatter)
512 530 FormatterABC.register(JSONFormatter)
531 FormatterABC.register(JavascriptFormatter)
513 532
514 533
515 534 def format_display_data(obj, include=None, exclude=None):
@@ -458,7 +458,7 b' Python objects can simply be passed to these functions and the appropriate'
458 458 representations will be displayed in the console as long as the objects know
459 459 how to compute those representations. The easiest way of teaching objects how
460 460 to format themselves in various representations is to define special methods
461 such as: ``__html``, ``__svg__`` and ``__png__``. IPython's display formatters
461 such as: ``_repr_html_``, ``_repr_svg_`` and ``_repr_png_``. IPython's display formatters
462 462 can also be given custom formatter functions for various types::
463 463
464 464 In [6]: ip = get_ipython()
@@ -39,7 +39,7 b' def print_basic_unicode(o, p, cycle):'
39 39
40 40
41 41 def print_png(o):
42 """A funciton to display sympy expression using LaTex -> PNG."""
42 """A function to display sympy expression using LaTex -> PNG."""
43 43 s = latex(o, mode='inline')
44 44 # mathtext does not understand certain latex flags, so we try to replace
45 45 # them with suitable subs.
@@ -60,7 +60,8 b' def getfigs(*fig_nums):'
60 60 f = Gcf.figs.get(num)
61 61 if f is None:
62 62 print('Warning: figure %s not available.' % num)
63 figs.append(f.canvas.figure)
63 else:
64 figs.append(f.canvas.figure)
64 65 return figs
65 66
66 67
General Comments 0
You need to be logged in to leave comments. Login now