2016-05-19 8 views
1

私はPandasとFlaskには新しく、定期的にユーザーに電子メールで送ることができるCSVの要約バージョンを出力することを試みています。Python Flask - Pandasにファイルをアップロード

指定された入力ファイル(例: 'users/sample.csv')と出力ファイルを与える限り、スタンドアロンの機能ですが、アプリケーションの一部として実行され、アップロードされたhtmlファイルを使用すると、 TypeError例外で失敗します。csuppfb()は、少なくとも2つの引数(0が与えられた)

は基本的に私は、関数にアップロードされたファイルを渡したい、とパンダを持ってそのことを行うが、それはそこまで取得していませんかかります。 ?

import re,os 
import beatbox 
import pandas as pd 
import numpy as np 
import argparse 
from jinja2 import Environment, FileSystemLoader 
from weasyprint import HTML 
from os.path import isfile,join 
from flask import Flask, request, redirect, url_for,render_template,json as fjson,send_from_directory 
from werkzeug import secure_filename 
from mapping import Autotagging,Manualtagging 
from defs import * 

UPLOAD_FOLDER = './uploads' 
PIVOT_FOLDER = './pivot' 
ALLOWED_EXTENSIONS = set(['csv']) 

app = Flask(__name__) 
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER 
app.config['PIVOT_FOLDER']= PIVOT_FOLDER 

@app.route('/feedback',methods=['GET', 'POST']) 
def feedback(): 
    if request.method == 'POST': 
     file = request.files['file'] 
     if file and allowed_file(file.filename): 
      filename = randomword(6)+'_'+secure_filename(file.filename) 
      file.save(os.path.join(app.config['PIVOT_FOLDER'], filename)) 
      return redirect(url_for('csuppfb',df=filename)) 

    return render_template('mappingtest.html') 

@app.route('/csuppfb', methods=['POST','GET']) 
def csuppfb(df,infile, index_list=["Case Owner","Case Number","Support Survey - Service rating"], value_list = ["Age (Hours)"]): 
    """ 
    Creating a pivot table from the raw dataframe and returning it as a dataframe 
    """ 
    table = pd.pivot_table(df, index=index_list, values = value_list, 
        aggfunc=[np.sum,np.mean], fill_value=0) 
    return table 

def get_summary_stats(df, product): 
    """ 
    Get a stats summary 
    """ 
    results.append(df[df["Support Survey - Service rating"]==product]["Closed"].mean()) 
    results.append(df[df["Support Survey - Service rating"]==product]["Age (Hours)"].mean()) 
    return results 

def dataform(df): 
    """ 
    Take the dataframe and output it in html to output a pdf report or display on a web page 
    """ 
    df = pd.read_csv(filename) 
    csuppreport = pivot_table(df,filename) 
    agent_df = [] 
    for agent in csuppreport.index.get_level_values(0).unique(): 
     agent_df.append([agent, csuppreport.xs(agent, level=0).to_html()]) 
    env = Environment(loader=FileSystemLoader('.')) 
    template = env.get_template("csupp.html") 


template_vars={"title": "CSUPP FB REPORT", 
      "Excellent": get_summary_stats(df,"Excellent"), 
      "Good": get_summary_stats(df,"Good"), 
      "csupp_pivot_table": csuppreport.to_html(), 
      "agent_detail": agent_df} 

html_out = template.render(template_vars) 
HTML(string=html_out).write_pdf(args.outfile.name,stylesheets=["style.css"]) 
return render_template('csupp.html') 

DEF csuppfb私は でデータフレームの引数として使用することがアップロードしたファイルを持っているための最良の方法は何(DF、INFILE ...

何かアドバイスします:以下のコードです非常に高く評価され。私はそれが私が欠けている紛れも明らかに何かを感じてきました。

答えて

0

あなたは引数を使用する必要が要求からオブジェクトのparamsすべてのURLが含まれている http://flask.pocoo.org/docs/0.10/quickstart/#the-request-object

この基本的な例を参照してください:

@app.route('/csuppfb', methods=['POST','GET']) 
    def csuppfb(): 
     if request.args['df'] : 
      df = request.args['df'] 
      #Do your panda stuff here. Instead of returning the filename :D 
      return str(df) 
     else : 
      return 'nofile' 
関連する問題