pass parameters from Flask main - python

I'm new at Flask. I try to pass a parameter from the main, but it doesn't work. Hope somebody can help me. Here is my code
from flask import Flask, render_template, request
import controllFlask
import pickle
app = Flask(__name__) # creates a flask object
import subprocess
#app.route('/', methods=['GET', 'POST'])
def ner():
'''controlls input of Webapp and calls proccessing methods'''
knownEntities = pickle.load( open( "entityDictThis.p", "rb" ))
print("loades")
content = ""
if request.method == 'POST':
testText = (request.form['input'])
app.ma = controllFlask.controll(testText, knownEntities)
subprocess.call("controllFlask.py", shell=True)
with open("test.txt", "r") as f:
content = f.read()
return render_template("ner.jinja2", content=content)
def flaskApp():
app.debug = True
app.run()
I want to open entityDictThis in flaskApp and give it to the ner-function. Because I hope in this way it loads only one time. At the moment it loads every time the page is reloaded and it takes very long. Is there a easy way?

This seems to be only a scoping problem, simply put the line that loads the pickle file in the scope above and it should solve the issue.
from flask import Flask, render_template, request
import controllFlask
import pickle
app = Flask(__name__) # creates a flask object
import subprocess
knownEntities = pickle.load( open( "entityDictThis.p", "rb" ))
#app.route('/', methods=['GET', 'POST'])
def ner():
'''controlls input of Webapp and calls proccessing methods'''
print("loades")
content = ""
if request.method == 'POST':
testText = (request.form['input'])
app.ma = controllFlask.controll(testText, knownEntities)
subprocess.call("controllFlask.py", shell=True)
with open("test.txt", "r") as f:
content = f.read()
return render_template("ner.jinja2", content=content)
def flaskApp():
app.debug = True
app.run()
I would also suggest, as #bouteillebleu mentioned, to close the loaded file, using the with keyword, which does this automagically for you.
with open( "entityDictThis.p", "rb" ) as f:
knownEntities = pickle.load(f)

Related

Unable to delete uploaded file Flask

I am fairly inexperienced in this topic. I am working on Windows. I am doing an application in which I need to upload a file, get its signatures and use for some purposes. At the instant I extract the signature, I want to delete the file that was uploaded. I am using Flask in backend. When I try to delete the file using os.remove() or pathlib.unlink, it gives me permission error
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'uploads\\file.exe'
This is app.py file
from flask import Flask, render_template, request, jsonify
import pefile
import os
from werkzeug.utils import secure_filename
from werkzeug.datastructures import FileStorage
from extracter import extract_infos
import pathlib
ALLOWED_EXTENSIONS = {'exe'}
app = Flask(__name__)
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
#app.route('/')
def index():
return render_template('index.html')
#app.route('/upload', methods = ['POST'])
def uploadFile():
if request.method == 'POST':
if 'file' not in request.files:
return render_template('page.html')
f = request.files['file']
if f.filename == '':
return render_template('page.html')
if f and allowed_file(f.filename):
fileLink = secure_filename(f.filename)
p = f.read()
with open('uploads/file.exe', 'wb') as w:
w.write(p)
w.close()
pe = extract_infos('uploads/file.exe')
f.close()
file_to_rem = pathlib.Path("uploads/file.exe")
file_to_rem.unlink()
#delete_link = app.config['UPLOAD_FOLDER'] + '\\' + fileLink
#os.remove(delete_link)
return jsonify(pe)
if __name__ == '__main__':
app.run(debug = True)
The error I get is
And the file gets uploaded in the uploads folder but it is not deleted
I did do some research and realized that this kind of error occurs usually when you forget to close a stream once you open it to read. But I don't understand where this could be happening in my code. I need help

Handle multiple request at same time on Flask web app

