Flask Admin Ckeditor Image Upload - python

For Flask Admin How is it possible to upload images in Text area with editor ?
I need simpler version than this:
https://github.com/greyli/flask-ckeditor

from flask_ckeditor import *
csrf = CSRFProtect(app)
csrf.init_app(app)
ckeditor = CKEditor(app)
app.config['CKEDITOR_SERVE_LOCAL'] = False
app.config['CKEDITOR_HEIGHT'] = 400
app.config['CKEDITOR_FILE_UPLOADER'] = 'upload'
app.config['UPLOADED_PATH'] = os.path.join(basedir, 'uploads')
#app.route('/files/<filename>')
#csrf.exempt
def uploaded_files(filename):
path = app.config['UPLOADED_PATH']
return send_from_directory(path, filename)
import uuid
#app.route('/upload', methods=['POST'])
#csrf.exempt
def upload():
f = request.files.get('upload')
extension = f.filename.split('.')[1].lower()
if extension not in ['jpg', 'gif', 'png', 'jpeg']:
return upload_fail(message='Image only!')
unique_filename = str(uuid.uuid4())
f.filename = unique_filename + '.' + extension
f.save(os.path.join(app.config['UPLOADED_PATH'], f.filename))
url = url_for('uploaded_files', filename=f.filename)
return upload_success(url=url)
in edit.html in flask admin
<script>
CKEDITOR.plugins.addExternal( 'filebrowser', '/static/ckeditor/filebrowser/', 'plugin.js' );
CKEDITOR.config.extraPlugins = 'filebrowser';
CKEDITOR.config.filebrowserBrowseUrl = '/upload';
<script>
This works for flask admin to upload images
https://github.com/greyli/flask-ckeditor I used this

Related

Flask User Interface not redirecting to prediction layout

