Convert sympy expressions to fenicsx expressions

Hi fenics experts!

I would like to be able to have sympy expressions to define source terms to go into the variational formulation

There used to be a way around this in legacy fenics by doing the following:

Expression(sp.printing.ccode(sympy_expression), t=0, degree=2)

Is there an equivalent workaround in fenicsx since the depreciation of the expression function?

I have attached an example code of what I would like to be able to do.

import numpy as np
import sympy as sp
from petsc4py import PETSc
from mpi4py import MPI
from dolfinx.mesh import create_rectangle
from dolfinx.fem import (
    Constant,
    Function,
    FunctionSpace,
)
from dolfinx.fem.petsc import (
    NonlinearProblem,
)
from dolfinx.nls.petsc import NewtonSolver
import ufl

mesh = create_rectangle(MPI.COMM_WORLD, [np.array([0, 0]), np.array([1, 1])], [10, 10])

V = FunctionSpace(mesh, ufl.FiniteElement("CG", mesh.ufl_cell(), 1))
u = Function(V)
v = ufl.TestFunction(V)

x = ufl.SpatialCoordinate(mesh)
xx = sp.Symbol("x[0]")

source_1 = 2 * sp.exp(2 * xx)
source_2 = (
    sp.Piecewise(
        (2 * xx, xx < 0.5),
        (3 * xx, True),
    ),
)

F = ufl.dot(ufl.grad(u), ufl.grad(v)) * ufl.dx
F += source_1 * v * ufl.dx
F += source_2 * v * ufl.dx

problem = NonlinearProblem(F, u, bcs=[])
solver = NewtonSolver(MPI.COMM_WORLD, problem)
solver.solve(u)

You could write this directly in the ufl syntax

import ufl

x = ufl.SpatialCoordinate(mesh)
source_1 = 2 * ufl.exp(2*x[0])
source_2 = ufl.conditional(ufl.lt(x[0], 0.5), 2*x[0], 3*x[0]))

Ok will do, thanks @dokken !