Improve user experience with interactive client
Based on Vue.js, mainly because it can be used without any compilation.
This commit is contained in:
39
app/app.py
39
app/app.py
@ -66,8 +66,28 @@ class Generator:
|
||||
return self.GENERATED_STL_FILE_NAME
|
||||
|
||||
|
||||
@app.route("/generate/<name>")
|
||||
async def generate_rest(name: str):
|
||||
@app.route("/generate", methods=["POST"])
|
||||
async def generate():
|
||||
try:
|
||||
# support both modern JSON body requests as well as classic forms
|
||||
form_data = await request.form
|
||||
json_data = await request.json
|
||||
|
||||
if form_data:
|
||||
data = form_data
|
||||
|
||||
elif json_data:
|
||||
data = json_data
|
||||
|
||||
else:
|
||||
raise ValueError
|
||||
|
||||
name = data["name"]
|
||||
|
||||
except (TypeError, KeyError, ValueError):
|
||||
abort(400)
|
||||
return
|
||||
|
||||
async with semaphore:
|
||||
with tempfile.TemporaryDirectory(prefix="fablab-bottle-clip-generator-") as tempdir:
|
||||
generator = Generator(name, tempdir)
|
||||
@ -98,24 +118,17 @@ async def generate_rest(name: str):
|
||||
|
||||
# using secure_filename allows us to send the file to the user with some safe yet reasonably
|
||||
# identifiable filename
|
||||
return await send_file(
|
||||
response = await send_file(
|
||||
bytes_io,
|
||||
mimetype="model/stl",
|
||||
as_attachment=True,
|
||||
attachment_filename=out_filename,
|
||||
)
|
||||
|
||||
# avoid the need for a content-disposition parser in the client code
|
||||
response.headers["download-filename"] = out_filename
|
||||
|
||||
# aside from the RESTful API above, we need a "traditional" HTML forms compatible end point
|
||||
@app.route("/generate")
|
||||
async def generate_form():
|
||||
try:
|
||||
name = request.args["name"]
|
||||
except KeyError:
|
||||
abort(400)
|
||||
return
|
||||
|
||||
return await generate_rest(name)
|
||||
return response
|
||||
|
||||
|
||||
@app.route("/")
|
||||
|
Reference in New Issue
Block a user