bottle.py呈现静态文件

时间:2014-12-02 16:20:47

标签: python bottle

我正在构建一个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) 

1 个答案:

答案 0 :(得分: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():
    ...
相关问题