Python Plotly multicolored line plot by Z values

Given a dataframe df with columns X,Y,Z, generating the xz plot with plotly is relatively easy:

    px.line(df, x='X', y='Y',).write_image("path.png")

The question is - how can I color encode it in, for example, grey-red colorcoding, where grey represents low values, and red high values, by the values stored in Z?
example plot


AFAIK it is not possible to have a gradient in a 2d line trace. If you can use px.scatter, then this is easily done using color="Z".