Show More
@@ -304,6 +304,11 b' class DisplayObject(object):' | |||||
304 | self.filename = None if filename is None else unicode_type(filename) |
|
304 | self.filename = None if filename is None else unicode_type(filename) | |
305 |
|
305 | |||
306 | self.reload() |
|
306 | self.reload() | |
|
307 | self._check_data() | |||
|
308 | ||||
|
309 | def _check_data(self): | |||
|
310 | """Override in subclasses if there's something to check.""" | |||
|
311 | pass | |||
307 |
|
312 | |||
308 | def reload(self): |
|
313 | def reload(self): | |
309 | """Reload the raw data from file or URL.""" |
|
314 | """Reload the raw data from file or URL.""" | |
@@ -331,13 +336,19 b' class DisplayObject(object):' | |||||
331 | except: |
|
336 | except: | |
332 | self.data = None |
|
337 | self.data = None | |
333 |
|
338 | |||
334 |
class |
|
339 | class TextDisplayObject(DisplayObject): | |
|
340 | """Validate that display data is text""" | |||
|
341 | def _check_data(self): | |||
|
342 | if self.data is not None and not isinstance(self.data, string_types): | |||
|
343 | raise TypeError("%s xpects text, not %r" % (self.__class__.__name__, self.data)) | |||
|
344 | ||||
|
345 | class Pretty(TextDisplayObject): | |||
335 |
|
346 | |||
336 | def _repr_pretty_(self): |
|
347 | def _repr_pretty_(self): | |
337 | return self.data |
|
348 | return self.data | |
338 |
|
349 | |||
339 |
|
350 | |||
340 | class HTML(DisplayObject): |
|
351 | class HTML(TextDisplayObject): | |
341 |
|
352 | |||
342 | def _repr_html_(self): |
|
353 | def _repr_html_(self): | |
343 | return self.data |
|
354 | return self.data | |
@@ -351,14 +362,14 b' class HTML(DisplayObject):' | |||||
351 | return self._repr_html_() |
|
362 | return self._repr_html_() | |
352 |
|
363 | |||
353 |
|
364 | |||
354 | class Math(DisplayObject): |
|
365 | class Math(TextDisplayObject): | |
355 |
|
366 | |||
356 | def _repr_latex_(self): |
|
367 | def _repr_latex_(self): | |
357 | s = self.data.strip('$') |
|
368 | s = self.data.strip('$') | |
358 | return "$$%s$$" % s |
|
369 | return "$$%s$$" % s | |
359 |
|
370 | |||
360 |
|
371 | |||
361 | class Latex(DisplayObject): |
|
372 | class Latex(TextDisplayObject): | |
362 |
|
373 | |||
363 | def _repr_latex_(self): |
|
374 | def _repr_latex_(self): | |
364 | return self.data |
|
375 | return self.data | |
@@ -398,7 +409,7 b' class SVG(DisplayObject):' | |||||
398 | return self.data |
|
409 | return self.data | |
399 |
|
410 | |||
400 |
|
411 | |||
401 | class JSON(DisplayObject): |
|
412 | class JSON(TextDisplayObject): | |
402 |
|
413 | |||
403 | def _repr_json_(self): |
|
414 | def _repr_json_(self): | |
404 | return self.data |
|
415 | return self.data | |
@@ -415,7 +426,7 b' lib_t1 = """$.getScript("%s", function () {' | |||||
415 | lib_t2 = """}); |
|
426 | lib_t2 = """}); | |
416 | """ |
|
427 | """ | |
417 |
|
428 | |||
418 | class Javascript(DisplayObject): |
|
429 | class Javascript(TextDisplayObject): | |
419 |
|
430 | |||
420 | def __init__(self, data=None, url=None, filename=None, lib=None, css=None): |
|
431 | def __init__(self, data=None, url=None, filename=None, lib=None, css=None): | |
421 | """Create a Javascript display object given raw data. |
|
432 | """Create a Javascript display object given raw data. |
General Comments 0
You need to be logged in to leave comments.
Login now