Hello Everyone,
I am new to the form. It was so nice to e-meeting you here.
I have been struggled with this Polygon is not JSON serializable for couple days and hope someone may help.
The situation is that I have a shape file. I am trying to use CustoJS to call back after some filter data I select.
The logic is geopandas dataframe → dictionary → convert to JS by using to_json() → apply GeoJSONDataSource
Everything seems fine until I called the show(figure) and it showed the error (TypeError: Object of type Polygon is not JSON serializable)
May I know what the reason is and how I can resolve it?
Thank you so much.
Best,
David
Below is the error message:
<details class='elided'>
<summary title='Show trimmed content'>···</summary>
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-490-54a49b4db53d> in <module>()
141 layout = column(widgetbox(select, data_table))
142
--> 143 show(layout)
/anaconda3/lib/python3.7/site-packages/bokeh/io/showing.py in show(obj, browser, new, notebook_handle, notebook_url)
135 if obj not in state.document.roots:
136 state.document.add_root(obj)
--> 137 return _show_with_state(obj, state, browser, new, notebook_handle=notebook_handle)
138
139 #-----------------------------------------------------------------------------
/anaconda3/lib/python3.7/site-packages/bokeh/io/showing.py in _show_with_state(obj, state, browser, new, notebook_handle)
173
174 if state.file or not shown:
--> 175 _show_file_with_state(obj, state, new, controller)
176
177 return comms_handle
/anaconda3/lib/python3.7/site-packages/bokeh/io/showing.py in _show_file_with_state(obj, state, new, controller)
156
157 '''
--> 158 filename = save(obj, state=state)
159 controller.open("file://" + filename, new=NEW_PARAM[new])
160
/anaconda3/lib/python3.7/site-packages/bokeh/io/saving.py in save(obj, filename, resources, title, template, state, **kwargs)
81
82 filename, resources, title = _get_save_args(state, filename, resources, title)
---> 83 _save_helper(obj, filename, resources, title, template)
84 return abspath(filename)
85
/anaconda3/lib/python3.7/site-packages/bokeh/io/saving.py in _save_helper(obj, filename, resources, title, template)
143 '''
144 from ..embed import file_html
--> 145 html = file_html(obj, resources, title=title, template=template)
146
147 with io.open(filename, mode="w", encoding="utf-8") as f:
/anaconda3/lib/python3.7/site-packages/bokeh/embed/standalone.py in file_html(models, resources, title, template, template_variables, theme)
279
280 with _ModelInDocument(models, apply_theme=theme) as doc:
--> 281 (docs_json, render_items) = standalone_docs_json_and_render_items(models)
282 title = _title_from_models(models, title)
283 bundle = bundle_for_objs_and_resources([doc], resources)
/anaconda3/lib/python3.7/site-packages/bokeh/embed/util.py in standalone_docs_json_and_render_items(models)
338 docs_json = {}
339 for doc, (docid, _) in docs.items():
--> 340 docs_json[docid] = doc.to_json()
341
342 render_items = []
/anaconda3/lib/python3.7/site-packages/bokeh/document/document.py in to_json(self)
779 # this is a total hack to go via a string, needed because
780 # our BokehJSONEncoder goes straight to a string.
--> 781 doc_json = self.to_json_string()
782 return loads(doc_json)
783
/anaconda3/lib/python3.7/site-packages/bokeh/document/document.py in to_json_string(self, indent)
808 }
809
--> 810 return serialize_json(json, indent=indent)
811
812 def validate(self):
/anaconda3/lib/python3.7/site-packages/bokeh/core/json_encoder.py in serialize_json(obj, pretty, indent, **kwargs)
211 indent = 2
212
--> 213 return json.dumps(obj, cls=BokehJSONEncoder, allow_nan=False, indent=indent, separators=separators, sort_keys=True, **kwargs)
/anaconda3/lib/python3.7/json/__init__.py in dumps(obj, skipkeys, ensure_ascii, check_circular, allow_nan, cls, indent, separators, default, sort_keys, **kw)
236 check_circular=check_circular, allow_nan=allow_nan, indent=indent,
237 separators=separators, default=default, sort_keys=sort_keys,
--> 238 **kw).encode(obj)
239
240
/anaconda3/lib/python3.7/json/encoder.py in encode(self, o)
197 # exceptions aren't as detailed. The list call should be roughly
198 # equivalent to the PySequence_Fast that ''.join() would do.
--> 199 chunks = self.iterencode(o, _one_shot=True)
200 if not isinstance(chunks, (list, tuple)):
201 chunks = list(chunks)
/anaconda3/lib/python3.7/json/encoder.py in iterencode(self, o, _one_shot)
255 self.key_separator, self.item_separator, self.sort_keys,
256 self.skipkeys, _one_shot)
--> 257 return _iterencode(o, 0)
258
259 def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,
/anaconda3/lib/python3.7/site-packages/bokeh/core/json_encoder.py in default(self, obj)
133
134 else:
--> 135 return self.transform_python_types(obj)
136
137 def serialize_json(obj, pretty=None, indent=None, **kwargs):
/anaconda3/lib/python3.7/site-packages/bokeh/core/json_encoder.py in transform_python_types(self, obj)
100
101 else:
--> 102 return super(BokehJSONEncoder, self).default(obj)
103
104 def default(self, obj):
/anaconda3/lib/python3.7/json/encoder.py in default(self, o)
177
178 """
--> 179 raise TypeError(f'Object of type {o.__class__.__name__} '
180 f'is not JSON serializable')
181
TypeError: Object of type Polygon is not JSON serializable