Show More
@@ -1,124 +1,124 b'' | |||
|
1 | 1 | """An interface for publishing rich data to frontends. |
|
2 | 2 | |
|
3 | 3 | There are two components of the display system: |
|
4 | 4 | |
|
5 | 5 | * Display formatters, which take a Python object and compute the |
|
6 | 6 | representation of the object in various formats (text, HTML, SVG, etc.). |
|
7 | 7 | * The display publisher that is used to send the representation data to the |
|
8 | 8 | various frontends. |
|
9 | 9 | |
|
10 | 10 | This module defines the logic display publishing. The display publisher uses |
|
11 | 11 | the ``display_data`` message type that is defined in the IPython messaging |
|
12 | 12 | spec. |
|
13 | 13 | """ |
|
14 | 14 | |
|
15 | 15 | # Copyright (c) IPython Development Team. |
|
16 | 16 | # Distributed under the terms of the Modified BSD License. |
|
17 | 17 | |
|
18 | 18 | |
|
19 | 19 | import sys |
|
20 | 20 | |
|
21 | 21 | from traitlets.config.configurable import Configurable |
|
22 | 22 | from traitlets import List |
|
23 | 23 | |
|
24 | 24 | # This used to be defined here - it is imported for backwards compatibility |
|
25 | 25 | from .display import publish_display_data |
|
26 | 26 | |
|
27 | 27 | #----------------------------------------------------------------------------- |
|
28 | 28 | # Main payload class |
|
29 | 29 | #----------------------------------------------------------------------------- |
|
30 | 30 | |
|
31 | 31 | class DisplayPublisher(Configurable): |
|
32 | 32 | """A traited class that publishes display data to frontends. |
|
33 | 33 | |
|
34 | 34 | Instances of this class are created by the main IPython object and should |
|
35 | 35 | be accessed there. |
|
36 | 36 | """ |
|
37 | 37 | |
|
38 | 38 | def _validate_data(self, data, metadata=None): |
|
39 | 39 | """Validate the display data. |
|
40 | 40 | |
|
41 | 41 | Parameters |
|
42 | 42 | ---------- |
|
43 | 43 | data : dict |
|
44 | 44 | The formata data dictionary. |
|
45 | 45 | metadata : dict |
|
46 | 46 | Any metadata for the data. |
|
47 | 47 | """ |
|
48 | 48 | |
|
49 | 49 | if not isinstance(data, dict): |
|
50 | 50 | raise TypeError('data must be a dict, got: %r' % data) |
|
51 | 51 | if metadata is not None: |
|
52 | 52 | if not isinstance(metadata, dict): |
|
53 | 53 | raise TypeError('metadata must be a dict, got: %r' % data) |
|
54 | 54 | |
|
55 | 55 | # use * to indicate transient, update are keyword-only |
|
56 | 56 | def publish(self, data, metadata=None, source=None, *, transient=None, update=False, **kwargs): |
|
57 | 57 | """Publish data and metadata to all frontends. |
|
58 | 58 | |
|
59 | 59 | See the ``display_data`` message in the messaging documentation for |
|
60 | 60 | more details about this message type. |
|
61 | 61 | |
|
62 | 62 | The following MIME types are currently implemented: |
|
63 | 63 | |
|
64 | 64 | * text/plain |
|
65 | 65 | * text/html |
|
66 | 66 | * text/markdown |
|
67 | 67 | * text/latex |
|
68 | 68 | * application/json |
|
69 | 69 | * application/javascript |
|
70 | 70 | * image/png |
|
71 | 71 | * image/jpeg |
|
72 | 72 | * image/svg+xml |
|
73 | 73 | |
|
74 | 74 | Parameters |
|
75 | 75 | ---------- |
|
76 | 76 | data : dict |
|
77 | 77 | A dictionary having keys that are valid MIME types (like |
|
78 | 78 | 'text/plain' or 'image/svg+xml') and values that are the data for |
|
79 | 79 | that MIME type. The data itself must be a JSON'able data |
|
80 | 80 | structure. Minimally all data should have the 'text/plain' data, |
|
81 | 81 | which can be displayed by all frontends. If more than the plain |
|
82 | 82 | text is given, it is up to the frontend to decide which |
|
83 | 83 | representation to use. |
|
84 | 84 | metadata : dict |
|
85 | 85 | A dictionary for metadata related to the data. This can contain |
|
86 | 86 | arbitrary key, value pairs that frontends can use to interpret |
|
87 | 87 | the data. Metadata specific to each mime-type can be specified |
|
88 | 88 | in the metadata dict with the same mime-type keys as |
|
89 | 89 | the data itself. |
|
90 | 90 | source : str, deprecated |
|
91 | 91 | Unused. |
|
92 | 92 | transient: dict, keyword-only |
|
93 | 93 | A dictionary for transient data. |
|
94 | 94 | Data in this dictionary should not be persisted as part of saving this output. |
|
95 | 95 | Examples include 'display_id'. |
|
96 | 96 | update: bool, keyword-only, default: False |
|
97 | 97 | If True, only update existing outputs with the same display_id, |
|
98 | 98 | rather than creating a new output. |
|
99 | 99 | """ |
|
100 | 100 | |
|
101 | 101 | # The default is to simply write the plain text data using sys.stdout. |
|
102 | 102 | if 'text/plain' in data: |
|
103 | 103 | print(data['text/plain']) |
|
104 | 104 | |
|
105 | 105 | def clear_output(self, wait=False): |
|
106 | 106 | """Clear the output of the cell receiving output.""" |
|
107 | 107 | print('\033[2K\r', end='') |
|
108 | 108 | sys.stdout.flush() |
|
109 | 109 | print('\033[2K\r', end='') |
|
110 | 110 | sys.stderr.flush() |
|
111 | 111 | |
|
112 | 112 | |
|
113 | 113 | class CapturingDisplayPublisher(DisplayPublisher): |
|
114 | 114 | """A DisplayPublisher that stores""" |
|
115 | 115 | outputs = List() |
|
116 | 116 | |
|
117 | def publish(self, data, metadata=None, source=None): | |
|
117 | def publish(self, data, metadata=None, source=None, *, transient=None, update=False): | |
|
118 | 118 | self.outputs.append((data, metadata)) |
|
119 | 119 | |
|
120 | 120 | def clear_output(self, wait=False): |
|
121 | 121 | super(CapturingDisplayPublisher, self).clear_output(wait) |
|
122 | 122 | |
|
123 | 123 | # empty the list, *do not* reassign a new list |
|
124 | 124 | del self.outputs[:] |
General Comments 0
You need to be logged in to leave comments.
Login now