how to use Pandas data frame in different callbacks using flask - python

I am working on flask, first #application.route used for uploading csv file and processed some calculations using pandas data frame and I want to return processed data frame as excel file in second call back with download button how to do that.
used this code for uploading file
#application.route('/chart2', methods=['GET', 'POST'])
def upload():
if request.method == 'POST':
df = pd.read_csv(request.files.get('file'))
some pandas caliculations
return render_template('index.html')
how to convert processed data frame as excel/csv by using download button in flask

Related

Download file from server location, using send_from_directory and filename

I'm struggling to understand how to pass an uploaded filename to the next #app.route in flask to retrieve a file saved on the server.
I have been able to create a form to upload the file to the server like this, and have been able to render the file data and the filename to the proceeding view.
#app.route('/file_upload', methods=['GET', 'POST'])
def upload():
form = UploadForm()
if form.validate_on_submit():
f=form.file.data
filename = secure_filename(f.filename)
file_path = os.path.join(UPLOAD_FOLDER, filename)
f.save(file_path)
Input_data_dataframe = pd.read_excel(f)
return render_template('Set_parameters.html',filename=filename,tables=[Input_data_dataframe.to_html(classes=["table table-dark table-hover"], header="true", index=False,)])
return render_template('file_upload.html',form=form)
The file successfully saves at the required server destination, and the filename is also rendered in the next view like so:
I now want this file to be retrieved from the server location for further processing. I understand that I can use the send_from_directory() function to retrieve the file, however I am not sure how to implement it with the correct filename from the previous #app.route('/file_upload')
#app.route('/Set_Parameters/', methods=['GET', 'POST'])
def processing(filename):
retrievedfile= send_from_directory(app.config['UPLOAD_FOLDER'],filename=filename, as_attachment=False)
Input_data_dataframe = pd.read_excel(retrievedfile) #converts uploaded file to a dataframe astype(int)
if request.method == 'POST':
Input_value1= request.form.get['Input_value1'] #get html form data value
Input_value= request.form.get['Input_value2'] #get html form data value
return render_template('Results.html',Input_value1=Input_value1, Input_value2=Input_value2, tables=[Input_data_dataframe.to_html(classes=["table table-dark table-hover"], header="true", index=False,)])
return render_template('Set_parameters.html')
Currently if I was to run as is, I would get this error
TypeError: processing() missing 1 required positional argument: 'filename' as I can understand I haven't passed the filename to the function.. I am just unsure how to do this without hardcoding the filename into the code, and then to use the same file for further processing.
Any help would be greatly appreciated.
Your processing function expects filename parameter from URL. You can see how it should be done here: https://flask.palletsprojects.com/en/1.1.x/quickstart/#variable-rules.
Route to this view should be: /Set_Parameters/<filename>/. It means that action attribute of the from should be, for example: action="/Set_Parameters/This_is_the_file.xlsx".
If you render that form in flask, using flask templates, it should be easy if you have a filename in a render context:
<form action="/Set_Parameters/{{ filename }}">
...
</form>

Accessing data and arrays from different function

I am creating a web application through flask where I want to upload an Excel file take all the data in the Excel file and store it in array, or dictionary for data processing in a different function. I am able to read the Excel file and store it in an array, but I was wondering if there's a way to access that data from a different function for processing. If that's possible a pseudo code would help.
from flask_restful import Resource, Api
import openpyxl
from openpyxl import load_workbook
import pandas as pd
import xlrd, xlwt
app = Flask(__name__)
api = Api(app)
seller_info = []
#app.route('/', methods=["GET", "POST"])
def main():
if request.method == "POST":
if request.files:
excel_file = request.files['excel_file']
wb = pd.ExcelFile(excel_file)
excel_data = pd.read_excel(wb, "Summary", header=None, nrows=5)
global seller_info
for row in excel_data.iterrows():
seller_info.append(row[1][1])
return render_template('main.html', seller_info=seller_info)
return render_template('main.html')
#app.route('/process', methods=["GET", "POST"])
def process():
return '??'
if __name__ == '__main__':
app.run(debug=True)
I want to be able to pull the data from seller_info list and use it later in the process method. I'm not sure how to do this.

is there any way of continously reading CSV file and show it on HTML using Flask?

I have written a small job that takes the data from CSV and show it on HTML using Flask. It works fine but I want to automate it in a way that it starts showing any change in CSV. The CSV file is getting data from live system which means that the file content is changing with time. From my code it only shows me only show me present file and does not update for the newer content.
I have tried running bat file that will start python script but the problem is once it is started, I need to start python again so that it takes latest CSV.
from flask import Flask,render_template
import tablib
app = Flask (__name__)
dataset = tablib.Dataset()
with open(os.path.join(os.path.dirname(__file__),'file.csv')) as f:
dataset.csv = f.read()
#app.route("/")
def index():
return dataset.html
if __name__ == "__main__":
app.run()
You need to read the file within your index function.
#app.route("/")
def index():
dataset = tablib.Dataset()
with open(os.path.join(os.path.dirname(__file__),'file.csv')) as f:
dataset.csv = f.read()
return dataset.html

