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.


what I get for filedata

what I get for error


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

Answered By – peng pan

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published