from flask import Flask, render_template, request, redirect, url_for, session from PIL import Image from gen_image import gen_image from printer_info import * from datetime import datetime from print import * from format_image import * import os app = Flask(__name__) app.config['UPLOAD_FOLDER'] = 'static/uploads' app.config['TEXT_FOLDER'] = 'static/text' app.secret_key = "blahaj" # Ensure the upload directory exists if not os.path.exists(app.config['UPLOAD_FOLDER']): os.makedirs(app.config['UPLOAD_FOLDER']) if not os.path.exists(app.config['TEXT_FOLDER']): os.makedirs(app.config['TEXT_FOLDER']) #TODO: make it so sessions don't interfere with eachother @app.route('/', methods=['GET', 'POST']) def base(): return render_template('base.html') @app.route('/image', methods=['GET', 'POST']) def image(): if request.method == 'POST': if 'image' not in request.files: return redirect(request.url) file = request.files['image'] if file.filename == '': return redirect(request.url) if file: extension = os.path.splitext(file.filename)[1] filepath = os.path.join(app.config['UPLOAD_FOLDER'], "upload" + extension) session["filepath"] = filepath file.save(filepath) # Process image # format_image(filepath).save(filepath) ################# session["filename"] = "upload"+extension return render_template('image.html', filename=session["filename"], cut=session["cut"]) session["cut"] = True return render_template('image.html', cut=True) #TODO: maybe merge some stuff with text-print @app.route('/image-print', methods=['GET', 'POST']) def image_print(): if request.method == 'POST': image = format_image_to_label(session["filepath"]) print_image(image) if 'cut' in request.form: print("printing and cutting") cut_paper() session["cut"] = True; else: print("printing") session["cut"] = False; return render_template('image.html', filename=session["filename"], cut=session["cut"]) return render_template('image.html', filename=session["filename"], cut=session["cut"]) templates = { "DNH": { "text1": { "string": "Do Not Hack", "size": 50, "pos": 0 }, "text2": { "string": "bottom text", "size": 25, "pos": 55 }, "cut": True }, "Food": { "text1": { "string": "Nickname", "size": 50, "pos": 0 }, "text2": { "string": "", "size": 25, "pos": 55 }, "cut": True } } @app.route('/text-template', methods=['GET', 'POST']) def text_template(): if request.method == 'POST': template = templates[request.form["template"]] if request.form["template"] == "Food": template["text2"]["string"] = datetime.now().strftime('%Y-%m-%d') session["text1"] = template["text1"] session["text2"] = template["text2"] session["cut"] = template["cut"] return render_template('text.html', filename="text.png", text1=session["text1"], text2=session["text2"], cut=session["cut"]) return render_template('text.html', filename="text.png", text1=session["text1"], text2=session["text2"], cut=session["cut"]) @app.route('/text-form', methods=['GET', 'POST']) def text_form(): if request.method == 'POST': session["text1"] = { "string": request.form["string1"], "size": int(request.form["size1"]), "pos": int(request.form["pos1"]), } session["text2"] = { "string": request.form["string2"], "size": int(request.form["size2"]), "pos": int(request.form["pos2"]), } img = gen_image(label_width, session["text1"], session["text2"]) filepath = os.path.join(app.config['TEXT_FOLDER'], "text.png") img.save(filepath) print(session) return render_template('text.html', filename="text.png", text1=session["text1"], text2=session["text2"], cut=session["cut"]) return render_template('text.html', filename="text.png", text1=session["text1"], text2=session["text2"], cut=session["cut"]) @app.route('/text', methods=['GET', 'POST']) def text(): session["text1"] = templates["DNH"]["text1"] session["text2"] = templates["DNH"]["text2"] session["cut"] = templates["DNH"]["cut"] return render_template('text.html', filename="text.png", text1=session["text1"], text2=session["text2"], cut=session["cut"]) @app.route('/text-print', methods=['GET', 'POST']) def text_print(): if request.method == 'POST': image = format_image_to_label("static/text/text.png") print_image(image) if 'cut' in request.form: print("printing and cutting") cut_paper() session["cut"] = True; else: print("printing") session["cut"] = False; return render_template('text.html', filename="text.png", text1=session["text1"], text2=session["text2"], cut=session["cut"]) return render_template('text.html', filename="text.png", text1=session["text1"], text2=session["text2"], cut=session["cut"]) @app.route('/uploads/') def uploaded_file(filename): return redirect(url_for('static', filename='uploads/' + filename)) @app.route('/text/') def generated_file(filename): return redirect(url_for('static', filename='text/' + filename)) if __name__ == "__main__": app.run(debug=True)