##// END OF EJS Templates
Merge pull request #10404 from gnestor/json-metadata...
Matthias Bussonnier -
r23504:1e874956 merge
parent child Browse files
Show More
@@ -622,7 +622,7 b' class JSON(DisplayObject):'
622 """
622 """
623 # wrap data in a property, which warns about passing already-serialized JSON
623 # wrap data in a property, which warns about passing already-serialized JSON
624 _data = None
624 _data = None
625 def __init__(self, data=None, url=None, filename=None, expanded=False, metadata=None):
625 def __init__(self, data=None, url=None, filename=None, expanded=False, metadata=None, **kwargs):
626 """Create a JSON display object given raw data.
626 """Create a JSON display object given raw data.
627
627
628 Parameters
628 Parameters
@@ -640,8 +640,11 b' class JSON(DisplayObject):'
640 metadata: dict
640 metadata: dict
641 Specify extra metadata to attach to the json display object.
641 Specify extra metadata to attach to the json display object.
642 """
642 """
643 self.expanded = expanded
643 self.metadata = {'expanded': expanded}
644 self.metadata = metadata
644 if metadata:
645 self.metadata.update(metadata)
646 if kwargs:
647 self.metadata.update(kwargs)
645 super(JSON, self).__init__(data=data, url=url, filename=filename)
648 super(JSON, self).__init__(data=data, url=url, filename=filename)
646
649
647 def _check_data(self):
650 def _check_data(self):
@@ -661,10 +664,7 b' class JSON(DisplayObject):'
661 self._data = data
664 self._data = data
662
665
663 def _data_and_metadata(self):
666 def _data_and_metadata(self):
664 md = {'expanded': self.expanded}
667 return self.data, self.metadata
665 if self.metadata:
666 md.update(self.metadata)
667 return self.data, md
668
668
669 def _repr_json_(self):
669 def _repr_json_(self):
670 return self._data_and_metadata()
670 return self._data_and_metadata()
@@ -681,17 +681,15 b' lib_t1 = """$.getScript("%s", function () {'
681 lib_t2 = """});
681 lib_t2 = """});
682 """
682 """
683
683
684 class GeoJSON(DisplayObject):
684 class GeoJSON(JSON):
685 """GeoJSON expects JSON-able dict
685 """GeoJSON expects JSON-able dict
686
686
687 not an already-serialized JSON string.
687 not an already-serialized JSON string.
688
688
689 Scalar types (None, number, string) are not allowed, only dict containers.
689 Scalar types (None, number, string) are not allowed, only dict containers.
690 """
690 """
691 # wrap data in a property, which warns about passing already-serialized JSON
692 _data = None
693
691
694 def __init__(self, data=None, url_template=None, layer_options=None, url=None, filename=None, metadata=None):
692 def __init__(self, *args, **kwargs):
695 """Create a GeoJSON display object given raw data.
693 """Create a GeoJSON display object given raw data.
696
694
697 Parameters
695 Parameters
@@ -724,9 +722,6 b' class GeoJSON(DisplayObject):'
724 ... "geometry": {
722 ... "geometry": {
725 ... "type": "Point",
723 ... "type": "Point",
726 ... "coordinates": [-81.327, 296.038]
724 ... "coordinates": [-81.327, 296.038]
727 ... },
728 ... "properties": {
729 ... "name": "Inca City"
730 ... }
725 ... }
731 ... },
726 ... },
732 ... url_template="http://s3-eu-west-1.amazonaws.com/whereonmars.cartodb.net/{basemap_id}/{z}/{x}/{y}.png",
727 ... url_template="http://s3-eu-west-1.amazonaws.com/whereonmars.cartodb.net/{basemap_id}/{z}/{x}/{y}.png",
@@ -742,41 +737,17 b' class GeoJSON(DisplayObject):'
742 the GeoJSON object.
737 the GeoJSON object.
743
738
744 """
739 """
745 self.url_template = url_template
746 self.layer_options = layer_options
747 self.metadata = metadata
748 super(GeoJSON, self).__init__(data=data, url=url, filename=filename)
749
750 def _check_data(self):
751 if self.data is not None and not isinstance(self.data, dict):
752 raise TypeError("%s expects a JSONable dict, not %r" % (self.__class__.__name__, self.data))
753
740
754 @property
741 super(GeoJSON, self).__init__(*args, **kwargs)
755 def data(self):
742
756 return self._data
757
758 @data.setter
759 def data(self, data):
760 if isinstance(data, str):
761 if getattr(self, 'filename', None) is None:
762 warnings.warn("GeoJSON expects JSONable dict or list, not JSON strings")
763 data = json.loads(data)
764 self._data = data
765
743
766 def _ipython_display_(self):
744 def _ipython_display_(self):
767 md = {}
768 if self.url_template:
769 md['tileUrlTemplate'] = self.url_template
770 if self.layer_options:
771 md['tileLayerOptions'] = self.layer_options
772 if self.metadata:
773 md.update(self.metadata)
774 bundle = {
745 bundle = {
775 'application/geo+json': self.data,
746 'application/geo+json': self.data,
776 'text/plain': '<jupyterlab_geojson.GeoJSON object>'
747 'text/plain': '<IPython.display.GeoJSON object>'
777 }
748 }
778 metadata = {
749 metadata = {
779 'application/geo+json': md
750 'application/geo+json': self.metadata
780 }
751 }
781 display(bundle, metadata=metadata, raw=True)
752 display(bundle, metadata=metadata, raw=True)
782
753
General Comments 0
You need to be logged in to leave comments. Login now