sam_b
sam_b

Reputation: 43

Dash Plotly error TypeError: Object of type DataFrame is not JSON serializable

Hello I am working with Dash for making dashboard. Below is my code. I tried to fix the error but not able to fix, Can anyone look into this? on chrome i am getting. Error loading layout I am getting TypeError

import dash_bootstrap_components as dbc
from dash import dcc
import dash_html_components as html
from dash import dash_table
import pandas as pd
import numpy as np

def getData():
    return preprocess()

def back_to_df(dictio):
    return pd.DataFrame.from_dict(dictio)

tblcols  =[{"name": i, "id": i} for i in back_to_df(getData()).columns]

app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
body = html.Div([
    html.H1("Live rates")
    , dbc.Row([
            dbc.Col(html.Div([dcc.Interval('graph-update', interval = 80, n_intervals = 0),
      dash_table.DataTable(
          id = 'table',
          data = getData(),
          columns=tblcols,
          page_size= 10,
          style_table={'overflowX': 'auto'},
      )]),width=3)
            ])
        ])
app.layout = html.Div([body])

@app.callback(
        dash.dependencies.Output('table','data'),
        [dash.dependencies.Input('graph-update', 'n_intervals')])
def updateTable(n):
     return getData()

if __name__ == "__main__":
  app.run_server(debug = False, port = 8010)

I tried to fix the error but not able to fix, Can anyone look into this? I am getting error as follows. Looking for help for below error. dash pandas plotly dataframe

Traceback (most recent call last):
  File "C:\Users\Admin\anaconda3\lib\site-packages\flask\app.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Users\Admin\anaconda3\lib\site-packages\flask\app.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\Users\Admin\anaconda3\lib\site-packages\flask\app.py", line 1821, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Users\Admin\anaconda3\lib\site-packages\flask\_compat.py", line 39, in reraise
    raise value
  File "C:\Users\Admin\anaconda3\lib\site-packages\flask\app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\Users\Admin\anaconda3\lib\site-packages\flask\app.py", line 1936, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "C:\Users\Admin\anaconda3\lib\site-packages\dash\dash.py", line 569, in serve_layout
    to_json(layout),
  File "C:\Users\Admin\anaconda3\lib\site-packages\dash\_utils.py", line 20, in to_json
    return to_json_plotly(value)
  File "C:\Users\Admin\anaconda3\lib\site-packages\plotly\io\_json.py", line 124, in to_json_plotly
    return json.dumps(plotly_object, cls=PlotlyJSONEncoder, **opts)
  File "C:\Users\Admin\anaconda3\lib\json\__init__.py", line 234, in dumps
    return cls(
  File "C:\Users\Admin\anaconda3\lib\site-packages\_plotly_utils\utils.py", line 59, in encode
    encoded_o = super(PlotlyJSONEncoder, self).encode(o)
  File "C:\Users\Admin\anaconda3\lib\json\encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "C:\Users\Admin\anaconda3\lib\json\encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File "C:\Users\Admin\anaconda3\lib\site-packages\_plotly_utils\utils.py", line 136, in default
    return _json.JSONEncoder.default(self, obj)
  File "C:\Users\Admin\anaconda3\lib\json\encoder.py", line 179, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '

TypeError: Object of type DataFrame is not JSON serializable```

Upvotes: 0

Views: 2064

Answers (1)

coralvanda
coralvanda

Reputation: 6606

Sounds like the getData function is returning a pandas DataFrame directly. That won't work. You'll need to do this:

return df.to_dict(orient='records')

That should work.

Upvotes: 1

Related Questions