How to load and read excel file in python flask form post api

Hi I want to upload a excel file form fronted and in back end i want to read all contents of that Excel File and store that in my database
#app.route('/getfile', methods=['POST'])
def getfile():
try:
file = request.files['file']
foo=file.filename
dframe = pd.read_excel(foo)
return dframe
except Exception as ex:
return ex
i am getting filename and from panda i want to read that excel file data but its showing no such directory exists
from flask import Flask, request, jsonify
import flask_excel as excel
app = Flask(__name__)
import json
import xlrd
import os
import pandas as pd
from collections import OrderedDict
from pathlib import Path
from flask_cors import CORS,cross_origin
from json import loads
import pandas as pd
wsgi_app = app.wsgi_app
excel.init_excel(app)
code 1
#app.route("/upload", methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
foo = request.get_array(field_name='file')
# data = json.dumps(foo, )
# for i in data[1:]:
return jsonify({"result":foo })
#code 2
#app.route('/getfile', methods=['POST'])
def getfile():
try:
file = request.files['file']
foo=file.filename
dframe = pd.read_excel(foo)
return dframe
except Exception as ex:
return ex
1st code giving me 404 exception and 2nd code giving me "No such Directory exits"
can anybody help me out in this reading the content of excel file
from flask import Flask, request, jsonify
from tablib import Dataset
app = Flask(__name__)
#app.route('/upload', methods=['POST'])
def upload_file():
# I used form data type which means there is a
# "Content-Type: application/x-www-form-urlencoded"
# header in my request
raw_data = request.files['myfile'].read() # In form data, I used "myfile" as key.
dataset = Dataset().load(raw_data)
return jsonify(dataset.export('json'))
if __name__ == '__main__':
app.run(debug=True)
This little snippet is working just fine. You don't need to use a huge data analyzing library to import something to database. But if you insist using pandas, your question needs another answer.
Check out the documentation before doing anything else:
http://flask.pocoo.org/docs/1.0/patterns/fileuploads/
http://docs.python-tablib.org/en/master/tutorial/#importing-data
Try this buddy, you need to read the file first (foo.read()). That way you'll get the un-parsed file, then you need to read this un-parsed file using pandas, like as follows:
#app.route('/getfile', methods=['POST'])
def getfile():
try:
file = request.files['file']
foo=file.filename
unparsedFile = foo.read()
dframe = pd.read_excel(unparsedFile)
return dframe
except Exception as ex:
return ex

Using Flask to load a txt file through the browser and access its data for processing

I am making a data visualization tool that takes input from the user (choosing a file on the computer); processes it in Python with Pandas, Numpy, etc; and displays the data in the browser on a local server.
I am having trouble accessing the data once the file is selected using an HTML input form.
HTML form:
<form action="getfile" method="POST" enctype="multipart/form-data">
Project file path: <input type="file" name="myfile"><br>
<input type="submit" value="Submit">
</form>
Flask routing:
#app.route("/")
def index():
return render_template('index.html')
#app.route('/getfile', methods=['GET','POST'])
def getfile():
if request.method == 'POST':
result = request.form['myfile']
else:
result = request.args.get['myfile']
return result
This returns a "Bad Request The browser (or proxy) sent a request that this server could not understand." error. I have tried a number of different ways of getting the data out of the file and simply printing it to the screen to start, and have received a range of errors including "TypeError: 'FileStorage' object is not callable" and "ImmutableMultiDict' object is not callable". Any pointers on how to approach this task correctly are appreciated.
Try this. I've been working on saving and unzipping files for the last few days. If you have any trouble with this code, let me know :)
I'd suggest saving the file on disk and then reading it. If you don't want to do that, you needn't.
from flask import Flask, render_template, request
from werkzeug import secure_filename
#app.route('/getfile', methods=['GET','POST'])
def getfile():
if request.method == 'POST':
# for secure filenames. Read the documentation.
file = request.files['myfile']
filename = secure_filename(file.filename)
# os.path.join is used so that paths work in every operating system
file.save(os.path.join("wherever","you","want",filename))
# You should use os.path.join here too.
with open("wherever/you/want/filename") as f:
file_content = f.read()
return file_content
else:
result = request.args.get['myfile']
return result
And as zvone suggested in the comments, I too would advise against using GET to upload files.
Uploading files
os.path by Effbot
Edit:-
You don't want to save the file.
Uploaded files are stored in memory or at a temporary location on the filesystem. You can access those files by looking at the files attribute on the request object. Each uploaded file is stored in that dictionary. It behaves just like a standard Python file object, but it also has a save() method that allows you to store that file on the filesystem of the server.
I got this from the Flask documentation. Since it's a Python file you can directly use file.read() on it without file.save().
Also if you need to save it for sometime and then delete it later, you can use os.path.remove to delete the file after saving it. Deleting a file in Python
A input type=file data isn't passed in as the form dictionary of a request object. It is passed in as request.files (files dictionary in the request object).
So simply change:
result = request.form['myfile']
to
result = request.files['myfile']

Categories

Resources