Show More
@@ -622,7 +622,7 b' class JSON(DisplayObject):' | |||
|
622 | 622 | """ |
|
623 | 623 | # wrap data in a property, which warns about passing already-serialized JSON |
|
624 | 624 | _data = None |
|
625 |
def __init__(self, data=None, url=None, filename=None, |
|
|
625 | def __init__(self, data=None, url=None, filename=None, metadata=None, **kwargs): | |
|
626 | 626 | """Create a JSON display object given raw data. |
|
627 | 627 | |
|
628 | 628 | Parameters |
@@ -640,8 +640,9 b' class JSON(DisplayObject):' | |||
|
640 | 640 | metadata: dict |
|
641 | 641 | Specify extra metadata to attach to the json display object. |
|
642 | 642 | """ |
|
643 |
self. |
|
|
644 |
|
|
|
643 | self.metadata = kwargs | |
|
644 | if metadata: | |
|
645 | self.metadata.update(metadata) | |
|
645 | 646 | super(JSON, self).__init__(data=data, url=url, filename=filename) |
|
646 | 647 | |
|
647 | 648 | def _check_data(self): |
@@ -661,10 +662,7 b' class JSON(DisplayObject):' | |||
|
661 | 662 | self._data = data |
|
662 | 663 | |
|
663 | 664 | def _data_and_metadata(self): |
|
664 | md = {'expanded': self.expanded} | |
|
665 | if self.metadata: | |
|
666 | md.update(self.metadata) | |
|
667 | return self.data, md | |
|
665 | return self.data, self.metadata | |
|
668 | 666 | |
|
669 | 667 | def _repr_json_(self): |
|
670 | 668 | return self._data_and_metadata() |
@@ -681,17 +679,15 b' lib_t1 = """$.getScript("%s", function () {' | |||
|
681 | 679 | lib_t2 = """}); |
|
682 | 680 | """ |
|
683 | 681 | |
|
684 |
class GeoJSON( |
|
|
682 | class GeoJSON(JSON): | |
|
685 | 683 | """GeoJSON expects JSON-able dict |
|
686 | 684 | |
|
687 | 685 | not an already-serialized JSON string. |
|
688 | 686 | |
|
689 | 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 | 691 | """Create a GeoJSON display object given raw data. |
|
696 | 692 | |
|
697 | 693 | Parameters |
@@ -724,9 +720,6 b' class GeoJSON(DisplayObject):' | |||
|
724 | 720 | ... "geometry": { |
|
725 | 721 | ... "type": "Point", |
|
726 | 722 | ... "coordinates": [-81.327, 296.038] |
|
727 | ... }, | |
|
728 | ... "properties": { | |
|
729 | ... "name": "Inca City" | |
|
730 | 723 | ... } |
|
731 | 724 | ... }, |
|
732 | 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 | 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 | |
|
755 | def data(self): | |
|
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 | |
|
739 | super(GeoJSON, self).__init__(*args, **kwargs) | |
|
740 | ||
|
765 | 741 | |
|
766 | 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 | 743 | bundle = { |
|
775 | 744 | 'application/geo+json': self.data, |
|
776 |
'text/plain': '< |
|
|
745 | 'text/plain': '<IPython.display.GeoJSON object>' | |
|
777 | 746 | } |
|
778 | 747 | metadata = { |
|
779 | 'application/geo+json': md | |
|
748 | 'application/geo+json': self.metadata | |
|
780 | 749 | } |
|
781 | 750 | display(bundle, metadata=metadata, raw=True) |
|
782 | 751 |
General Comments 0
You need to be logged in to leave comments.
Login now