What I have: I've a Flask web app deployed to Heroku's server, which consists of only one web process app.py. Here it is:
#importation
from flask import Flask, render_template, current_app, send_file, request, json, send_file
import os
#working functions
#json write
def json_write(dictionary):
with open("./json/info.json", "w+") as f:
json.dump(dictionary, f, indent=4)
#make file name
def make_file_name(name):
filename = "tube4u_"
for t in str(name):
if t.isalnum():
filename += t
filename += ".mp4"
return filename
#application initialisation
app=Flask(__name__)
#home
#app.route("/")
def home():
return render_template("index.html")
#processor
#app.route("/process/", methods=["GET"])
def process():
#get url
url = request.args["url"]
#import & initialisation
from pytube import YouTube
import pickle
json_dict = {}
try:
yt = YouTube(url)
except:
return "<h1>Invalid URL</h1>"
all_videos = yt.streams.filter(type="video", progressive=True)
json_dict["title"] = yt.title
json_dict["thumbnail"] = yt.thumbnail_url
json_dict["name"] = make_file_name(yt.title)
with open("./pickle/ytobj.pkl", "wb") as f:
pickle.dump(all_videos, f)
#videos with itag
json_dict["videos"] = [ {"itag": item.itag, "res": item.resolution} for item in all_videos]
json_write(json_dict)
return render_template("menu.html")
#download
#app.route("/download/", methods=["GET"])
def download():
import pickle
itag = int(request.args["itag"])
with open("./json/info.json") as f:
json_dict = json.load(f)
with open("./pickle/ytobj.pkl", "rb") as f:
all_videos = pickle.load(f)
video = all_videos.get_by_itag(itag)
video.download(output_path="./video", filename=f"{json_dict['name']}")
return render_template("thank.html")
#return video
#app.route("/video/", methods=["GET"])
def video():
filename = request.args["filename"]
return send_file(f"./video/{filename}", as_attachment=True)
#return json
#app.route("/json")
def fetchjson():
with open("./json/info.json") as f:
content = json.load(f)
return content
#get name
#app.route("/name")
def fetchname():
with open("./json/info.json") as f:
content = json.load(f)
return content
#app.route("/list")
def listall():
return f"{os.listdir('./video')}"
#running the app
if __name__ == "__main__":
app.run(debug=True)
How it works: here I made the app like that, whenever someone enter a URL and click Go then it creates a json file with the name info.json. after it gets everything properly it performs some task with the given URL reading from the file.
My problem:
Now the problem is, if I make a request of the web it will create a json with my given URL, suppose at the same time someone else make a request and enter a URL then server will lost my information and rewrite the json file with another client's given input URL my task will be performed with another's input url. It's really weird.
How to fix it? Like if there any way to create the info.json file on separate path for each client and gets deleted after work done?
There is a lot of ways in my point of view
When the server get client request then check if there is already a file.if there is already a file then add timestamp or add something else in the filename so the file will not be overwritten.
Ask the user file name and also add timestamp in the name and save it.
You can also use databases to store data of different clients .may be you can create login system and give every user an id and store data for every user in database accordingly.
So on...
You can see there is a lot of ways to solve this.

Uploading xml file and processing using flask

I am developing a Python backend to which I send an xml file from the front end. This is so that I can generate python code based on it and show the contents in the front end. How can I do this using flask?
I have attached the code I tried below. It does not work for me. I was not able to save the xml file into a directory.
from flask import Flask, request, render_template
app = Flask(__name__, template_folder='templates')
from main import run
import os
#app.route('/')
def home():
return render_template('home.html')
#app.route('/submit/', methods=['POST'])
def upload():
if request.method == 'POST':
uploaded_file = xmltodict.parse(request.get_data())
file = os.path.join(app.config['upload'].uploaded_file.filename)
uploaded_file.save(file)
return "Successfully uploaded"
#app.route('/submit/')
def convert():
path='upload'
os.chdir(path)
for file in os.listdir():
if file.endswith(".py"):
file_path = f"{path}\{file}"
run(file_path,'tmp','python')
return "Code generated"
#app.route('/view/')
def view_python_script():
# Folder path
path='tmp'
os.chdir(path)
content=""
for file in os.listdir():
if file.endswith(".py"):
file_path = f"{path}\{file}"
with open(file_path, "r") as f:
content = content + f.read().replace('\n','<br>')
return render_template('upload.html', details=content)
if __name__ == "__main__":
app.run(port=3000, debug=True)
I occupy this: uploaded_file = request.files ['file_upload'].
file_upload I pass it from the html with the parameter name = "file_upload" of input contained within the form.
The problem I have is that when I want to share it in another html page it closes and throws me a ValueError: I / O operation on closed file.
But well, I hope it helps you !!!

