BytesIO are simply files stored in memory.
Why do I need to use bytes?
Sometimes when we need to do frequent operations/serial processes whose outputs depend on each other. Such processes are usually are storage-intensive. To avoid abusing the storage devices we need to find a way to store data in RAM.
Bytes IO
String IO
WEBP IO
An example of rendering plots in memory with BytesIO
This is figure can directly be passed as a variable to Jinja/Jinja2 template engine and it’ll render it as webp.
return render_template('index.html', figure=figure)
Line 10 in WEBP IO decodes the stored image format in a way that is understood by the browsers. (Base 64 encoding/decoding)