2.0 -> 2.0.2 "TypeError: Object of type 'type' is not JSON serializable"?

Hello,

I run the below code in PyCharm and get:
TypeError: Object of type type is not JSON serializable

When I run from the command line I get:
TypeError: Object of type ‘type’ is not JSON serializable

(anonymized stack trace below code)

# de_minimis.py
from bokeh.plotting import figure
from bokeh.models import CustomJS, Toggle
from bokeh.io import show
from bokeh.models.layouts import Row

p = figure(plot_height=800, plot_width=1000, title="Metric",
               toolbar_location=None, tools="", sizing_mode='fixed')

controls_choice_show_toggle = Toggle(visible=True, name='controls_choice_show_toggle', tags=['Show_Toggle'], button_type='success', label='Show Control Options', disabled=False)

unified_callback = CustomJS(
        args=dict(filter=filter, p=p,
                  controls_choice_show_toggle=controls_choice_show_toggle
                  ), code=''' ''')

controls_choice_show_toggle.js_on_change('value', unified_callback)
show(Row(controls_choice_show_toggle, p))

WARNING:bokeh.core.validation.check:W-1000 (MISSING_RENDERERS): Plot has no renderers: Figure(id=‘1001’, …)
Traceback (most recent call last):

  • File “C:/Projects/BarCharts/de_minimis.py”, line 18, in *
  • show(Row(controls_choice_show_toggle, p))*
  • File “C:\Users\me\AppData\Local\Programs\Python\Python38-32\lib\site-packages\bokeh\io\showing.py”, line 145, in show*
  • return _show_with_state(obj, state, browser, new, notebook_handle=notebook_handle)*
  • File “C:\Users\me\AppData\Local\Programs\Python\Python38-32\lib\site-packages\bokeh\io\showing.py”, line 183, in _show_with_state*
  • _show_file_with_state(obj, state, new, controller)*
  • File “C:\Users\me\AppData\Local\Programs\Python\Python38-32\lib\site-packages\bokeh\io\showing.py”, line 166, in _show_file_with_state*
  • filename = save(obj, state=state)*
  • File “C:\Users\me\AppData\Local\Programs\Python\Python38-32\lib\site-packages\bokeh\io\saving.py”, line 86, in save*
  • _save_helper(obj, filename, resources, title, template)*
  • File “C:\Users\me\AppData\Local\Programs\Python\Python38-32\lib\site-packages\bokeh\io\saving.py”, line 148, in _save_helper*
  • html = file_html(obj, resources, title=title, template=template)*
  • File “C:\Users\me\AppData\Local\Programs\Python\Python38-32\lib\site-packages\bokeh\embed\standalone.py”, line 291, in file_html*
  • (docs_json, render_items) = standalone_docs_json_and_render_items(models, suppress_callback_warning=suppress_callback_warning)*
  • File “C:\Users\me\AppData\Local\Programs\Python\Python38-32\lib\site-packages\bokeh\embed\util.py”, line 297, in standalone_docs_json_and_render_items*
  • docs_json[docid] = doc.to_json()*
  • File “C:\Users\me\AppData\Local\Programs\Python\Python38-32\lib\site-packages\bokeh\document\document.py”, line 845, in to_json*
  • doc_json = self.to_json_string()*
  • File “C:\Users\me\AppData\Local\Programs\Python\Python38-32\lib\site-packages\bokeh\document\document.py”, line 874, in to_json_string*
  • return serialize_json(json, indent=indent)*
  • File “C:\Users\me\AppData\Local\Programs\Python\Python38-32\lib\site-packages\bokeh\core\json_encoder.py”, line 160, in serialize_json*
  • return json.dumps(obj, cls=BokehJSONEncoder, allow_nan=False, indent=indent, separators=separators, sort_keys=True, *kwargs)
  • File “C:\Users\me\AppData\Local\Programs\Python\Python38-32\lib\json_init_.py”, line 234, in dumps*
  • return cls(*
  • File “C:\Users\me\AppData\Local\Programs\Python\Python38-32\lib\json\encoder.py”, line 199, in encode*
  • chunks = self.iterencode(o, _one_shot=True)*
  • File “C:\Users\me\AppData\Local\Programs\Python\Python38-32\lib\json\encoder.py”, line 257, in iterencode*
  • return _iterencode(o, 0)*
  • File “C:\Users\me\AppData\Local\Programs\Python\Python38-32\lib\site-packages\bokeh\core\json_encoder.py”, line 251, in default*
  • return self.transform_python_types(obj)*
  • File “C:\Users\me\AppData\Local\Programs\Python\Python38-32\lib\site-packages\bokeh\core\json_encoder.py”, line 218, in transform_python_types*
  • return super(BokehJSONEncoder, self).default(obj)*
  • File “C:\Users\me\AppData\Local\Programs\Python\Python38-32\lib\json\encoder.py”, line 179, in default*
  • raise TypeError(f’Object of type {o.class.name} '*
    TypeError: Object of type type is not JSON serializable

Process finished with exit code 1

Note: in my full application I get the same error, but not a “missing renderers” warning.

What am I doing wrong? Is there a guide I can refer to for converting my code from the “Soon to be deprecated” to the “We really deprecated it now, get at it!” ?

Thank you for your time.
Kyle

Hi @Kyle_Marcroft

Your anonymized code includes the following statement

unified_callback = CustomJS(
        args=dict(filter=filter, p=p,
                  controls_choice_show_toggle=controls_choice_show_toggle
                  ), code=''' ''')

However, there is no quantity filter defined. Moreover, filter is a built-in function in Python 3, which is the source of the error you are seeing, I believe. See https://docs.python.org/3/library/functions.html

I hope this helps.

J

1 Like

At one point I had been using filters to control what is displayed in a DataTable. Removing the assignment fixed the cryptic error. Thank you!