Show More
@@ -1,88 +1,95 b'' | |||||
1 | .. _integrating: |
|
1 | .. _integrating: | |
2 |
|
2 | |||
3 | ===================================== |
|
3 | ===================================== | |
4 | Integrating your objects with IPython |
|
4 | Integrating your objects with IPython | |
5 | ===================================== |
|
5 | ===================================== | |
6 |
|
6 | |||
7 | Tab completion |
|
7 | Tab completion | |
8 | ============== |
|
8 | ============== | |
9 |
|
9 | |||
10 | To change the attributes displayed by tab-completing your object, define a |
|
10 | To change the attributes displayed by tab-completing your object, define a | |
11 | ``__dir__(self)`` method for it. For more details, see the documentation of the |
|
11 | ``__dir__(self)`` method for it. For more details, see the documentation of the | |
12 | built-in `dir() function <http://docs.python.org/library/functions.html#dir>`_. |
|
12 | built-in `dir() function <http://docs.python.org/library/functions.html#dir>`_. | |
13 |
|
13 | |||
14 | You can also customise key completions for your objects, e.g. pressing tab after |
|
14 | You can also customise key completions for your objects, e.g. pressing tab after | |
15 | ``obj["a``. To do so, define a method ``_ipython_key_completions_()``, which |
|
15 | ``obj["a``. To do so, define a method ``_ipython_key_completions_()``, which | |
16 | returns a list of objects which are possible keys in a subscript expression |
|
16 | returns a list of objects which are possible keys in a subscript expression | |
17 | ``obj[key]``. |
|
17 | ``obj[key]``. | |
18 |
|
18 | |||
19 | .. versionadded:: 5.0 |
|
19 | .. versionadded:: 5.0 | |
20 | Custom key completions |
|
20 | Custom key completions | |
21 |
|
21 | |||
22 | .. _integrating_rich_display: |
|
22 | .. _integrating_rich_display: | |
23 |
|
23 | |||
24 | Rich display |
|
24 | Rich display | |
25 | ============ |
|
25 | ============ | |
26 |
|
26 | |||
27 | The notebook and the Qt console can display richer representations of objects. |
|
27 | The notebook and the Qt console can display richer representations of objects. | |
28 |
To use this, you can define any |
|
28 | To use this, you can define any number of ``_repr_*_()`` methods. Note that | |
29 | these are surrounded by single, not double underscores. |
|
29 | these are surrounded by single, not double underscores. | |
30 |
|
30 | |||
31 | Both the notebook and the Qt console can display ``svg``, ``png`` and ``jpeg`` |
|
31 | Both the notebook and the Qt console can display ``svg``, ``png`` and ``jpeg`` | |
32 | representations. The notebook can also display ``html``, ``javascript``, |
|
32 | representations. The notebook can also display ``html``, ``javascript``, | |
33 | ``markdown`` and ``latex``. If the methods don't exist, or return ``None``, it |
|
33 | ``markdown`` and ``latex``. If the methods don't exist, or return ``None``, it | |
34 | falls back to a standard ``repr()``. |
|
34 | falls back to a standard ``repr()``. | |
35 |
|
35 | |||
36 | For example:: |
|
36 | For example:: | |
37 |
|
37 | |||
38 | class Shout(object): |
|
38 | class Shout(object): | |
39 | def __init__(self, text): |
|
39 | def __init__(self, text): | |
40 | self.text = text |
|
40 | self.text = text | |
41 |
|
41 | |||
42 | def _repr_html_(self): |
|
42 | def _repr_html_(self): | |
43 | return "<h1>" + self.text + "</h1>" |
|
43 | return "<h1>" + self.text + "</h1>" | |
44 |
|
44 | |||
|
45 | We often want to provide frontends with guidance on how to display the data. To | |||
|
46 | support this, ``_repr_*_()`` methods can also return a data, metadata tuple where | |||
|
47 | metadata is a dictionary containing arbitrary key, value pairs for the frontend | |||
|
48 | to interpret. An example use case is ``_repr_jpeg()``, which can be set to | |||
|
49 | return a jpeg image and a ``{'height': 400, 'width': 600}`` dictionary to inform | |||
|
50 | the frontend how to size the image. | |||
|
51 | ||||
45 | There are also two more powerful display methods: |
|
52 | There are also two more powerful display methods: | |
46 |
|
53 | |||
47 | .. class:: MyObject |
|
54 | .. class:: MyObject | |
48 |
|
55 | |||
49 | .. method:: _repr_mimebundle_(include=None, exclude=None) |
|
56 | .. method:: _repr_mimebundle_(include=None, exclude=None) | |
50 |
|
57 | |||
51 | Should return a dictionary of multiple formats, keyed by mimetype, or a tuple |
|
58 | Should return a dictionary of multiple formats, keyed by mimetype, or a tuple | |
52 | of two dictionaries: *data, metadata*. If this returns something, other |
|
59 | of two dictionaries: *data, metadata*. If this returns something, other | |
53 | ``_repr_*_`` methods are ignored. The method should take keyword arguments |
|
60 | ``_repr_*_`` methods are ignored. The method should take keyword arguments | |
54 | ``include`` and ``exclude``, though it is not required to respect them. |
|
61 | ``include`` and ``exclude``, though it is not required to respect them. | |
55 |
|
62 | |||
56 | .. method:: _ipython_display_() |
|
63 | .. method:: _ipython_display_() | |
57 |
|
64 | |||
58 | Displays the object as a side effect; the return value is ignored. If this |
|
65 | Displays the object as a side effect; the return value is ignored. If this | |
59 | is defined, all other display methods are ignored. |
|
66 | is defined, all other display methods are ignored. | |
60 |
|
67 | |||
61 | Formatters for third-party types |
|
68 | Formatters for third-party types | |
62 | -------------------------------- |
|
69 | -------------------------------- | |
63 |
|
70 | |||
64 | The user can also register formatters for types without modifying the class:: |
|
71 | The user can also register formatters for types without modifying the class:: | |
65 |
|
72 | |||
66 | from bar import Foo |
|
73 | from bar import Foo | |
67 |
|
74 | |||
68 | def foo_html(obj): |
|
75 | def foo_html(obj): | |
69 | return '<marquee>Foo object %s</marquee>' % obj.name |
|
76 | return '<marquee>Foo object %s</marquee>' % obj.name | |
70 |
|
77 | |||
71 | html_formatter = get_ipython().display_formatter.formatters['text/html'] |
|
78 | html_formatter = get_ipython().display_formatter.formatters['text/html'] | |
72 | html_formatter.for_type(Foo, foo_html) |
|
79 | html_formatter.for_type(Foo, foo_html) | |
73 |
|
80 | |||
74 | # Or register a type without importing it - this does the same as above: |
|
81 | # Or register a type without importing it - this does the same as above: | |
75 | html_formatter.for_type_by_name('bar.Foo', foo_html) |
|
82 | html_formatter.for_type_by_name('bar.Foo', foo_html) | |
76 |
|
83 | |||
77 | Custom exception tracebacks |
|
84 | Custom exception tracebacks | |
78 | =========================== |
|
85 | =========================== | |
79 |
|
86 | |||
80 | Rarely, you might want to display a custom traceback when reporting an |
|
87 | Rarely, you might want to display a custom traceback when reporting an | |
81 | exception. To do this, define the custom traceback using |
|
88 | exception. To do this, define the custom traceback using | |
82 | `_render_traceback_(self)` method which returns a list of strings, one string |
|
89 | `_render_traceback_(self)` method which returns a list of strings, one string | |
83 | for each line of the traceback. For example, the `ipyparallel |
|
90 | for each line of the traceback. For example, the `ipyparallel | |
84 | <http://ipyparallel.readthedocs.io/>`__ a parallel computing framework for |
|
91 | <http://ipyparallel.readthedocs.io/>`__ a parallel computing framework for | |
85 | IPython, does this to display errors from multiple engines. |
|
92 | IPython, does this to display errors from multiple engines. | |
86 |
|
93 | |||
87 | Please be conservative in using this feature; by replacing the default traceback |
|
94 | Please be conservative in using this feature; by replacing the default traceback | |
88 | you may hide important information from the user. |
|
95 | you may hide important information from the user. |
General Comments 0
You need to be logged in to leave comments.
Login now