I'm a beginner in ML, I'm working with some research paper posted on github listed below:
https://github.com/aksh-ai/neuralBlack
I'm trying to execute this project for my study purpose. When I run the script deploy.py with powershell, the server execute and when I try to upload image and click on classify, it refresh the page and is not showing the predicted layout.
This is what error says:
127.0.0.1 - - [05/Aug/2021 04:54:13] "POST /predict HTTP/1.1" 404 - Exception
Expecting value: line 1 column 1 (char 0)
I thought there might be url name issue as the class of predict.html is with different name so I did change the url from(http://localhost:3000/predict) to (http://localhost:3000/pred_page) as below:
From
with open(os.path.join(app.config['UPLOAD_FOLDER'], filename),'rb') as img:
predicted = requests.post("http://localhost:3000/predict", files={"file": img}).json()
to
with open(os.path.join(app.config['UPLOAD_FOLDER'], filename),'rb') as img:
predicted = requests.post("http://localhost:3000/pred_page", files={"file": img}).json()
it shows this error:
127.0.0.1 - - [05/Aug/2021 05:04:41] "POST /pred_page HTTP/1.1" 405 - Exception
Expecting value: line 1 column 1 (char 0)
This is the whole deploy.py code:
import os
import requests
import json
from flask import Flask, flash, request, redirect, url_for, render_template, session
from werkzeug.utils import secure_filename
UPLOAD_FOLDER = './static/images'
ALLOWED_EXTENSIONS = ['png', 'jpg', 'jpeg']
app = Flask(__name__, template_folder='template')
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.secret_key = "m4xpl0it"
#app.route('/empty_page')
def empty_page():
filename = session.get('filename', None)
os.remove(os.path.join(UPLOAD_FOLDER, filename))
return redirect(url_for('index'))
#app.route('/pred_page')
def pred_page():
pred = session.get('pred_label', None)
f_name = session.get('filename', None)
return render_template('pred.html', pred=pred, f_name=f_name)
#app.route('/', methods=['POST', 'GET'])
def index():
try:
if request.method == 'POST':
f = request.files['bt_image']
filename = str(f.filename)
if filename!='':
ext = filename.split(".")
if ext[1] in ALLOWED_EXTENSIONS:
filename = secure_filename(f.filename)
f.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
with open(os.path.join(app.config['UPLOAD_FOLDER'], filename),'rb') as img:
predicted = requests.post("http://localhost:3000/pred_page", files={"file": img}).json()
session['pred_label'] = predicted['class_name']
session['filename'] = filename
return redirect(url_for('pred_page'))
except Exception as e:
print("Exception\n")
print(e, '\n')
return render_template('index.html')
if __name__=="__main__":
app.run(port=3000)
You should create its function into deploy.py
#app.route('/predict')
def predict():
...
return redirect(url_for('you template'))

youtube-dl front-end Video Download - using python and flask?

How to download youtube and other website video and audio file through youtube-dl (Front-End download - WEB UI)
Search and download the video through frontend.
i have write a script is python and flask -
from flask import (
Flask, Response,
render_template,
request,
redirect,
flash,
url_for,
send_file,
session,
)
import subprocess
from ydl import get_media, verify, fetch_name
from zipper import zipping
import os
app = Flask(__name__)
app.secret_key = "supposed to be a secret"
#app.route("/return-file/")
def return_file():
import pdb
#pdb.set_trace()
num_choice = session.get("choice")
filename = session.get("filename")
url = session.get("url")
if num_choice == 1:
filename_formatted = filename + ".mp3"
location = "media/Audio downloads/{}.mp3".format(session.get("id"))
if num_choice == 2:
#filename_formatted = filename + ".mp4"
#cc = get_media(url, num_choice)
print(url)
print('==============================================================================')
#"youtube-dl", "--get-url", url
#subprocess.run(["youtube-dl", "--no-check-certificate", "--get-url", url])
#subprocess.run(["youtube-dl", "--no-check-certificate", url])
test = subprocess.run(["youtube-dl", "--no-check-certificate", "--get-filename", url])
print(test)
csv = '1,2,3\n4,5,6\n'
return Response(
csv,
mimetype="text/csv",
headers={"Content-disposition":
"attachment; filename=test"})
#return send_file('', attachment_filename="myplot.csv")
print('==============================================================================')
#subprocess.run(["youtube-dl", "--no-check-certificate", url])
#location = "media/{}.mp4".format(session.get("id"))
#if os.path.isdir(location):
#print('True')
if num_choice == 3 or num_choice == 4:
filename_formatted = filename + ".zip"
location = "media/{}.zip".format(session.get("id"))
#return send_file(
#location, attachment_filename=filename_formatted, as_attachment=True
#)
#app.route("/", methods=["GET", "POST"])
def home_page():
"""
Displaying homepage
"""
title = "YDL | YouTube Downloader"
if request.method == "POST":
attempted_url = request.form["url"]
attempted_choice = int(request.form["submit"])
title = [attempted_url, attempted_choice]
if attempted_url != "":
if verify(attempted_url):
result_id = get_media(attempted_url, attempted_choice)
session["url"] = attempted_url
session["id"] = result_id
session["choice"] = attempted_choice
filename = fetch_name(attempted_url)
session["filename"] = filename
# return render_template('material-life.html', title = "Success {}".format(title))
# return render_template('material-life.html', title = result_id)
return redirect(url_for("return_file"))
else:
return render_template(
"material-life.html", title="YDL | Doesn't belong to YouTube"
)
else:
return render_template(
"material-life.html", title="YDL | URL shouldn't be empty"
)
return render_template("material-life.html", title=title)
#app.errorhandler(404)
def page_not_found(error):
"""
for anyone trying different links or searching for images within the server
"""
return (
render_template(
"error_template.html",
title="404 bud",
message="Time to make the chimi-fuckin'-changas. ",
subline="404, not there",
image_location=url_for("static", filename="images/deadpool-funny.jpg"),
),
404,
)
#app.errorhandler(400)
def bad_request(error):
"""
For handling situations where the server doesn't know what to do with the browser's request
"""
return (
render_template(
"error_template.html",
title="Aaaah ...",
message="나는 이해하지 못한다.",
subline="Yeah, the server couldn't understand what you asked for, Sorry",
image_location=url_for("static", filename="images/simpson-gangam.jpg"),
),
400,
)
if __name__ == "__main__":
app.run(debug=True)
its working on console base download... but i want to download through front end... without save - direct download
You can use pytube module, which depends on youtube-dl.
You seem to be building an app, but with the video's url, you can use the below one-liner to download the video:
from pytube import YouTube
YouTube(video_url).streams.first().download(filename='file_name')

upload an image and a label with Flask

I am toying around with an image uploader using Flask. I cloned it from this repository https://github.com/chokepoint/flaskgur . I have it running on my VPS here
http://107.170.119.38
What I'm trying to do is enable someone to write a custom label for an image they are about to upload. The final product should display an image and the custom label the user typed in. The image uploader works fine but I can't seem to store the label in the database on upload. Here is my code
from flask import Flask, request, g, redirect, url_for, abort, render_template,send_from_directory
from werkzeug import secure_filename
from hashlib import md5
from PIL import Image
import sqlite3
import os
import time
DEBUG = True
BASE_DIR = '/var/www/flaskgur/'
UPLOAD_DIR = BASE_DIR + 'pics'
DATABASE = BASE_DIR + 'flaskgur.db'
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif'])
app = Flask(__name__)
app.config.from_object(__name__)
# Make sure extension is in the ALLOWD_EXTENSIONS set
def check_extension(extension):
return extension in ALLOWED_EXTENSIONS
def connect_db():
return sqlite3.connect(app.config['DATABASE'])
# Return a list of the last 25 uploaded images
def get_last_pics():
cur = g.db.execute('select * from pics order by id desc limit 25')
filenames = [dict(id=row[0], filename=row[1], label=row[2]) for row in cur.fetchall()]
#filenames = [row[0] for row in cur.fetchall()]
return filenames
# Insert filename into database
def add_pic(filename, label):
g.db.executemany('insert into pics (filename, label) values (?, ?)', [filename, label])
g.db.commit()
# Generate thumbnail image
def gen_thumbnail(filename):
height = width = 200
original = Image.open(os.path.join(app.config['UPLOAD_DIR'], filename))
thumbnail = original.resize((width, height), Image.ANTIALIAS)
thumbnail.save(os.path.join(app.config['UPLOAD_DIR'], 'thumb_'+filename))
def add_label(label):
label = request.form['label']
g.db.execute('INSERT INTO pics (label) VALUES (?)', [label])
g.db.commit()
# Taken from flask example app
#app.before_request
def before_request():
g.db = connect_db()
# Taken from flask example app
#app.teardown_request
def teardown_request(exception):
db = getattr(g, 'db', None)
if db is not None:
db.close()
#app.errorhandler(404)
def page_not_found(e):
return render_template('404.html'), 404
#app.route('/', methods=['GET','POST'])
def upload_pic():
if request.method == 'POST':
file = request.files['file']
try:
extension = file.filename.rsplit('.', 1)[1].lower()
except IndexError, e:
abort(404)
if file and check_extension(extension):
# Salt and hash the file contents
filename = md5(file.read() + str(round(time.time() * 1000))).hexdigest() + '.' + extension
file.seek(0) # Move cursor back to beginning so we can write to disk
file.save(os.path.join(app.config['UPLOAD_DIR'], filename, label))
add_pic(filename)
add_label(label)
gen_thumbnail(filename)
return redirect(url_for('show_pic', filename=filename))
else: # Bad file extension
abort(404)
else:
return render_template('upload.html', pics=get_last_pics())
#app.route('/show')
def show_pic():
filename = request.args.get('filename','')
return render_template('upload.html', filename=filename, label=label)
def show_label():
g.db = connect_db()
cur = g.db.execute('SELECT label FROM pics WHERE id=(?)')
labels = cur.fetchone()
return render_template('upload.html', labels=labels)
#app.route('/pics/<filename>')
def return_pic(filename):
return send_from_directory(app.config['UPLOAD_DIR'], secure_filename(filename))
if __name__ == '__main__':
app.run(debug=True,host='0.0.0.0')
And my template (upload.html):
{% if filename is defined %}
<div id="image"><img class="new" src="{{ pic_path(filename) }}"></div>
<h2><p>The Label is: </p>{{ labels }}</h2>
{% else %}
<ul>
{% for pic in pics %}
<li class="thumb"><img class="thumb" src="{{ pic_path('thumb_'+pic) }}"></li>
{% endfor %}
base template (base.html):
<div id="upload">
<h1>Upload Picture</h1>
<form action="/" method=post enctype=multipart/form-data>
<p>
<input type=file name=file><br />
<p>Add a Label:</p>
<input type=text name=label value={{ request.form.label }}><br />
<input type=submit value=Upload>
</p>
</div>
I apologize if my code is atrocious. I am trying my best to learn
I figured it out. Rewrote the get_last_pics() function to this:
# Return a list of the last 25 uploaded images
def get_last_pics():
try:
cur = g.db.execute('select filename, label from pics order by id desc limit 25')
filenames = []
for row in cur.fetchall():
filenames.append({"filename": row[0], "label": row[1] or ''})
return filenames
except:
return []
rewrote add_pic() to this:
# Insert filename and label into database
def add_pic(filename, label):
g.db.execute('insert into pics (filename, label) values (?, ?)', [filename, label])
g.db.commit()
rewrote upload_pic() to this:
#app.route('/', methods=['GET', 'POST'])
def upload_pic():
if request.method == 'POST':
file = request.files['file']
label = request.form['label']
try:
extension = file.filename.rsplit('.', 1)[1].lower()
except IndexError, e:
abort(404)
if file and check_extension(extension):
# Salt and hash the file contents
filename = md5(file.read() + str(round(time.time() * 1000))).hexdigest() + '.' + extension
file.seek(0) # Move cursor back to beginning so we can write to disk
file.save(os.path.join(app.config['UPLOAD_DIR'], filename))
add_pic(filename, label)
gen_thumbnail(filename)
return redirect(url_for('show_pic', filename=filename))
else:
# Bad file extension
abort(404)
else:
return render_template('upload.html', pics=get_last_pics())
rewrote show_pic() to this:
#app.route('/show')
def show_pic():
filename = request.args.get('filename', '')
t = (filename,)
cur = g.db.execute('select label from pics where filename=?', t)
label = cur.fetchone()[0]
return render_template('upload.html', filename=filename, label=label)
Everything works now. Hope that helps someone in the future

Python check if file exists doesnt work although the file DO exists

i am writing an application in Flask and at some point I want to start some process (quick process) and then check if there is output on the server. If there is - download it, if not - show approprieate communicate.
Here is my code:
import os
import subprocess
import sqlite3
from flask import Flask, render_template, request, redirect, g, send_from_directory
app = Flask(__name__)
app.config.from_object(__name__)
# Config
app.config.update(dict(
DATABASE = os.path.join(app.root_path, 'database/records.db'),
DEBUG = True,
UPLOAD_FOLDER = 'uploads',
OUTPUT_FOLDER = 'outputs',
ALLOWED_EXTENSIONS = set(['txt', 'gro', 'doc', 'docx'])
))
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1] in app.config['ALLOWED_EXTENSIONS']
def run_calculations(filename):
subprocess.call(['python', os.path.join(app.root_path, 'topologia.py'), 'uploads/' + filename])
def get_db():
db = getattr(g, '_database', None)
if db is None:
db = g._database = sqlite3.connect(app.config['DATABASE'])
return db
#app.teardown_appcontext
def close_connection(exception):
db = getattr(g, '_database', None)
if db is not None:
db.close()
#app.route('/')
def index():
return render_template('index.html')
#app.route('/outputs/<filename>')
def calculated_record(filename):
return send_from_directory(app.config['OUTPUT_FOLDER'], filename)
#app.route('/upload', methods = ['GET', 'POST'])
def upload():
with app.app_context():
cur = get_db().cursor()
cur.execute('INSERT INTO records(calculated) VALUES(0)')
file_id = cur.lastrowid
get_db().commit()
file = request.files['file']
if file and allowed_file(file.filename):
input_file = str(file_id) +'.'+file.filename.rsplit('.', 1)[1]
file.save(os.path.join(app.config['UPLOAD_FOLDER'], input_file))
run_calculations(input_file)
output_name = '/outputs/topologia_wynik' + str(file_id) + '.top'
if os.path.isfile(output_name):
return redirect(output_name)
else:
return 'Your file is beeing loaded'
else:
return "Something went wrong, check if your file is in right format ('txt', 'gro', 'doc', 'docx')"
if __name__ == '__main__':
app.run()
My whole problem is in this part of code:
if os.path.isfile(output_name):
return redirect(output_name)
else:
return 'Your file is beeing loaded'
Because if is never true... When I delete this part of code and redirect to output file without checking it all works fine... Do you have any idea why this happens?
The reason is probably / in the beginning of '/outputs/topologia_wynik' + str(file_id) + '.top'. It means "outputs" folder should be under the root folder, and in your case it seems to be under the server working directory.
Why not pass os.path.join(app.config['OUTPUT_FOLDER'], 'topologia_wynik' + str(file_id) + '.top') to os.path.isfile() as you did with the input file name?

