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?

image

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

bpy.ops.render.opengl(write_still=True)

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

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