2014-12-02 95 views
0

我正在构建一个bottle.py应用程序,它从MongoDB获取一些数据并使用pygal将其呈现到网页中。bottle.py渲染静态文件

该代码在我的浏览器中生成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) 

回答

2

你没有给一个参数的路线,所以功能没有得到任何。

你可能想要做什么,或者是:

@get('/<chart>') 
def serve_static(chart): 
    ... 

如果你想/myfile.svg工作,或:

@get('/chart/<chart>') 
def serve_static(chart): 
    ... 

如果你想/chart/myfile.svg工作。

如果你只是想每次都显示相同的SVG文件,你可以离开关参数:

@get('/chart') 
def serve_static(): 
    ...