How to write derivative of a function

I am looking for help on how to implement these as the linear functional on variational formulation.

Let me show you an example on 2D mesh. Here are the expressions of \nabla f,f_x,f_y.

from fenics import *
mesh = UnitSquareMesh(10,10)
V = FunctionSpace(mesh,"P",1)
f = Function(V)
grad_f = grad(f)
f_x = grad(f)[0]
f_y = grad(f)[1]