Everytime restart required for flask to work [duplicate] - python

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_)

Related

File uploads using flask looking in CWD for file to upload instead of using users sample directory

Basically my issue is, whenever I go to upload a file through Flask it wants the file that is being uploaded in the same directory as the Python file my flask server is being run from. This becomes an issue when I go to my local machine instead of my VM and it searches for /home/kali/Downloads/(sample name) instead of wherever the sample is on the windows machine ex(C:\temp(sample)). It also does this on the VM itself where it is only looking in the /home/kali/Downloads folder for the sample to be uploaded. It's almost like it skips the sample entirely.
Here is my code:
from flask import Flask, render_template, request, redirect, send_file
import os
import shutil
from flaskmalwarecheck import malwaresignature
from flaskmalwarecheck import formattedpdf
from flaskmalwarecheck import entropy
import argparse
from elastic import elasticupload
filetypes = [b'MZ']
app= Flask(__name__)
#app.route('/')
def main():
return render_template('attempt.html')
#app.route('/upload', methods = ['GET', 'POST'])
def upload():
try:
upload_folder = "/home/kali/Downloads/webserverup/"
if request.method == 'POST':
n = request.files['file']
filename = n.filename
with open(filename, 'rb') as f:
header = f.read(32)
for call in filetypes:
if call in header:
n.save(os.path.join(upload_folder,filename))
os.chdir(upload_folder)
malware_file, ISO8601, hashmethod, arch, importeddlls, imphash, fuzzyhash,warnings = malwaresignature(n.filename)
formattedpdf(n.filename,malware_file,ISO8601, hashmethod, arch, importeddlls, imphash, fuzzyhash,warnings)
download()
os.remove(n.filename)
os.chdir('..')
elasticupload()
return redirect('/download', code=302)
else:
return redirect('/download', code=302)
except FileNotFoundError as e:
return redirect('/download', code=302)
#app.route('/download')
def download():
return send_file('/home/kali/Downloads/webserverup/Sample.pdf', as_attachment=True)
#app.route('/transparent')
def transparent():
with app.open_resource('flaskmalwarecheck.py', 'r') as e:
contents = e.read()
return contents
parser = argparse.ArgumentParser()
parser.add_argument("ip", help="Enter host IP", type=str)
parser.add_argument("port", help="Port to be hosted on", type=int)
args = parser.parse_args()
if __name__ == "__main__":
app.run(host=args.ip, port=args.port, ssl_context=('cert.pem', 'key.pem'))
The code uploaded really wonky so if something is misplaced it most likely isnt in the actual code, but feel free to point it out anyways. If you also want to see the HTML page I can provide that, but I didn't think it was relevant. This is my first time working with Flask so any pointers would be greatly appreciated as well ;)
You might want to use the config variable UPLOAD_FOLDER to specify where the uploaded file goes & secure_filename to strip & sanitize the path
import os
from werkzeug.utils import secure_filename
UPLOAD_FOLDER = '/path/to/the/uploads'
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
#app.route(...<route info>...)
def upload_file():
...
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
...
See Flask documentation on Uploading Files

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.

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.

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

Python/Flask - Uploading a file by passing a path from a Jupyter Notebook

Context
I have created a Flask application that allows me to:
(1) upload a GeoTIFF file to a specified folder (UPLOAD_FOLDER)
(2) use GDAL to open the uploaded GeoTIFF as a Pandas data frame, and return a JSON containing all the cell values. The app code is below:
import os
import gdal
import numpy as np
import pandas as pd
import json
from flask import Flask, flash, request, redirect, url_for
from werkzeug.utils import secure_filename
UPLOAD_FOLDER = 'PATH_GOES_HERE' #specify path
ALLOWED_EXTENSIONS = set(['tif'])
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
#app.route('/upload', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
if file.filename == '':
flash('No selected file')
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return redirect(url_for('get_raster_data',
filename=filename))
return '''
<!doctype html>
<title>Upload raster file</title>
<h1>Upload raster file</h1>
<form method=post enctype=multipart/form-data>
<input type=file name=file>
<input type=submit value=Upload>
</form>
'''
#app.route('/rasterdata', methods=['GET'])
def get_raster_data():
filename = secure_filename(request.args.get('filename'))
try:
if filename and allowed_file(filename):
f = os.path.join(app.config['UPLOAD_FOLDER'], filename)
rast_data = gdal.Open(f)
rast_array = np.array(rast_data.GetRasterBand(1).ReadAsArray())
return pd.DataFrame(rast_array).to_json()
except IOError:
pass
return "Unable to read file"
The application works properly (i.e. I've tested using a local host and running in debug mode). The application allows me to open a web page with "Choose file" and "upload" buttons. Once I upload the file I am redirected to the '/rasterdata' page which has the expected output.
I have been tasked with creating a Jupyter Notebook that basically only requires users to specify the path to a GeoTIFF that they would like to upload. Once the path is specified the Flask app needs to run and return a data frame of all the GeoTIFF's cell values. From there, the Notebook goes through a few processing steps that require the data frame as the input, but these are not relevant to my question.
Question
How can I upload a file to UPLOAD_FOLDER by simply specifying the path to the GeoTIFF in the Jupyter Notebook? Below is the code from my Jupyter Notebook. I've added comments specifying where I am stuck. I suspect that I will need to modify the Flask app to take in a path name. I could not find any tutorials for this though. All the tutorials I could find give me code that is similar to what I currently have.
url = f'http://localhost:5000/upload'
my_path = r'C:\Users\admievan\Desktop\FlaskGDAL\my_raster.tif'
#Opening the upload page
with urllib.request.urlopen(path) as url:
#THIS IS WHERE I AM STUCK
#I want to pass my_path to the Flask application rather than having to
#manually navigate to the file in the GUI interface that comes up when clicking
#the "Choose file" button
#Reading the data web page as a panadas data frame
#This part works fine if 'my_raster.tif' is already in the UPLOAD_FOLDER
url = f'http://localhost:5000/rasterdata?filename=my_raster.tif'
df = pd.read_json(url, orient='rows')
df
The requests package is your best friend when it comes to dealing with uploads/extractions and API calls.
Whatever your host is for the url is where you would need to pass this through.
Uploading is not too difficult and could look something like this:
import os
import base64
import urllib
import json
import requests
def jupyter_upload(token, filePath, resourceDstPath, jupyterUrl='http://localhost:8888'):
dstPath = urllib.quote(resourceDstPath)
dstUrl = '%s/api/contents/%s' % (jupyterUrl, dstPath)
fileName = filePath[1 + filePath.rfind(os.sep):]
headers = {}
headers['Authorization'] = 'token '+token
with open(filePath, 'r') as myfile:
data=myfile.read()
b64data=base64.encodestring(data)
body = json.dumps({
'content':b64data,
'name': fileName,
'path': resourceDstPath,
'format': 'base64',
'type':'file'
})
return requests.put(dstUrl, data=body, headers=headers, verify=True)`

Categories

Resources