我正在构建一个bottle.py应用程序,它从MongoDB获取一些数据,并使用pygal将其呈现为网页。
这段代码在我的浏览器中生成了一个Error: 500 Internal Server Error。
在服务器上,我看到:Exception: TypeError('serve_static() takes exactly 1 argument (0 given)',)。
我的问题是:如何更正代码以呈现.svg文件?
代码:
import sys
import bottle
from bottle import get, post, request, route, run, static_file
import pymongo
import json
import pygal
connection = pymongo.MongoClient("mongodb://localhost", safe=True)
@get('/chart')
def serve_static(chart):
db = connection.control
chart = db.chart
cursor = chart.find({}, {"num":1, "x":1, "_id":0})
data = []
for doc in cursor:
data.append(doc)
list = [int(i.get('x')) for i in data]
line = pygal.Line()
line.title = 'widget quality'
line.x_labels = map(str, range(1, 20))
line.add('quality measure', list)
line.render_to_file('chart.svg')
try:
return static_file(chart.svg, root='/home/johnk/Desktop/chart/',mimetype='image/svg+xml')
except:
return "<p>Yikes! Somethin' wrong!</p>"
bottle.debug(True)
bottle.run(host='localhost', port=8080) 发布于 2014-12-03 00:58:33
您没有给路由提供参数,所以函数不会得到任何参数。
您可能想要做的是:
@get('/<chart>')
def serve_static(chart):
...如果您希望/myfile.svg正常工作,或者:
@get('/chart/<chart>')
def serve_static(chart):
...如果你想让/chart/myfile.svg正常工作。
如果您只想每次都显示相同的SVG文件,则可以省略该参数:
@get('/chart')
def serve_static():
...https://stackoverflow.com/questions/27254078
复制相似问题