agiliq/django-graphos

zip object is not JSON serializable when generating graph

Closed this issue · 4 comments

By following the tutorial, I have the following set up

# view 
def salesboard(request):
    # render the graph here
    data =  [
            ['Year', 'Sales', 'Expenses'],
            [2004, 1000, 400],
            [2005, 1170, 460],
            [2006, 660, 1120],
            [2007, 1030, 540]
        ]
    chart = flot.LineChart(SimpleDataSource(data=data), html_id="Line Chart")
    return render_to_response('scoreboard.html', {'chart':chart})

Template
{{ chart.as_html }}

Error

Error during template rendering

In template C:\Users\TIMLEU~1\Desktop\SYSTEM~1\sp-venv\lib\site-packages\graphos\templates\graphos\flot.html, error at line 4
<zip object at 0x00000052AAC8D448> is not JSON serializable
1   <div id="{{ chart.html_id }}" style="width:{{ chart.width }}px;height:{{ chart.height }}px;"></div>
2   <script type="text/javascript">
3   $(function () {
4       data{{ chart.html_id }} = {{ chart.get_series_objects_json|safe }}
5       $plot{{ chart.html_id }} = $.plot(
6           $("#{{ chart.html_id }}"),
7           data{{ chart.html_id }},
8           {{ chart.get_options_json|safe }}
9       );
10  });
11  </script>

I'm having the same issue with python3.4
I did some digging and found that get_serieses in BaseFlowChart is storing the result of zip which then gets json serialized.
image
The problem is that the zip method in python 2 returns a list of tuples whereas in python 3 a zip object is returned.
The zip should be converted to a list as such.
image

Yup Thats what I did to fix it as well! Feel like we should create a pull request for this

Pull request #32 takes care of this.

I've included this pull request in my fork at https://github.com/ModusVivendi/django-graphos and also on pip at django-graphos-3.

@timothyleung @e78908b943c8 @ModusVivendi @sheepeatingtaz PR #32 has been merged and updated code has been uploaded to Pypi. With recent "pip install django-graphos" this issue should be resolved.