Can't update x_range/y_range using CustomJS and select from dropdown

I am trying to update y_range for my Bokeh plot using CustomJS which should update based on the selection from the dropdown widget. I tried using y_range.factors = factors in CustomJS, but it does not seem to be working. My axis is categorical. It would be great if someone could help me in this. Here’s my code:

p3 = figure(title="title",
           x_axis_location="below", plot_width=400, plot_height=800,
            x_range=sensors, y_range=FactorRange(*run_ids),
           tools=TOOLS, toolbar_location='above',
           tooltips=[('Tooltip', '@tooltip'])

callback = CustomJS(args=dict(source1 = Overall, sc1 = Curr, plot = p3), code="""
console.log(plot)
var f = cb_obj.value
console.log(f)
sc1.data['run_id']=[\*]
for(var i = 0; i <= source1.get_length(); i++){
    if (source1.data['computer_name'][i] == f){
        sc1.data['run_id'].push(source1.data['run_id'][i])
        }
    }
console.log(plot.y_range)
plot.y_range.factors = sc1.data['run_id'].filter((v, i, a) => a.indexOf(v) === i);
console.log(plot.y_range)
plot.y_range.change.emit();
""")

p3.rect(x="sensor", y="run_id", width=1, height=1,
       source=Curr,
       fill_color={'field': 'avg', 'transform': mapper},
       line_color=None)

menu = Select(options=list(df['computer_name'].unique()),value=laptop_list[0], title = 'Laptop')

menu.js_on_change('value', callback)
layout=column(menu, p3)
show(layout)

It throws an error in console when setting y_range.factors:
“cannot read property ‘length’ of undefined”

Please let me know if I can improve on submitting a question. Thank you!

Hi @Yogi please edit your post to use code formatting so that the code is intelligible (either with the </> icon on the editing toolbar, or triple backtick ``` fences around the code blocks)

Hi Bryan, thank you for the feedback. I changed it.

Also, figured out the solution. Instead of plot.y_range.change.emit() it needs to be plot.change.emit()

@Yogi I am surprised that you need either. Are you saying it does not work if you delete the emit line altogether?

Sorry for the delayed response @Bryan. Yes, if I remove emit line, the plot does not update.

That’s strange, but a would need complete minimal reproducer to investigate further.

I will test this with a simple dataset today and will let update you with what I find.