Show More
@@ -10,6 +10,7 b' try:' | |||||
10 | except ImportError: |
|
10 | except ImportError: | |
11 | from base64 import encodestring as base64_encode |
|
11 | from base64 import encodestring as base64_encode | |
12 |
|
12 | |||
|
13 | from binascii import b2a_hex | |||
13 | import json |
|
14 | import json | |
14 | import mimetypes |
|
15 | import mimetypes | |
15 | import os |
|
16 | import os | |
@@ -26,7 +27,7 b" __all__ = ['display', 'display_pretty', 'display_html', 'display_markdown'," | |||||
26 | 'display_javascript', 'display_pdf', 'DisplayObject', 'TextDisplayObject', |
|
27 | 'display_javascript', 'display_pdf', 'DisplayObject', 'TextDisplayObject', | |
27 | 'Pretty', 'HTML', 'Markdown', 'Math', 'Latex', 'SVG', 'JSON', 'Javascript', |
|
28 | 'Pretty', 'HTML', 'Markdown', 'Math', 'Latex', 'SVG', 'JSON', 'Javascript', | |
28 | 'Image', 'clear_output', 'set_matplotlib_formats', 'set_matplotlib_close', |
|
29 | 'Image', 'clear_output', 'set_matplotlib_formats', 'set_matplotlib_close', | |
29 | 'publish_display_data', 'update_display'] |
|
30 | 'publish_display_data', 'update_display', 'DisplayHandle'] | |
30 |
|
31 | |||
31 | #----------------------------------------------------------------------------- |
|
32 | #----------------------------------------------------------------------------- | |
32 | # utility functions |
|
33 | # utility functions | |
@@ -132,6 +133,12 b' def publish_display_data(data, metadata=None, source=None, *, transient=None, **' | |||||
132 | **kwargs |
|
133 | **kwargs | |
133 | ) |
|
134 | ) | |
134 |
|
135 | |||
|
136 | ||||
|
137 | def _new_id(): | |||
|
138 | """Generate a new random text id with urandom""" | |||
|
139 | return b2a_hex(os.urandom(16)).decode('ascii') | |||
|
140 | ||||
|
141 | ||||
135 | def display(*objs, include=None, exclude=None, metadata=None, transient=None, display_id=None, **kwargs): |
|
142 | def display(*objs, include=None, exclude=None, metadata=None, transient=None, display_id=None, **kwargs): | |
136 | """Display a Python object in all frontends. |
|
143 | """Display a Python object in all frontends. | |
137 |
|
144 | |||
@@ -163,13 +170,23 b' def display(*objs, include=None, exclude=None, metadata=None, transient=None, di' | |||||
163 | display_id : str, optional |
|
170 | display_id : str, optional | |
164 | Set an id for the display. |
|
171 | Set an id for the display. | |
165 | This id can be used for updating this display area later via update_display. |
|
172 | This id can be used for updating this display area later via update_display. | |
|
173 | If given as True, generate a new display_id | |||
166 | kwargs: additional keyword-args, optional |
|
174 | kwargs: additional keyword-args, optional | |
167 | Additional keyword-arguments are passed through to the display publisher. |
|
175 | Additional keyword-arguments are passed through to the display publisher. | |
|
176 | ||||
|
177 | Returns | |||
|
178 | ------- | |||
|
179 | ||||
|
180 | handle: DisplayHandle | |||
|
181 | Returns a handle on updatable displays, if display_id is given. | |||
|
182 | Returns None if no display_id is given (default). | |||
168 | """ |
|
183 | """ | |
169 | raw = kwargs.pop('raw', False) |
|
184 | raw = kwargs.pop('raw', False) | |
170 | if transient is None: |
|
185 | if transient is None: | |
171 | transient = {} |
|
186 | transient = {} | |
172 | if display_id: |
|
187 | if display_id: | |
|
188 | if display_id == True: | |||
|
189 | display_id = _new_id() | |||
173 | transient['display_id'] = display_id |
|
190 | transient['display_id'] = display_id | |
174 | if kwargs.get('update') and 'display_id' not in transient: |
|
191 | if kwargs.get('update') and 'display_id' not in transient: | |
175 | raise TypeError('display_id required for update_display') |
|
192 | raise TypeError('display_id required for update_display') | |
@@ -192,12 +209,13 b' def display(*objs, include=None, exclude=None, metadata=None, transient=None, di' | |||||
192 | if metadata: |
|
209 | if metadata: | |
193 | # kwarg-specified metadata gets precedence |
|
210 | # kwarg-specified metadata gets precedence | |
194 | _merge(md_dict, metadata) |
|
211 | _merge(md_dict, metadata) | |
195 | publish_display_data(data=format_dict, metadata=md_dict, |
|
212 | publish_display_data(data=format_dict, metadata=md_dict, **kwargs) | |
196 | **kwargs) |
|
213 | if display_id: | |
|
214 | return DisplayHandle(display_id) | |||
197 |
|
215 | |||
198 |
|
216 | |||
199 |
def update_display(obj, *, display_id |
|
217 | def update_display(obj, *, display_id, **kwargs): | |
200 |
"""Update an existing display |
|
218 | """Update an existing display by id | |
201 |
|
219 | |||
202 | Parameters |
|
220 | Parameters | |
203 | ---------- |
|
221 | ---------- | |
@@ -208,8 +226,51 b' def update_display(obj, *, display_id=None, **kwargs):' | |||||
208 | The id of the display to update |
|
226 | The id of the display to update | |
209 | """ |
|
227 | """ | |
210 | kwargs['update'] = True |
|
228 | kwargs['update'] = True | |
211 |
|
|
229 | display(obj, display_id=display_id, **kwargs) | |
|
230 | ||||
|
231 | ||||
|
232 | class DisplayHandle(object): | |||
|
233 | """A handle on an updatable display | |||
|
234 | ||||
|
235 | Call .update(obj) to display a new object. | |||
|
236 | ||||
|
237 | Call .display(obj) to add a new instance of this display, | |||
|
238 | and update existing instances. | |||
|
239 | """ | |||
|
240 | ||||
|
241 | def __init__(self, display_id=None): | |||
|
242 | if display_id is None: | |||
|
243 | display_id = _new_id() | |||
|
244 | self.display_id = display_id | |||
212 |
|
245 | |||
|
246 | def __repr__(self): | |||
|
247 | return "<%s display_id=%s>" % (self.__class__.__name__, self.display_id) | |||
|
248 | ||||
|
249 | def display(self, obj, **kwargs): | |||
|
250 | """Make a new display with my id, updating existing instances. | |||
|
251 | ||||
|
252 | Parameters | |||
|
253 | ---------- | |||
|
254 | ||||
|
255 | obj: | |||
|
256 | object to display | |||
|
257 | **kwargs: | |||
|
258 | additional keyword arguments passed to display | |||
|
259 | """ | |||
|
260 | display(obj, display_id=self.display_id, **kwargs) | |||
|
261 | ||||
|
262 | def update(self, obj, **kwargs): | |||
|
263 | """Update existing displays with my id | |||
|
264 | ||||
|
265 | Parameters | |||
|
266 | ---------- | |||
|
267 | ||||
|
268 | obj: | |||
|
269 | object to display | |||
|
270 | **kwargs: | |||
|
271 | additional keyword arguments passed to update_display | |||
|
272 | """ | |||
|
273 | update_display(obj, display_id=self.display_id, **kwargs) | |||
213 |
|
274 | |||
214 |
|
275 | |||
215 | def display_pretty(*objs, **kwargs): |
|
276 | def display_pretty(*objs, **kwargs): |
General Comments 0
You need to be logged in to leave comments.
Login now