使用bokeh和javascrip的滑块回调

2024-09-28 01:33:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我用滑块显示一些单词。所有的名单都很好。我有20多个元素。滑块在Bokeh0.12.6下工作,但现在某些元素在0.12.9时根本不显示。奇怪的是,每次运行代码时,这似乎不是随机的。例如,元素6不是每次都出现,而是存在于列表中。 我使用滑块的值同时更改hbar和文本。 有人有同样的问题吗?我在回调函数中出错了吗?

sourcesl = ColumnDataSource(data=dict(ysl=ysl, rightsl=rightsl, hightsl=hightsl, leftsl=leftsl, thertpctn0=thertpctn0, thertpct0=thertpct0, wlen=wlen, wtext=wtext, thepct=thepct, prtg=prtg))
callback = CustomJS(args=dict(source=sourcesl), code="""
            var data = source.data;
            var w = cb_obj.value

            rightsl = data['rightsl']
            prtg = data['prtg']
            wtext = data['wtext']
            ysl = data['ysl']            
            thertpctn0 = data['thertpctn0']
            thertpct0 = data['thertpct0']
            wlen = data['wlen']                
            thepct = data['thepct']           

            var som = 0
            for (i = 0; i < w; i++) {
                som = som +thertpctn0[i]
            }
            rightsl[0] = 100*(som/wlen[0])
            rightsl[1] = 100                
            wtext[0] = thertpct0[w] 
            prtg[0] = thepct[w]

            source.change.emit();
        """)
        slider = Slider(start=1, end=20, value=20, step=1, title="SLIDER for Words Most Used")
        slider.js_on_change('value', callback)
psl = figure(width=355, height=200,tools="pan,reset,previewsave")
        psl.y_range = Range1d(0,4)
        psl.x_range = Range1d(0, 100)
        psl.hbar('ysl', 'hightsl','rightsl', 'leftsl',  color="LightSeaGreen", source=sourcesl)
psl.text(45, 3, text='wtext',
                alpha=0.6667, text_font_size='32pt', text_baseline='middle',
                text_align='center', source=sourcesl)
        nby = 2
        psl.text(45, nby, text='prtg',
                 alpha=1, text_font_size='12pt', text_baseline='middle',
                 text_align='center', source=sourcesl)
        tex = ['\u0025']
        sourcespct = ColumnDataSource(data=dict(tex=tex))
        psl.text(54, nby, text='tex',
                 alpha=0.6667, text_font_size='12pt', text_baseline='middle',
                 text_align='center', source=sourcespct)

enter image description hereenter image description here

***更新了***最后测试的代码:一些元素如6(?)不显示 这里是用伪代码测试:

^{pr2}$

Tags: text元素sourcedatapslsomprtgwlen

热门问题