##// 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 display(*objs, include=['text/plain','application/json'])
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 SVGFormatter,
52 SVGFormatter,
53 PNGFormatter,
53 PNGFormatter,
54 LatexFormatter,
54 LatexFormatter,
55 JSONFormatter
55 JSONFormatter,
56 JavascriptFormatter
56 ]
57 ]
57 d = {}
58 d = {}
58 for cls in formatter_classes:
59 for cls in formatter_classes:
@@ -220,15 +221,14 b' class BaseFormatter(Configurable):'
220 obj_id = id(obj)
221 obj_id = id(obj)
221 try:
222 try:
222 obj_class = getattr(obj, '__class__', None) or type(obj)
223 obj_class = getattr(obj, '__class__', None) or type(obj)
223 if hasattr(obj_class, self.print_method):
224 # First try to find registered singleton printers for the type.
224 printer = getattr(obj_class, self.print_method)
225 return printer(obj)
226 try:
225 try:
227 printer = self.singleton_printers[obj_id]
226 printer = self.singleton_printers[obj_id]
228 except (TypeError, KeyError):
227 except (TypeError, KeyError):
229 pass
228 pass
230 else:
229 else:
231 return printer(obj)
230 return printer(obj)
231 # Next look for type_printers.
232 for cls in pretty._get_mro(obj_class):
232 for cls in pretty._get_mro(obj_class):
233 if cls in self.type_printers:
233 if cls in self.type_printers:
234 return self.type_printers[cls](obj)
234 return self.type_printers[cls](obj)
@@ -236,6 +236,10 b' class BaseFormatter(Configurable):'
236 printer = self._in_deferred_types(cls)
236 printer = self._in_deferred_types(cls)
237 if printer is not None:
237 if printer is not None:
238 return printer(obj)
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 return None
243 return None
240 except Exception:
244 except Exception:
241 pass
245 pass
@@ -442,66 +446,80 b' class HTMLFormatter(BaseFormatter):'
442 """An HTML formatter.
446 """An HTML formatter.
443
447
444 To define the callables that compute the HTML representation of your
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 or :meth:`for_type_by_name` methods to register functions that handle
450 or :meth:`for_type_by_name` methods to register functions that handle
447 this.
451 this.
448 """
452 """
449 format_type = Str('text/html')
453 format_type = Str('text/html')
450
454
451 print_method = Str('__html__')
455 print_method = Str('_repr_html_')
452
456
453
457
454 class SVGFormatter(BaseFormatter):
458 class SVGFormatter(BaseFormatter):
455 """An SVG formatter.
459 """An SVG formatter.
456
460
457 To define the callables that compute the SVG representation of your
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 or :meth:`for_type_by_name` methods to register functions that handle
463 or :meth:`for_type_by_name` methods to register functions that handle
460 this.
464 this.
461 """
465 """
462 format_type = Str('image/svg+xml')
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 class PNGFormatter(BaseFormatter):
471 class PNGFormatter(BaseFormatter):
468 """A PNG formatter.
472 """A PNG formatter.
469
473
470 To define the callables that compute the PNG representation of your
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 or :meth:`for_type_by_name` methods to register functions that handle
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 format_type = Str('image/png')
481 format_type = Str('image/png')
476
482
477 print_method = Str('__png__')
483 print_method = Str('_repr_png_')
478
484
479
485
480 class LatexFormatter(BaseFormatter):
486 class LatexFormatter(BaseFormatter):
481 """A LaTeX formatter.
487 """A LaTeX formatter.
482
488
483 To define the callables that compute the LaTeX representation of your
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 or :meth:`for_type_by_name` methods to register functions that handle
491 or :meth:`for_type_by_name` methods to register functions that handle
486 this.
492 this.
487 """
493 """
488 format_type = Str('text/latex')
494 format_type = Str('text/latex')
489
495
490 print_method = Str('__latex__')
496 print_method = Str('_repr_latex_')
491
497
492
498
493 class JSONFormatter(BaseFormatter):
499 class JSONFormatter(BaseFormatter):
494 """A JSON string formatter.
500 """A JSON string formatter.
495
501
496 To define the callables that compute the JSON string representation of
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 or :meth:`for_type_by_name` methods to register functions that handle
504 or :meth:`for_type_by_name` methods to register functions that handle
499 this.
505 this.
500 """
506 """
501 format_type = Str('application/json')
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 FormatterABC.register(BaseFormatter)
524 FormatterABC.register(BaseFormatter)
507 FormatterABC.register(PlainTextFormatter)
525 FormatterABC.register(PlainTextFormatter)
@@ -510,6 +528,7 b' FormatterABC.register(SVGFormatter)'
510 FormatterABC.register(PNGFormatter)
528 FormatterABC.register(PNGFormatter)
511 FormatterABC.register(LatexFormatter)
529 FormatterABC.register(LatexFormatter)
512 FormatterABC.register(JSONFormatter)
530 FormatterABC.register(JSONFormatter)
531 FormatterABC.register(JavascriptFormatter)
513
532
514
533
515 def format_display_data(obj, include=None, exclude=None):
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 representations will be displayed in the console as long as the objects know
458 representations will be displayed in the console as long as the objects know
459 how to compute those representations. The easiest way of teaching objects how
459 how to compute those representations. The easiest way of teaching objects how
460 to format themselves in various representations is to define special methods
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 can also be given custom formatter functions for various types::
462 can also be given custom formatter functions for various types::
463
463
464 In [6]: ip = get_ipython()
464 In [6]: ip = get_ipython()
@@ -39,7 +39,7 b' def print_basic_unicode(o, p, cycle):'
39
39
40
40
41 def print_png(o):
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 s = latex(o, mode='inline')
43 s = latex(o, mode='inline')
44 # mathtext does not understand certain latex flags, so we try to replace
44 # mathtext does not understand certain latex flags, so we try to replace
45 # them with suitable subs.
45 # them with suitable subs.
@@ -60,7 +60,8 b' def getfigs(*fig_nums):'
60 f = Gcf.figs.get(num)
60 f = Gcf.figs.get(num)
61 if f is None:
61 if f is None:
62 print('Warning: figure %s not available.' % num)
62 print('Warning: figure %s not available.' % num)
63 figs.append(f.canvas.figure)
63 else:
64 figs.append(f.canvas.figure)
64 return figs
65 return figs
65
66
66
67
General Comments 0
You need to be logged in to leave comments. Login now