Accessing data and arrays from different function - python

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.

Related

how to use Pandas data frame in different callbacks using flask

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

Win32Com Python use one global Dispatcher instance to create multiple workbook objects in Flask

Heres the code of what im trying to implement
from flask import Flask, request, jsonify
import json
import pythoncom
import win32com.client
from win32com.client import Dispatch
xl = None
xl = win32com.client.Dispatch("Excel.Application")
app = Flask(__name__)
#app.route("/check", methods=['POST'])
def check():
pythoncom.CoInitialize()
if request.method == 'POST':
data = request.get_json()
fname = data['fname']
phName = data['PH_Name']
liName = data['LI_NAME']
ppm = data['PPM']
policyTerm = data['Policy_Term']
sumAssured = data['Sum_Assured']
wb = None
if xl :
wb = xl.Workbooks.Open(fname)
inp_sheet = wb.Sheets["Inputs"]
#read data from sheet
data = inp_sheet.Range('D8').Value
wb.Close(False)
xl.Quit()
return (data)
if __name__ == '__main__':
app.run(threaded=True, port=5001, debug=True)
The issue is that win32com creates new threads whenever a flask function is used to open a workbook and thus requires to marshal the Dispatcher object onto a stream to pass it to the function.
How do i do that for an api call ?
I found some suggestions using CoMarshalInterThreadInterfaceInStream and CoGetInterfaceAndReleaseStream but they fail to run when multiple parallel calls are made to the api.

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

Everytime restart required for flask to work [duplicate]

This question already has an answer here:
Refering to a directory in a Flask app doesn't work unless the path is absolute
(1 answer)
Closed 4 years ago.
I have a flask app which will read some dataframes and displays some output in front end. I have two routes. One will accept user input and is send to second route. Second route make use of this input and process some dataframe and provides some output. Issue is, if go to user input page again and if I try submitting another input, it gives me page not working error.
Is it because of any memory issue? If I restart my sever, then I repeat it once(every time I need to restart).
from flask import flash, redirect, render_template, url_for, request, jsonify
import math
import os
import glob
import pandas as pd
from . import fa
from database import connection
UPLOAD_DIRECTORY = './uploads'
if not os.path.exists(UPLOAD_DIRECTORY):
os.makedirs(UPLOAD_DIRECTORY)
#fa.route('/fc', methods=['GET', 'POST'])
def index():
return render_template('fc.html',flag=0)
#fa.route('/fc/s', methods=['GET', 'POST'])
def start():
if request.method == 'POST':
material_number = request.form['ma']
path = UPLOAD_DIRECTORY
extension = 'xlsx'
os.chdir(path)
result = [i for i in glob.glob('*.{}'.format(extension))]
allFiles = result
frame = pd.DataFrame()
list_ = []
for file_ in allFiles:
df = pd.read_excel(file_)
list_.append(df)
frame = pd.concat(list_)
frame = frame.reset_index(drop=True)
df1 = frame[frame['Ma'].str.contains(ma,regex=True)]
pr = df1['Pr'].unique().tolist()
pro = pd.read_excel(r'~pathhiddn~\dtrvsproj.xlsx')
return render_template('fc.html',flag=1,ma=ma,prs=pr)
return redirect(url_for('fa.index'))
os.chdir(path)
This was causing the issue. I removed that and replaced df = pd.read_excel(file_) with df = pd.read_excel('./uploads/data/'+file_)

Categories

Resources