pythonflasknumpy-stl

How to use numpy-stl with file uploaded with Flask request


I am writing an app, which takes a STL file as input. I want to get volume of the stl object without saving the stl file and use the volume to calculate a quote and post it back to browser. Right now I am using numpy-stl package, but I am stuck on how to create a mesh object for numpy-stl from the file I get with request.files['file'].read(). Any help is appreciated.

mycode:

what I get for filedata

what I get for error


Solution

  • You can try the following code:

    import io
    
    filedata = request.files['file'].read()
    data = io.BytesIO(filedata)
    tmp_mesh = mesh.Mesh.from_file("tmp.stl", fh=data)
    

    You can use tmp_mesh object to do you interesting operation

    suggestion to add error handle on something not expected

    1. if request.files not contain 'file' keys