Update y-axis range upon changes of x-axis range (due to zooming/panning) ; FigureWidget/Python

I need to make changes to the y-axis range each time the visible x-axis range changes. I got as far correctly determining the new y-axis range.

But for some strange reason I canโ€™t manage the y-axsis range with fig.update_layout(yaxis_range=[yrange_min, yrange_max])
Something breaks, but when I comment out the update_layout then the y-axis min and max are correctly determined after the callback is invoked.

I must use FIgureWidget because I use the Jupyter extension in VS Code.

Can someone please point me in the right direction? Below my code (comment out the logging function before running)

Thanks,
Matt

import plotly.graph_objects as go
import numpy as np
import logging
import os
from config.logging_config import setup_logging


log_file = os.path.join("logs", "visuals.log")
setup_logging(logging_enabled=True, log_level=logging.INFO, log_file=log_file, console_output=False, clean_log_file=True)
logger = logging.getLogger(__name__)
logger.info("logger started")

# Generate some sample data
np.random.seed(0)
x_data = np.linspace(0, 100, 100)
y_data = np.random.rand(100) * 10

# Create a FigureWidget
fig = go.FigureWidget(data=[go.Scatter(x=x_data, y=y_data)])
fig.update_layout(width=1200, height=400)


# Define the callback function for relayout events
def handle_relayout(change):
    """
    Callback function to handle relayout events via observe.
    """ 
    
    if "new" in change:
        if "_js2py_relayout" in change["new"]:
            if "relayout_data" in change["new"]["_js2py_relayout"]:
                if "xaxis.range[0]" in change["new"]["_js2py_relayout"]["relayout_data"]:
                    xrange_min = change["new"]["_js2py_relayout"]["relayout_data"]["xaxis.range[0]"]
                    xrange_max = change["new"]["_js2py_relayout"]["relayout_data"]["xaxis.range[1]"]
                elif "xaxis.autorange" in change["new"]["_js2py_relayout"]["relayout_data"]:
                    xrange_min = min(x_data)
                    xrange_max = max(x_data)
                else:
                    return
               
                #now update the visible y-axis scale
                # Calculate y-axis range with padding
                padding_percent = 0.05
                mask = (x_data >= xrange_min) & (x_data <= xrange_max)
                yrange_min = min(y_data[mask])
                yrange_max = max(y_data[mask])
                yrange_min -= yrange_min * padding_percent
                yrange_max += yrange_max * padding_percent
                
                # Update the y-axis range
                fig.update_layout(yaxis_range=[yrange_min, yrange_max])
                logger.info(f"x-axis range changed to: {xrange_min} - {xrange_max} and y-axis range changed to: {yrange_min} - {yrange_max}")

fig.observe(handle_relayout)
fig