TypeError: bad operand type for abs(): 'dateti

2019-08-15 08:03发布

问题:

I have a very simple code that should draw an XY chart using pygal library, firstly, the code:

@app.route('/graph2/',methods=["GET","POST"])
def graph2():
    graph = pygal.XY()
    f=[('f20', 2868, datetime(2016, 5, 1, 6, 54, 46)),('f21', 2887, datetime(2016, 5, 1, 6, 55, 37)),('c9', 2868, datetime(2016, 5, 1, 6, 55, 42)),('f20', 2868, datetime(2016, 5, 1, 6, 55, 46)),('f21', 2887, datetime(2016, 5, 1, 6, 56, 37)),('c9', 2862, datetime(2016, 5, 1, 6, 56, 42))]
    graph.add('f21', [(item[2],item[1] if item[0]=='f21' else 0) for item in f] )
    graph.add('f20', [(item[2],item[1] if item[0]=='f20' else 0) for item in f] )
    values = [(item[2],item[1] if item[0]=='f20' else 0) for item in f]
    print values # values here is just for test purpose
    print "cool?" # this gets printed 
    graph_data = graph.render_data_uri()
    print "cooler?" # this is not printed 
    return render_template("graph.html", graph_data = graph_data)

So, it appears that this error happens when it is rendering

127.0.0.1 - - [01/May/2016 08:03:47] "GET /graph2/ HTTP/1.1" 500 -
Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\flask\app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
  File "C:\Python27\lib\site-packages\flask\app.py", line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "C:\Python27\lib\site-packages\flask\app.py", line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Python27\lib\site-packages\flask\app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Python27\lib\site-packages\flask\app.py", line 1477, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\Python27\lib\site-packages\flask\app.py", line 1381, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Python27\lib\site-packages\flask\app.py", line 1475, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\Python27\lib\site-packages\flask\app.py", line 1461, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "C:\Users\Adminstration\Desktop\flask-demo\flask-demo.py", line 145, in graph2
    graph_data = graph.render_data_uri()
  File "C:\Python27\lib\site-packages\pygal\graph\public.py", line 104, in render_data_uri
    self.render(**kwargs)
  File "C:\Python27\lib\site-packages\pygal\graph\public.py", line 49, in render
    self.setup(**kwargs)
  File "C:\Python27\lib\site-packages\pygal\graph\base.py", line 219, in setup
    self._draw()
  File "C:\Python27\lib\site-packages\pygal\graph\graph.py", line 924, in _draw
    self._compute_x_labels()
  File "C:\Python27\lib\site-packages\pygal\graph\dual.py", line 41, in _compute_x_labels
    self.order_min, self.min_scale, self.max_scale
  File "C:\Python27\lib\site-packages\pygal\util.py", line 164, in compute_scale
    order = round(log10(max(abs(min_), abs(max_)))) - 1
TypeError: bad operand type for abs(): 'datetime.datetime'

I have no clue how to fix this problem, any suggestions?