How to render viewport to file via python?

Hi all, how can I render the viewport to a file as in the command shown below?


This seems to do it. Now how to set the viewport to the camera before rendering?


adding view_context=False at the end uses the current camera.

bpy.ops.render.opengl(write_still=True, view_context=False)