this my code:
import os
import pandas as pd
from flask import Flask, request,render_template, redirect, url_for, send_from_directory
from werkzeug.utils import secure_filename
# create app
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = '/home/Firiyuu77/mysite/uploads'
app.config['ALLOWED_EXTENSIONS'] = set(['txt','csv','xlsx'])
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1] in app.config['ALLOWED_EXTENSIONS']
@app.route('/')
def main():
return render_template('index.html')
# Route that will process the file upload
@app.route('/upload', methods=['POST'])
def upload():
# Get the name of the uploaded file
file = request.files['file']
# Check if the file is one of the allowed types/extensions
if file and allowed_file(file.filename):
# Make the filename safe, remove unsupported chars
filename = secure_filename(file.filename)
# Move the file form the temporal folder to
# the upload folder we setup
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
# Redirect the user to the uploaded_file route, which
# will basicaly show on the browser the uploaded file
return redirect(url_for('uploaded_file',
filename=filename))
# This route is expecting a parameter containing the name
# of a file. Then it will locate that file on the upload
# directory and show it on the browser, so if the user uploads
# an csv , that csv is going to be returned after the eupload then evaluation.
@app.route('/uploads/<filename>', methods=['GET', 'POST'])
def uploaded_file(filename):
if __name__ == '__main__':
app.run(
host="0.0.0.0",
port=int("80"),
debug=True
)
if request.method == 'GET':
# show html form
return render_template('formcsv.html')
elif request.method == 'POST':
# calculate result
data_df = pd.read_csv(filename)
data_df['Forecasted Values:']=0
m1 = int(request.form.get('m1'))
m2 = int(request.form.get('m2'))
for i, row in data_df.iterrows() :
rem = data_df.iloc[i]['Current SOH']
sold1 = data_df.iloc[i][m1]
sold2 = data_df.iloc[i][m2]
rem = int(rem)
sold1 = int(sold1)
sold2 = int(sold2)
result = forecast(rem,sold1,sold2)
data_df.set_value([i], ['Forecasted Values:'], result)
data_df.to_csv(filename)
return send_from_directory(app.config['UPLOAD_FOLDER'],filename)
It's actually an app that gets a csv upload, manipulates it via pandas and returns it to the user. But somehow I got stuck here when I got this traceback, no errors on the console, just on the website error log.
2017-04-18 15:33:59,759 :Traceback (most recent call last):
2017-04-18 15:33:59,760 : File "/home/Firiyuu77/.virtualenvs/my-virtualenv/local/lib/python2.7/site-packages/flask/app.py", line 1982, in wsgi_app
2017-04-18 15:33:59,760 : response = self.full_dispatch_request()
2017-04-18 15:33:59,760 : File "/home/Firiyuu77/.virtualenvs/my-virtualenv/local/lib/python2.7/site-packages/flask/app.py", line 1614, in full_dispatch_request
2017-04-18 15:33:59,760 : rv = self.handle_user_exception(e)
2017-04-18 15:33:59,760 : File "/home/Firiyuu77/.virtualenvs/my-virtualenv/local/lib/python2.7/site-packages/flask/app.py", line 1517, in handle_user_exception
2017-04-18 15:33:59,760 : reraise(exc_type, exc_value, tb)
2017-04-18 15:33:59,760 : File "/home/Firiyuu77/.virtualenvs/my-virtualenv/local/lib/python2.7/site-packages/flask/app.py", line 1612, in full_dispatch_request
2017-04-18 15:33:59,760 : rv = self.dispatch_request()
2017-04-18 15:33:59,760 : File "/home/Firiyuu77/.virtualenvs/my-virtualenv/local/lib/python2.7/site-packages/flask/app.py", line 1598, in dispatch_request
2017-04-18 15:33:59,760 : return self.view_functions[rule.endpoint](**req.view_args)
2017-04-18 15:33:59,760 : File "/home/Firiyuu77/mysite/flask_app.py", line 61, in uploaded_file
2017-04-18 15:33:59,760 : data_df = pd.read_csv(filename)
2017-04-18 15:33:59,760 : File "/home/Firiyuu77/.virtualenvs/my-virtualenv/local/lib/python2.7/site-packages/pandas/io/parsers.py", line 646, in parser_f
2017-04-18 15:33:59,760 : return _read(filepath_or_buffer, kwds)
2017-04-18 15:33:59,761 : File "/home/Firiyuu77/.virtualenvs/my-virtualenv/local/lib/python2.7/site-packages/pandas/io/parsers.py", line 389, in _read
2017-04-18 15:33:59,761 : parser = TextFileReader(filepath_or_buffer, **kwds)
2017-04-18 15:33:59,761 : File "/home/Firiyuu77/.virtualenvs/my-virtualenv/local/lib/python2.7/site-packages/pandas/io/parsers.py", line 730, in __init__
2017-04-18 15:33:59,761 : self._make_engine(self.engine)
2017-04-18 15:33:59,761 : File "/home/Firiyuu77/.virtualenvs/my-virtualenv/local/lib/python2.7/site-packages/pandas/io/parsers.py", line 923, in _make_engine
2017-04-18 15:33:59,761 : self._engine = CParserWrapper(self.f, **self.options)
2017-04-18 15:33:59,761 : File "/home/Firiyuu77/.virtualenvs/my-virtualenv/local/lib/python2.7/site-packages/pandas/io/parsers.py", line 1390, in __init__
2017-04-18 15:33:59,761 : self._reader = _parser.TextReader(src, **kwds)
2017-04-18 15:33:59,761 : File "pandas/parser.pyx", line 373, in pandas.parser.TextReader.__cinit__ (pandas/parser.c:4184)
2017-04-18 15:33:59,761 : File "pandas/parser.pyx", line 667, in pandas.parser.TextReader._setup_parser_source (pandas/parser.c:8449)
2017-04-18 15:33:59,761 :IOError: File p2c-inventory-performance-20170219173023.csv does not exist
Why does the file does not exist when it states the correct name of the file at the traceback? So it shouldve have read it. ANy ideas why it was not found? I searched for it on the uploads folder and its there, but havent been evaluated which means, from the reading of the filename it wasn't found. Any ideas how I should calibrate it to make this work? I searched some solutions but they were for files with filenames stated and they just added "r" to it, , but on mine I'm using the variable for it -> filename
but when I added "r" by doing "r" + filename
it still doesnt work, sorry to be such a blockhead, but seriously im struggling with this. Need help
You're using a relative path and the working directory is different to what you expect. Use an absolute path or ensure that the relative path is rooted in the correct working directory.