Set bmesh active edge

How would I go about setting the active bmesh element in python, specifically the active edge? (And seeing it correctly in the viewport?)

I’ve been searching around and there’s quite a bit on getting it, but I can’t seem to find anything on setting it.

To get the active element, use bm.select_history.active

To set an active element, use bm.select_history.add(elem)
The last added element will be active, provided that it’s selected.

Optionally, use bm.select_history.validate() after changes to ensure that only selected elements are in select_history.

Update the edit mesh afterwards.

Example that clears the active list and sets an edge (bm.edges[0]) to active:

import bpy
import bmesh

me = bpy.context.object.data
bm = bmesh.from_edit_mesh(me)

edge = bm.edges[0]
edge.select = True  # select the element before making it active

bm.select_history.clear()  # optionally clear previous elements
bm.select_history.add(edge)

bmesh.update_edit_mesh(me)

Thanks! I got my script working thanks to your help. I suspected it was related to to select_history before, but I was missing a few things.