Django #csrf_exempt doesn't work on webfaction server?

My iOS app need send POST request to Django.
I use "#csrf_exempt" to disable to "CSRF". On local, iOS and Django works very well.
But after I deploy the website to to the "webfaction", the Django can not get the iOS POST request.
My question is how to deal with this problem about CSRF, need I do some other settings to Django?
[Here is my Django code]
__author__ = 'zhaonanli'
from django.views.decorators.csrf import csrf_exempt
from django.http import HttpResponse
import json
from models import *
import umpme.settings
import base64
#csrf_exempt
def upload_image_for_profile(request):
static_dir = umpme.settings.STATIC_ROOT
users_dir = static_dir + 'users/'
back_dic = {}
if request.method == 'POST':
if request.raw_post_data:
try:
json_data_decoding = json.loads(request.raw_post_data)
big_image_raw_data_b64string = json_data_decoding['big_imageb64string']
small_image_raw_data_b64string = json_data_decoding['small_imageb64string']
big_image_raw_data = base64.b64decode(big_image_raw_data_b64string)
small_image_raw_data = base64.b64decode(small_image_raw_data_b64string)
uid = json_data_decoding['uid']
user = UserAccountInfo.objects.get(id=uid)
uemail = user.usignup_email
whole_image_dir = users_dir + 'umpme_' + uemail + '_user/'
bigimage_filename = whole_image_dir + 'bigprofile.jpg'
smallimage_filename = whole_image_dir + 'smallprofile.jpg'
bigimage = open(bigimage_filename, 'wb')
bigimage.write(big_image_raw_data)
bigimage.close()
smallimage = open(smallimage_filename, 'wb')
smallimage.write(small_image_raw_data)
smallimage.close()
back_dic['succ'] = 'yes'
back_dic['error'] = 'nothing'
back_dic['uid'] = uid
except Exception, e:
back_dic['succ'] = 'no'
back_dic['error'] = 'error = ' + str(e)
back_dic['uid'] = 'none'
else:
back_dic['succ'] = 'no'
back_dic['error'] = 'cannotgetrawpostdata'
back_dic['uid'] = 'none'
else:
back_dic['succ'] = 'no'
back_dic['error'] = 'notgetrequest'
back_dic['uid'] = 'none'
return HttpResponse(json.dumps(back_dic), content_type='application/json')

Categories