Files
wallter/plotter-app/app.py
Nathan-rek dd4ff7bfa8 maj
2024-11-18 16:25:39 +01:00

100 lines
2.7 KiB
Python

from flask import Flask, render_template, request
import sys
import os
from streamer import stream_gcode
import svgToGcode
app = Flask(__name__)
ALLOWED_EXTENSIONS_SVG_FORM = {'svg'}
ALLOWED_EXTENSIONS_GCODE_FORM = {'gcode'}
def allowed_file_svg(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS_SVG_FORM
def allowed_file_gcode(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS_GCODE_FORM
@app.route("/", methods=["POST", "GET"])
def index():
return render_template("form.html")
@app.route("/svg_file", methods=["POST", "GET"])
def getSVG():
if request.method == "POST":
file = request.files.get("file")
if file and allowed_file_svg(file.filename):
output = 'gcode_output.gcode'
gcode = svgToGcode.convertToGcode(file.read() ,output)
stream_gcode(gcode.splitlines())
print(gcode)
print("done!")
else:
print('unaccepted file')
return render_template("form.html")
@app.route("/gcode_file", methods=["POST", "GET"])
def getGcode():
if request.method == "POST":
file = request.files.get("file")
if file and allowed_file_gcode(file.filename):
gcode = file.readlines()
print(str(gcode))
stream_gcode(gcode)
else:
print('empty file or unaccepted file')
return render_template("form.html")
@app.route("/text", methods=["POST", "GET"])
def getText():
if request.method == "POST":
txt = request.form["txt"].strip()
if txt:
# Découpe le texte en lignes de 20 caractères max
formatted_text = ""
current_line = ""
for word in txt.split():
if len(current_line) + len(word) + 1 > 20:
formatted_text += current_line.strip() + "\n"
current_line = word + " "
else:
current_line += word + " " # Ajoute le mot à la ligne actuelle
# Ajoute la dernière ligne si elle n'est pas vide
if current_line.strip():
formatted_text += current_line.strip()
print("Original text:", txt)
print("Formatted text with line breaks:")
print(formatted_text)
else:
print('empty text')
return render_template("form.html")