Using callback to update plot legend

Hi,
I am using SciPy to generate a regression line that displays on a scatter plot, and the legend for that line displays the regression equation as a string. When I use box select on certain points in the scatter, a CustomJS call will update the regression line to match only the selected points. I would like to be able to also update the legend to match the new equation during the same callback, but for whatever reason it’s not updating. Any advice on how to make this work? My code is below. Thanks!

xline = my_data

yline =

for i in range(0, len(xline)):

yline.append((slope*xline[i])+intercept)

lineeq =

for i in range(0, len(xline)):

lineeq.append(“y = %.3fx + %.3f, r^2 = %.2f” %(slope, intercept, r_value**2))

sline = ColumnDataSource(

data = dict(

x = xline,

y = yline,

xo = xline,

yo = yline,

l = lineeq,

lo = lineeq

)

)

p1.line(xline, yline, line_color=‘green’, line_width=2, source=sline, legend=lineeq[0])

//s1 is the scatter plot. Callback does update correctly for the regression line data.

s1.callback = CustomJS(args=dict(sline=sline), code="""

var inds = cb_obj.get(‘selected’)[‘1d’].indices;

var d1 = cb_obj.get(‘data’); //Figure 1 data

var dline = sline.get(‘data’); //Figure 1, regression line data

if (inds.length != 0) {

ynew =

xnew =

for (i = 0; i < inds.length; i++) {

ynew.push(d1[‘y’][inds[i]])

xnew.push(d1[‘x’][inds[i]])

}

var lr = {};

    var n = ynew.length;

    var sum_x = 0;

    var sum_y = 0;

    var sum_xy = 0;

    var sum_xx = 0;

    var sum_yy = 0;

    for (var i = 0; i < ynew.length; i++) {

        sum_x += xnew[i];

	sum_y += ynew[i];

	sum_xy += (xnew[i]*ynew[i]);

	sum_xx += (xnew[i]*xnew[i]);

	sum_yy += (ynew[i]*ynew[i]);

    }

    lr['slope'] = (n * sum_xy - sum_x * sum_y) / (n*sum_xx - sum_x * sum_x);

    lr['intercept'] = (sum_y - lr.slope * sum_x)/n;

    lr['r2'] = Math.pow((n*sum_xy - sum_x*sum_y)/Math.sqrt((n*sum_xx-sum_x*sum_x)*(n*sum_yy-sum_y*sum_y)),2);

dline[‘x’] = xnew

dline[‘y’] =

dline[‘l’] =

for (i = 0; i < dline[‘x’].length; i++) {

dline[‘y’].push(lr[‘slope’]*dline[‘x’][i]+lr[‘intercept’])

dline[‘l’].push(lr[‘slope’]+'x + '+lr[‘intercept’] + ', r^2 = ’ + lr[‘r2’]);

}

} else { //

dline[‘x’] = dline[‘xo’];

dline[‘y’] = dline[‘yo’];

dline[‘l’] = dline[‘lo’];

}

sline.trigger(‘change’);

“”")