requests.content works normally but saves corrupt pdf file when used inside app.route flask

I am trying to download a pdf from the link :
https://ptenantectdtest.blob.core.windows.net/documentcontainer/fae488ce-514d-4367-be48-610b19193e10?sv=2015-12-11&sr=b&sig=It1gKsb%2BHmQwjqxAprbAROySOKAdd2qyFnW%2FoBi0uM0%3D&st=2019-07-18T18%3A20%3A05Z&se=2019-07-19T18%3A30%3A05Z&sp=r&rscd=attachment%3B%20filename%3D%20%228d4508bf-453e-45fd-8457-8fd158152ba7.pdf%22
When I use requests.content inside a python function, it works well and downloads the pdf but when I use the same inside app.route, it saves a corrupt pdf file.
Code for normal python function using requests:
def download_url(url):
r = requests.get(url)
with open('D:/file_.pdf', 'wb') as f:
f.write(r.content)
categories = convert('D:/file_.pdf')
return categories
Code for downloading from app.route:
import requests
import ectd
from ectd import convert
from flask import Flask
from flask_restful import Resource, Api
app = Flask(__name__)
api = Api(app)
class ectdtext(Resource):
def get(self, result):
return {'data': ectd.convert(result)}
#app.route('/', defaults={'path': ''})
#app.route('/<path:path>')
def get_dir(path):
r = requests.get(path)
with open('D:/3file_.pdf', 'wb') as f:
f.write(r.content)
categories = convert('D:/3file_.pdf')
return categories
if __name__ == '__main__':
app.run()

Generating word docs with Flask?

I'm trying to spin up a single page flask application that allows users to download a word document. I've already figured out how to make/save the document using python-docx, but now I need to make the document available in the response. Any ideas?
Here's what I have so far:
from flask import Flask, render_template
from docx import Document
from cStringIO import StringIO
#app.route('/')
def index():
document = Document()
document.add_heading("Sample Press Release", 0)
f = StringIO()
document.save(f)
length = f.tell()
f.seek(0)
return render_template('index.html')
instead of render_template('index.html') you can just:
from flask import Flask, render_template, send_file
from docx import Document
from cStringIO import StringIO
#app.route('/')
def index():
document = Document()
document.add_heading("Sample Press Release", 0)
f = StringIO()
document.save(f)
length = f.tell()
f.seek(0)
return send_file(f, as_attachment=True, attachment_filename='report.doc')
You could use the send_from_directory as in this answer.
If you are sending text, you could also use the make_response helper as in this answer.
Use
return Response(generate(), mimetype='text/docx')
Generate() should be replaced with f in your case
For more information look at streaming in flask
http://flask.pocoo.org/docs/1.0/patterns/streaming/
For those how pass after me...
referring to these two links:
python 3.x ImportError: No module named 'cStringIO' [StackOverFlow Question]
TypeError: string argument expected, got 'bytes' [GitHub issue]
io.StringIO now replaces cStringIO.StringIO
also it will raise an error
as document.save(f) should receive a pass or binary file
code should be like this:
from flask import Flask, render_template, send_file
from docx import Document
from io import BytesIO
#app.route('/')
def index():
document = Document()
f = BytesIO()
# do staff with document
document.save(f)
f.seek(0)
return send_file(
f,
as_attachment=True,
attachment_filename='report.docx'
)

Categories