小编Cod*_*nja的帖子

从Flask视图返回JSON响应

我有一个功能,用Pandas分析CSV文件,并生成一个包含摘要信息的dict.我想将结果作为Flask视图的响应返回.如何返回JSON响应?

@app.route("/summary")
def summary():
    d = make_summary()
    # send it back as json
Run Code Online (Sandbox Code Playgroud)

python json flask

418
推荐指数
11
解决办法
42万
查看次数

在dthon中将dict转换为已排序的dict

我想在dthon中将dict转换为已排序的dict

data = pandas.read_csv('D:\myfile.csv')
for colname, dtype in data.dtypes.to_dict().iteritems():
    if dtype == 'object':
        print colname
        count = data[colname].value_counts()
        d = dict((str(k), int(v)) for k, v in count.iteritems())
        f = dict(sorted(d.iteritems(), key=lambda item: item[1], reverse = True)[:5])
        print f

        m ={}
        m["count"]= int(sum(count))    
        m["Top 5"]= f    
        print m    
        k = json.dumps(m)
        print k    
f = {'Gears of war 3': 6, 'Batman': 5, 'gears of war 3': 4, 'Rocksmith': 5, 'Madden': 3}
Run Code Online (Sandbox Code Playgroud)

我想要的输出是:

f = {'Gears of war 3': 6, 'Batman': 5, …
Run Code Online (Sandbox Code Playgroud)

python sorteddictionary pandas

9
推荐指数
1
解决办法
1万
查看次数

将参数值添加到flask python中的url

我有以下网址的代码:http://localhost/summary/myfile.csv我希望网址看起来像这样:http:// localhost/summary?file = myfile.csv

代码将写在烧瓶中.

我的第一个网址的代码如下:

@app.route('/summary/<filename>',methods = ['GET'])
def api_summary(filename):
    url = 'C:\\Users\\Desktop\\myproject\\'
    if os.path.exists(url + filename):
        data = pandas.read_csv( url + filename)
        Numeric_Summary = data.describe().to_dict()
        resp = jsonify(Numeric_Summary)
        resp.status_code = 200
        return resp
Run Code Online (Sandbox Code Playgroud)

python urlparse flask

0
推荐指数
1
解决办法
5003
查看次数

标签 统计

python ×3

flask ×2

json ×1

pandas ×1

sorteddictionary ×1

urlparse ×1