class DisplayFormatter(Configurable): active_types = List(Unicode(), help="""List of currently active mime-types to display. You can use this to set a white-list for formats to display. Most users will not need to change this value. """).tag(config=True) @default('active_types') def _active_types_default(self): return self.format_types @observe('active_types') def _active_types_changed(self, change): for key, formatter in self.formatters.items(): if key in change['new']: formatter.enabled = True else: formatter.enabled = False ipython_display_formatter = ForwardDeclaredInstance('FormatterABC') @default('ipython_display_formatter') def _default_formatter(self): return IPythonDisplayFormatter(parent=self) # A dict of formatter whose keys are format types (MIME types) and whose # values are subclasses of BaseFormatter. formatters = Dict() @default('formatters') def _formatters_default(self): """Activate the default formatters.""" formatter_classes = [ PlainTextFormatter, HTMLFormatter, MarkdownFormatter, SVGFormatter, PNGFormatter, PDFFormatter, JPEGFormatter, LatexFormatter, JSONFormatter, JavascriptFormatter ] d = {} for cls in formatter_classes: f = cls(parent=self) d[f.format_type] = f return d def format(self, obj, include=None, exclude=None): """Return a format data dict for an object. By default all format types will be computed. The following MIME types are currently implemented: * text/plain * text/html * text/markdown * text/latex * application/json * application/javascript * application/pdf * image/png * image/jpeg * image/svg+xml Parameters ---------- obj : object The Python object whose format data will be computed. include : list or tuple, optional A list of format type strings (MIME types) to include in the format data dict. If this is set *only* the format types included in this list will be computed. exclude : list or tuple, optional A list of format type string (MIME types) to exclude in the format data dict. If this is set all format types will be computed, except for those included in this argument. Returns ------- (format_dict, metadata_dict) : tuple of two dicts format_dict is a dictionary of key/value pairs, one of each format that was generated for the object. The keys are the format types, which will usually be MIME type strings and the values and JSON'able data structure containing the raw data for the representation in that format. metadata_dict is a dictionary of metadata about each mime-type output. Its keys will be a strict subset of the keys in format_dict. """ format_dict = {} md_dict = {} if self.ipython_display_formatter(obj): # object handled itself, don't proceed return {}, {} for format_type, formatter in self.formatters.items(): if include and format_type not in include: continue if exclude and format_type in exclude: continue md = None try: data = formatter(obj) except: # FIXME: log the exception raise # formatters can return raw data or (data, metadata) if isinstance(data, tuple) and len(data) == 2: data, md = data if data is not None: format_dict[format_type] = data if md is not None: md_dict[format_type] = md return format_dict, md_dict @property def format_types(self): """Return the format types (MIME types) of the active formatters.""" return list(self.formatters.keys())
class ForwardDeclaredInstanceListTrait(HasTraits): value = List(ForwardDeclaredInstance('ForwardDeclaredBar'))
class DisplayFormatter(Configurable): active_types = List(Unicode(), help="""List of currently active mime-types to display. You can use this to set a white-list for formats to display. Most users will not need to change this value. """).tag(config=True) @default('active_types') def _active_types_default(self): return self.format_types @observe('active_types') def _active_types_changed(self, change): for key, formatter in self.formatters.items(): if key in change['new']: formatter.enabled = True else: formatter.enabled = False ipython_display_formatter = ForwardDeclaredInstance('FormatterABC') @default('ipython_display_formatter') def _default_formatter(self): return IPythonDisplayFormatter(parent=self) mimebundle_formatter = ForwardDeclaredInstance('FormatterABC') @default('mimebundle_formatter') def _default_mime_formatter(self): return MimeBundleFormatter(parent=self) # A dict of formatter whose keys are format types (MIME types) and whose # values are subclasses of BaseFormatter. formatters = Dict() @default('formatters') def _formatters_default(self): """Activate the default formatters.""" formatter_classes = [ PlainTextFormatter, HTMLFormatter, MarkdownFormatter, SVGFormatter, PNGFormatter, PDFFormatter, JPEGFormatter, LatexFormatter, JSONFormatter, JavascriptFormatter ] d = {} for cls in formatter_classes: f = cls(parent=self) d[f.format_type] = f return d def format(self, obj, include=None, exclude=None): """Return a format data dict for an object. By default all format types will be computed. The following MIME types are usually implemented: * text/plain * text/html * text/markdown * text/latex * application/json * application/javascript * application/pdf * image/png * image/jpeg * image/svg+xml Parameters ---------- obj : object The Python object whose format data will be computed. include : list, tuple or set; optional A list of format type strings (MIME types) to include in the format data dict. If this is set *only* the format types included in this list will be computed. exclude : list, tuple or set; optional A list of format type string (MIME types) to exclude in the format data dict. If this is set all format types will be computed, except for those included in this argument. Mimetypes present in exclude will take precedence over the ones in include Returns ------- (format_dict, metadata_dict) : tuple of two dicts format_dict is a dictionary of key/value pairs, one of each format that was generated for the object. The keys are the format types, which will usually be MIME type strings and the values and JSON'able data structure containing the raw data for the representation in that format. metadata_dict is a dictionary of metadata about each mime-type output. Its keys will be a strict subset of the keys in format_dict. Notes ----- If an object implement `_repr_mimebundle_` as well as various `_repr_*_`, the data returned by `_repr_mimebundle_` will take precedence and the corresponding `_repr_*_` for this mimetype will not be called. """ format_dict = {} md_dict = {} if self.ipython_display_formatter(obj): # object handled itself, don't proceed return {}, {} format_dict, md_dict = self.mimebundle_formatter(obj, include=include, exclude=exclude) if format_dict or md_dict: if include: format_dict = {k:v for k,v in format_dict.items() if k in include} md_dict = {k:v for k,v in md_dict.items() if k in include} if exclude: format_dict = {k:v for k,v in format_dict.items() if k not in exclude} md_dict = {k:v for k,v in md_dict.items() if k not in exclude} for format_type, formatter in self.formatters.items(): if format_type in format_dict: # already got it from mimebundle, maybe don't render again. # exception: manually registered per-mime renderer # check priority: # 1. user-registered per-mime formatter # 2. mime-bundle (user-registered or repr method) # 3. default per-mime formatter (e.g. repr method) try: formatter.lookup(obj) except KeyError: # no special formatter, use mime-bundle-provided value continue if include and format_type not in include: continue if exclude and format_type in exclude: continue md = None try: data = formatter(obj) except: # FIXME: log the exception raise # formatters can return raw data or (data, metadata) if isinstance(data, tuple) and len(data) == 2: data, md = data if data is not None: format_dict[format_type] = data if md is not None: md_dict[format_type] = md return format_dict, md_dict @property def format_types(self): """Return the format types (MIME types) of the active formatters.""" return list(self.formatters.keys())
class ForwardDeclaredInstanceTrait(HasTraits): value = ForwardDeclaredInstance('ForwardDeclaredBar', allow_none=True)