json_dumps - ValueError: Circular reference detected

I'm on an error since 2 days and I don't really know what to do ... I want to display a data frame as a graph in my Flask app using Plotly. The data frame looks good (Figure1) The figure is well shown using fig.show() BUT when I want to convert to json I have the following error:

Traceback (most recent call last):
  File "/Users/benjamin/PycharmProjects/sorare-app/webapp.py", line 32, in homepage
    graph_json = json.dumps(fig, cls=plotly.utils.PlotlyJSONEncoder)
  File "/Users/benjamin/PycharmProjects/sorare-collect/venv/lib/python3.10/site-packages/dynamodb_json/json_util.py", line 40, in dumps
    result_ = TypeSerializer().serialize(json.loads(json.dumps(dct, default=json_serial),
  File "/Users/benjamin/PycharmProjects/sorare-collect/venv/lib/python3.10/site-packages/simplejson/__init__.py", line 412, in dumps
    **kw).encode(obj)
  File "/Users/benjamin/PycharmProjects/sorare-collect/venv/lib/python3.10/site-packages/simplejson/encoder.py", line 296, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/Users/benjamin/PycharmProjects/sorare-collect/venv/lib/python3.10/site-packages/simplejson/encoder.py", line 378, in iterencode
    return _iterencode(o, 0)
ValueError: Circular reference detected

Does someone could help me please ? I don't know how to face it ... Thanks in advance

My full code here:

leaderboards = football_so5_leaderboards.get_leaderboards_in_group("global-all_star-division-4", dynamodb_client)
df = pd.DataFrame(json.loads(leaderboards))
print(df)
fig = px.line(df, x = 'so5_fixture_gameweek', y='last_rewarded_score', title='Average Last Rewarded Score', labels={
                 'so5_fixture_gameweek': 'Gameweek',
                 'last_rewarded_score': 'Score'
                })
fig.show()
print(fig)
graph_json = json.dumps(fig, cls=plotly.utils.PlotlyJSONEncoder)
return render_template('homepage.html', grap_json=graph_json)

Figure 1: dataframe

    so5_fixture_gameweek  last_rewarded_score
0                    298               341.25
1                    299               322.81
2                    300                 0.00
3                    301               360.42
4                    302               347.76
5                    303                 0.00
6                    304               345.43
7                    305                 0.00
8                    306                 0.00
9                    307               302.05
10                   308                 0.00
11                   309               296.09
12                   310                 0.00
13                   311                 0.00
14                   312                 0.00
15                   313                 0.00
16                   314               332.46
17                   315               353.26
18                   316               332.81
19                   317               364.27
20                   318               342.25
21                   319               356.51
22                   320               333.90
23                   321               333.16

Figure 2: fig

Figure({
    'data': [{'hovertemplate': 'Gameweek=%{x}<br>Score=%{y}<extra></extra>',
              'legendgroup': '',
              'line': {'color': '#636efa', 'dash': 'solid'},
              'marker': {'symbol': 'circle'},
              'mode': 'lines',
              'name': '',
              'orientation': 'v',
              'showlegend': False,
              'type': 'scatter',
              'x': array([298, 299, 300, 301, 302, 303, 304, 305, 306, 307, 308, 309, 310, 311,
                          312, 313, 314, 315, 316, 317, 318, 319, 320, 321]),
              'xaxis': 'x',
              'y': array([341.25, 322.81,   0.  , 360.42, 347.76,   0.  , 345.43,   0.  ,   0.  ,
                          302.05,   0.  , 296.09,   0.  ,   0.  ,   0.  ,   0.  , 332.46, 353.26,
                          332.81, 364.27, 342.25, 356.51, 333.9 , 333.16]),
              'yaxis': 'y'}],
    'layout': {'legend': {'tracegroupgap': 0},
               'template': '...',
               'title': {'text': 'Average Last Rewarded Score'},
               'xaxis': {'anchor': 'y', 'domain': [0.0, 1.0], 'title': {'text': 'Gameweek'}},
               'yaxis': {'anchor': 'x', 'domain': [0.0, 1.0], 'title': {'text': 'Score'}}}
})

I've load date in data frame and create a figure. Then convert to Json Figure should be well convert in Json but it dont

Upvotes: 0

Views: 1285

Answers (0)

Related Questions