如何将csv从角度发送到烧瓶并装入熊猫?

时间:2016-02-05 19:22:59

标签: angularjs pandas flask

我正在尝试将csv文件从我的客户端发布到我的服务器并将文件加载到pandas中。我收到错误IOError:预期的文件路径名或类文件对象,得到类型

我尝试通过邮递员将同一个文件发送到同一个网址,但没有错误。所以我认为角度发送文件或如何将其附加到FormData存在问题。

app.py

from flask import Flask, render_template, request, send_from_directory
from minimongo import Model, configure
import pandas
import csv

app = Flask(__name__)

configure(host="xx.xx.com", port=xx, username="xx", password="xx")

class Placement(Model):
    class Meta:
        database= "flask_api"
        collection = "placements"

@app.route('/')
def index():
    return render_template("index.html")

@app.route('/<path:path>')
def send_static(path):
    return send_from_directory('static', path)

@app.route('/receive_form', methods=['GET', 'POST'])
def receive_form():
    instance = Placement()
    instance.x = "test"
    instance.save()
    df = pandas.read_csv(request.files.get("csv"))
    return "200"

if __name__ == '__main__':
    app.run(debug=True)

app.js

angular.module("negatizer", ['ngRoute'])

.service("getJson", function($http){
  this.getJson = function(callback){
    $http.get("/mock/data.json").then(callback)
  }
})
.service("postFormData", function($http){
  this.postFormData = function(data, callback){
    console.log("service")
    $http.post("/receive_form", data).then(callback)
  }
})


.controller("mainCtrl", function($scope, $window, getJson, postFormData){
  $scope.fileChanged = function(element) {
    var form = new FormData()
    form.append("csv", element.files[0])
    postFormData.postFormData(form, function() {
      console.log("I sent the the form")
    })
  }

  getJson.getJson(function(response) {
    console.log(response.data)
    $scope.varName = response.data
  })
})


.config(function ($routeProvider) {
    $routeProvider
    .when("/", {
        controller: "mainCtrl",
        templateUrl: "partials/home.html"
    })
    .otherwise({
        redirectTo: "/mock/data.json"
    });
});

index.html中的表单

<form action="/" enctype="multipart/form-data" id="form" method="post">
  <span id="upload">
  Upload Automatic Placements<input ng-model="photo" onchange="angular.element(this).scope().fileChanged(this)" type="file" name="csv">
  </span>
</form>

回溯

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/Users/student/Documents/flask_projects/neg_app/app.py", line 28, in receive_form
    df = pandas.read_csv(request.form.get("csv"))
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/io/parsers.py", line 498, in parser_f
    return _read(filepath_or_buffer, kwds)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/io/parsers.py", line 275, in _read
    parser = TextFileReader(filepath_or_buffer, **kwds)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/io/parsers.py", line 590, in __init__
    self._make_engine(self.engine)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/io/parsers.py", line 731, in _make_engine
    self._engine = CParserWrapper(self.f, **self.options)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/io/parsers.py", line 1103, in __init__
    self._reader = _parser.TextReader(src, **kwds)
  File "pandas/parser.pyx", line 353, in pandas.parser.TextReader.__cinit__ (pandas/parser.c:3246)

  File "pandas/parser.pyx", line 608, in pandas.parser.TextReader._setup_parser_source (pandas/parser.c:6288)

IOError: Expected file path name or file-like object, got <type 'NoneType'> type

0 个答案:

没有答案