Is possible to generate a preview material from python? I would like to generate the preview of a list of materials. I could create a sphere and apply the material, render and save it in a loop, but what I would like is to use the internal functionality of template preview.

My idea is to get the image object of the template preview, and then save it as any image.

I have seen something about using a custom render, but I don't know where find more information.

I will appreciate any idea.
BlenderKit has a built in thumbnail renderer that does a pretty decent job, and the operator can be used quite easily in your custom script. The disadvantage of it might be the speed, since it renders a full cycles scene, unlike the preview that has obviously many optimisations and renders instantly.