This repository has been archived on 2020-04-29. You can view files and clone it, but cannot push or open issues or pull requests.
namensschilder/server.py
Simon Bruder 3b5d1a932c
Better handling of concurrent requests
It now supports multiple name tags for different names to be generated
at the same time. Two concurrent requests for the same name will
probably fail.
2020-04-29 15:29:35 +02:00

62 lines
1.6 KiB
Python
Executable file

#!/usr/bin/env python3
from jinja2 import Template
from pyppeteer import launch
from quart import Quart, request, render_template, url_for, redirect
from subprocess import run
import asyncio
import os
app = Quart(__name__)
def run_check(*args, **kwargs):
return run(*args, **kwargs, check=True)
async def gen_pdf(name):
browser = await launch(args=["--no-sandbox"])
page = await browser.newPage()
await page.goto("file://" + os.path.join(os.getcwd(), "namensschild.html"))
await page.evaluate(f"document.querySelector('#name').innerText = '{name}'")
await page.pdf(path=f"static/{name}.pdf", width="180px", height="60px")
await browser.close()
@app.route("/")
async def index():
return await render_template("index.html.j2")
@app.route("/generate")
async def generate():
name = request.args["name"]
await gen_pdf(name)
run_check(
["inkscape", "-T", "-E", f"static/{name}.eps", f"static/{name}.pdf",]
)
run_check(
[
"pstoedit",
"-q",
"-flat",
"0.0001",
"-dt",
"-f",
"dxf: -polyaslines -mm",
f"static/{name}.eps",
f"static/{name}.dxf",
]
)
with open("namensschild.scad.j2", "r") as f:
template = Template(f.read())
with open(f"static/{name}.scad", "w") as f:
f.write(template.render(name=name))
run_check(["openscad", "-o", f"static/{name}.stl", f"static/{name}.scad"])
return redirect(url_for("static", filename=f"{name}.stl"), code=302)
if __name__ == "__main__":
app.run(debug=True)