Show More
@@ -145,6 +145,9 def display(*objs, include=None, exclude=None, metadata=None, transient=None, di | |||||
145 | By default all representations will be computed and sent to the frontends. |
|
145 | By default all representations will be computed and sent to the frontends. | |
146 | Frontends can decide which representation is used and how. |
|
146 | Frontends can decide which representation is used and how. | |
147 |
|
147 | |||
|
148 | In terminal IPython this will be similar to using `print`, for use in richer | |||
|
149 | frontends see Jupyter notebook examples with rich display logic. | |||
|
150 | ||||
148 | Parameters |
|
151 | Parameters | |
149 | ---------- |
|
152 | ---------- | |
150 | objs : tuple of objects |
|
153 | objs : tuple of objects | |
@@ -173,19 +176,109 def display(*objs, include=None, exclude=None, metadata=None, transient=None, di | |||||
173 | If given as True, generate a new display_id |
|
176 | If given as True, generate a new display_id | |
174 | kwargs: additional keyword-args, optional |
|
177 | kwargs: additional keyword-args, optional | |
175 | Additional keyword-arguments are passed through to the display publisher. |
|
178 | Additional keyword-arguments are passed through to the display publisher. | |
176 |
|
179 | |||
177 | Returns |
|
180 | Returns | |
178 | ------- |
|
181 | ------- | |
179 |
|
182 | |||
180 | handle: DisplayHandle |
|
183 | handle: DisplayHandle | |
181 | Returns a handle on updatable displays, if display_id is given. |
|
184 | Returns a handle on updatable displays, if display_id is given. | |
182 | Returns None if no display_id is given (default). |
|
185 | Returns None if no display_id is given (default). | |
|
186 | ||||
|
187 | Examples | |||
|
188 | -------- | |||
|
189 | ||||
|
190 | >>> class Json(object): | |||
|
191 | ... def __init__(self, json): | |||
|
192 | ... self.json = json | |||
|
193 | ... def _repr_pretty_(self, pp, cycle): | |||
|
194 | ... import json | |||
|
195 | ... pp.text(json.dumps(self.json, indent=2)) | |||
|
196 | ... def __repr__(self): | |||
|
197 | ... return str(self.json) | |||
|
198 | ... | |||
|
199 | ||||
|
200 | >>> d = Json({1:2, 3: {4:5}}) | |||
|
201 | ||||
|
202 | >>> print(d) | |||
|
203 | {1: 2, 3: {4: 5}} | |||
|
204 | ||||
|
205 | >>> display(d) | |||
|
206 | { | |||
|
207 | "1": 2, | |||
|
208 | "3": { | |||
|
209 | "4": 5 | |||
|
210 | } | |||
|
211 | } | |||
|
212 | ||||
|
213 | >>> def int_formatter(integer, pp, cycle): | |||
|
214 | ... pp.text('I'*integer) | |||
|
215 | ||||
|
216 | >>> plain = get_ipython().display_formatter.formatters['text/plain'] | |||
|
217 | >>> plain.for_type(int, int_formatter) | |||
|
218 | <function _repr_pprint at 0x...> | |||
|
219 | >>> display(7-5) | |||
|
220 | II | |||
|
221 | ||||
|
222 | >>> del plain.type_printers[int] | |||
|
223 | >>> display(7-5) | |||
|
224 | 2 | |||
|
225 | ||||
|
226 | See Also | |||
|
227 | -------- | |||
|
228 | ||||
|
229 | `update_display` | |||
|
230 | ||||
|
231 | Notes | |||
|
232 | ----- | |||
|
233 | ||||
|
234 | In Python, objects can declare their textual representation using the | |||
|
235 | `__repr__` method. IPython expands on this idea and allows objects to declare | |||
|
236 | other, rich representations including: | |||
|
237 | ||||
|
238 | - HTML | |||
|
239 | - JSON | |||
|
240 | - PNG | |||
|
241 | - JPEG | |||
|
242 | - SVG | |||
|
243 | - LaTeX | |||
|
244 | ||||
|
245 | A single object can declare some or all of these representations; all are | |||
|
246 | handled by IPython's display system. | |||
|
247 | ||||
|
248 | The main idea of the first approach is that you have to implement special | |||
|
249 | display methods when you define your class, one for each representation you | |||
|
250 | want to use. Here is a list of the names of the special methods and the | |||
|
251 | values they must return: | |||
|
252 | ||||
|
253 | - `_repr_html_`: return raw HTML as a string | |||
|
254 | - `_repr_json_`: return a JSONable dict | |||
|
255 | - `_repr_jpeg_`: return raw JPEG data | |||
|
256 | - `_repr_png_`: return raw PNG data | |||
|
257 | - `_repr_svg_`: return raw SVG data as a string | |||
|
258 | - `_repr_latex_`: return LaTeX commands in a string surrounded by "$". | |||
|
259 | - `_repr_mimebundle_`: return a full mimebundle containing the mapping | |||
|
260 | from all mimetypes to data | |||
|
261 | ||||
|
262 | When you are directly writing your own classes, you can adapt them for | |||
|
263 | display in IPython by following the above approach. But in practice, you | |||
|
264 | often need to work with existing classes that you can't easily modify. | |||
|
265 | ||||
|
266 | You can refer to the documentation on IPython display formatters in order to | |||
|
267 | register custom formatters for already existing types. | |||
|
268 | ||||
|
269 | Since IPython 5.4 and 6.1 display is automatically made available to the | |||
|
270 | user without import. If you are using display in a document that might be | |||
|
271 | used in a pure python context or with older version of IPython, use the | |||
|
272 | following import at the top of your file:: | |||
|
273 | ||||
|
274 | from IPython.display import display | |||
|
275 | ||||
183 | """ |
|
276 | """ | |
184 | raw = kwargs.pop('raw', False) |
|
277 | raw = kwargs.pop('raw', False) | |
185 | if transient is None: |
|
278 | if transient is None: | |
186 | transient = {} |
|
279 | transient = {} | |
187 | if display_id: |
|
280 | if display_id: | |
188 |
if display_id |
|
281 | if display_id is True: | |
189 | display_id = _new_id() |
|
282 | display_id = _new_id() | |
190 | transient['display_id'] = display_id |
|
283 | transient['display_id'] = display_id | |
191 | if kwargs.get('update') and 'display_id' not in transient: |
|
284 | if kwargs.get('update') and 'display_id' not in transient: |
@@ -56,6 +56,7 from IPython.core.payload import PayloadManager | |||||
56 | from IPython.core.prefilter import PrefilterManager |
|
56 | from IPython.core.prefilter import PrefilterManager | |
57 | from IPython.core.profiledir import ProfileDir |
|
57 | from IPython.core.profiledir import ProfileDir | |
58 | from IPython.core.usage import default_banner |
|
58 | from IPython.core.usage import default_banner | |
|
59 | from IPython.display import display | |||
59 | from IPython.testing.skipdoctest import skip_doctest |
|
60 | from IPython.testing.skipdoctest import skip_doctest | |
60 | from IPython.utils import PyColorize |
|
61 | from IPython.utils import PyColorize | |
61 | from IPython.utils import io |
|
62 | from IPython.utils import io | |
@@ -618,6 +619,7 class InteractiveShell(SingletonConfigurable): | |||||
618 | # removing on exit or representing the existence of more than one |
|
619 | # removing on exit or representing the existence of more than one | |
619 | # IPython at a time. |
|
620 | # IPython at a time. | |
620 | builtin_mod.__dict__['__IPYTHON__'] = True |
|
621 | builtin_mod.__dict__['__IPYTHON__'] = True | |
|
622 | builtin_mod.__dict__['display'] = display | |||
621 |
|
623 | |||
622 | self.builtin_trap = BuiltinTrap(shell=self) |
|
624 | self.builtin_trap = BuiltinTrap(shell=self) | |
623 |
|
625 |
General Comments 0
You need to be logged in to leave comments.
Login now