How to render an animation made with a python Script

I have a simpĺe python script that triggers an animation base on the location of a cube.

import bpy

scene = bpy.context.scene
obj = scene.objects['Text']

def recalculate_text(scene):
    x = scene.objects['Cube'].location[0]
    obj.data.body = '{0:.1f}'.format(x)

bpy.app.handlers.frame_change_pre.append(recalculate_text)

Using a text Object it shows the location of the cube in the Viewport.

The problem is that because my python skills are very basic, i can’t Render the animation; once i hit “Render animation” ( Itried: EEVEE and CYCLES: Image sequence and video) it only shows the first frame of the text object. Here is the Blend File

I’m using Blender 2.90

found a solution, in case someone has the same problem:

import bpy

def frame_handler(scene, depsgraph):
    obj = scene.objects['Cube']
    obj = obj.evaluated_get(depsgraph)
    scene.objects['Text'].data.body = '{0:.1f}'.format(obj.location.x)

bpy.app.handlers.frame_change_post.clear()
bpy.app.handlers.frame_change_post.append(frame_handler)

But for me is only working in 2.80 (Linux)