##// END OF EJS Templates
JSON: Include kwargs in metadata
Grant Nestor -
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, 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,9 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 = kwargs
644 self.metadata = metadata
644 if metadata:
645 self.metadata.update(metadata)
645 super(JSON, self).__init__(data=data, url=url, filename=filename)
646 super(JSON, self).__init__(data=data, url=url, filename=filename)
646
647
647 def _check_data(self):
648 def _check_data(self):
@@ -661,10 +662,7 b' class JSON(DisplayObject):'
661 self._data = data
662 self._data = data
662
663
663 def _data_and_metadata(self):
664 def _data_and_metadata(self):
664 md = {'expanded': self.expanded}
665 return self.data, self.metadata
665 if self.metadata:
666 md.update(self.metadata)
667 return self.data, md
668
666
669 def _repr_json_(self):
667 def _repr_json_(self):
670 return self._data_and_metadata()
668 return self._data_and_metadata()
@@ -681,17 +679,15 b' lib_t1 = """$.getScript("%s", function () {'
681 lib_t2 = """});
679 lib_t2 = """});
682 """
680 """
683
681
684 class GeoJSON(DisplayObject):
682 class GeoJSON(JSON):
685 """GeoJSON expects JSON-able dict
683 """GeoJSON expects JSON-able dict
686
684
687 not an already-serialized JSON string.
685 not an already-serialized JSON string.
688
686
689 Scalar types (None, number, string) are not allowed, only dict containers.
687 Scalar types (None, number, string) are not allowed, only dict containers.
690 """
688 """
691 # wrap data in a property, which warns about passing already-serialized JSON
692 _data = None
693
689
694 def __init__(self, data=None, url_template=None, layer_options=None, url=None, filename=None, metadata=None):
690 def __init__(self, *args, **kwargs):
695 """Create a GeoJSON display object given raw data.
691 """Create a GeoJSON display object given raw data.
696
692
697 Parameters
693 Parameters
@@ -724,9 +720,6 b' class GeoJSON(DisplayObject):'
724 ... "geometry": {
720 ... "geometry": {
725 ... "type": "Point",
721 ... "type": "Point",
726 ... "coordinates": [-81.327, 296.038]
722 ... "coordinates": [-81.327, 296.038]
727 ... },
728 ... "properties": {
729 ... "name": "Inca City"
730 ... }
723 ... }
731 ... },
724 ... },
732 ... url_template="http://s3-eu-west-1.amazonaws.com/whereonmars.cartodb.net/{basemap_id}/{z}/{x}/{y}.png",
725 ... url_template="http://s3-eu-west-1.amazonaws.com/whereonmars.cartodb.net/{basemap_id}/{z}/{x}/{y}.png",
@@ -742,41 +735,17 b' class GeoJSON(DisplayObject):'
742 the GeoJSON object.
735 the GeoJSON object.
743
736
744 """
737 """
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
738
754 @property
739 super(GeoJSON, self).__init__(*args, **kwargs)
755 def data(self):
740
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
741
766 def _ipython_display_(self):
742 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 = {
743 bundle = {
775 'application/geo+json': self.data,
744 'application/geo+json': self.data,
776 'text/plain': '<jupyterlab_geojson.GeoJSON object>'
745 'text/plain': '<IPython.display.GeoJSON object>'
777 }
746 }
778 metadata = {
747 metadata = {
779 'application/geo+json': md
748 'application/geo+json': self.metadata
780 }
749 }
781 display(bundle, metadata=metadata, raw=True)
750 display(bundle, metadata=metadata, raw=True)
782
751
General Comments 0
You need to be logged in to leave comments. Login now