How do I return binary stream data from the canvas through the interface


I want to return the finished graph through the interface, but because of the slow I/O, I want to directly convert the canvas into binary stream data, do you have any solution?

From your post I am not sure exactly what you mean. I think you are describing a case where you want to serve rendered images over a network interface of some sort. Your local disk is very slow so instead of saving a file to disk and then re-reading that back to push into the socket, you want to write the rendered image to an in memory buffer and then directly push that into the socket?

If that is the case then you can pass a io.BytesIO instance to fig.savefig .
For a complete example, see Embedding in a web application server (Flask) — Matplotlib 3.5.1 documentation which also includes an html template for putting the image in a dataurl.