angularjs - How to send csv from angular to flask and load into pandas? -
i trying post csv file client server , load file pandas. error ioerror: expected file path name or file-like object, got type
i tried sending same file same url through postman , there no error. think there problem how angular sends file or how appended formdata.
app.py
from flask import flask, render_template, request, send_from_directory 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 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" }); });
the form in 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
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
Comments
Post a Comment