How to use the bokeh.models.CustomJS function in bokeh

To help you get started, we’ve selected a few bokeh examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github bokeh / bokeh / tests / integration / tools / test_point_draw_tool.py View on Github external
def modify_doc(doc):
        source = ColumnDataSource(dict(x=[1, 2], y=[1, 1]))
        plot = Plot(plot_height=400, plot_width=400, x_range=Range1d(0, 3), y_range=Range1d(0, 3), min_border=0)
        renderer = plot.add_glyph(source, Circle(x='x', y='y'))
        tool = PointDrawTool(renderers=[renderer])
        plot.add_tools(tool)
        plot.toolbar.active_multi = tool
        div = Div(text='False')
        def cb(attr, old, new):
            if cds_data_almost_equal(new, expected):
                div.text = 'True'
        source.on_change('data', cb)
        code = RECORD("matches", "div.text")
        plot.add_tools(CustomAction(callback=CustomJS(args=dict(div=div), code=code)))
        doc.add_root(column(plot, div))
    return modify_doc
github bokeh / bokeh / tests / integration / tools / test_wheel_zoom_tool.py View on Github external
def _make_plot(dimensions="both"):
    source = ColumnDataSource(dict(x=[1, 2], y=[1, 1]))
    plot = Plot(plot_height=400, plot_width=400, x_range=Range1d(0, 1), y_range=Range1d(0, 1), min_border=0)
    plot.add_glyph(source, Rect(x='x', y='y', width=0.9, height=0.9))
    plot.add_tools(WheelZoomTool(dimensions=dimensions))
    code = RECORD("xrstart", "p.x_range.start", final=False) + \
           RECORD("xrend", "p.x_range.end", final=False) + \
           RECORD("yrstart", "p.y_range.start", final=False) + \
           RECORD("yrend", "p.y_range.end")
    plot.add_tools(CustomAction(callback=CustomJS(args=dict(p=plot), code=code)))
    plot.toolbar_sticky = False
    return plot
github bokeh / bokeh / tests / integration / widgets / test_radio_group.py View on Github external
def modify_doc(doc):
            source = ColumnDataSource(dict(x=[1, 2], y=[1, 1], val=["a", "b"]))
            plot = Plot(plot_height=400, plot_width=400, x_range=Range1d(0, 1), y_range=Range1d(0, 1), min_border=0)
            plot.add_glyph(source, Circle(x='x', y='y', size=20))
            plot.add_tools(CustomAction(callback=CustomJS(args=dict(s=source), code=RECORD("data", "s.data"))))
            group = RadioGroup(labels=LABELS, css_classes=["foo"])
            def cb(active):
                source.data['val'] = [active, "b"]
            group.on_click(cb)
            doc.add_root(column(group, plot))
github bokeh / bokeh / tests / integration / tools / test_box_edit_tool.py View on Github external
def _make_plot(dimensions="both", num_objects=0):
    source = ColumnDataSource(dict(x=[1, 2], y=[1, 1], width=[0.5, 0.5], height=[0.5, 0.5]))
    plot = Plot(plot_height=400, plot_width=400, x_range=Range1d(0, 3), y_range=Range1d(0, 3), min_border=0)
    renderer = plot.add_glyph(source, Rect(x='x', y='y', width='width', height='height'))
    tool = BoxEditTool(dimensions=dimensions, num_objects=num_objects, renderers=[renderer])
    plot.add_tools(tool)
    plot.toolbar.active_multi = tool
    code = RECORD("x", "source.data.x", final=False) + \
           RECORD("y", "source.data.y", final=False) + \
           RECORD("width", "source.data.width", final=False) + \
           RECORD("height", "source.data.height")
    plot.add_tools(CustomAction(callback=CustomJS(args=dict(source=source), code=code)))
    plot.toolbar_sticky = False
    return plot
github holoviz / panel / pyviz_panels / util.py View on Github external
def patch_bk_plot(plot, plot_id):
    """
    Patches bokeh CustomJS models with top-level plot_id
    """
    if not plot_id: return
    for js in plot.select({'type': CustomJS}):
        js.code = js.code.replace(plot.ref['id'], plot_id)
github hellobiek / smart_deal_tool / visualization / mmap / main.py View on Github external
df['tind_name'] = '无数据'
        df['find_name'] = '无数据' 
    else:
        csi_df = csi_df.drop('name', axis=1)
        df = pd.merge(csi_df, df, how='inner', on=['code'])
    if df is None or df.empty: return p
    df = df[['code', 'name', 'profit', 'pday', 'sw_industry', 'tind_name', 'find_name']]
    df = df.reset_index(drop = True)
    global msource, isource
    profit_list = df.profit.tolist()
    msource = ColumnDataSource(df)
    color_mapper = LinearColorMapper(palette = "Viridis256", low = min(profit_list), high = max(profit_list))
    p.circle(x = 'pday', y = 'profit', color = transform('profit', color_mapper), size = 5, alpha = 0.6, source = msource)
    color_bar = ColorBar(color_mapper = color_mapper, label_standoff = 12, location = (0,0), title = '强度')
    p.add_layout(color_bar, 'right')
    callback = CustomJS(args=dict(msource = msource, tsource = tsource, mtable = mtable, isource = isource), code="""
            var inds = cb_obj.indices;
            var d1 = msource.data;
            var d2 = tsource.data;
            //var d3 = isource.data;
            var d3 = {};
            var ndata = {};
            var tind_industry = '';
            var find_industry = '';
            var tind_industrys = [];
            var find_industrys = [];
            d2['code'] = [];
            d2['name'] = [];
            d2['pday'] = [];
            d2['profit'] = [];
            d2['sw_industry'] = [];
            d2['tind_name'] = [];
github holoviz / panel / panel / pane.py View on Github external
def _get_model(self, doc, root=None, parent=None, comm=None):
        if root is None:
            return self._get_root(doc, comm)

        model = self.object
        ref = root.ref['id']
        for js in model.select({'type': CustomJS}):
            js.code = js.code.replace(model.ref['id'], ref)

        if model._document and doc is not model._document:
            remove_root(model, doc)

        self._models[ref] = model
        self._link_object(doc, root, parent, comm)
        return model
github BenGutierrez / Meica_Report / bokeh_plot.py View on Github external
available_ICAmaps    = ColumnDataSource(data=dict(urls=ICAmap_paths,cID=cID))
ICAmap_to_display    = ColumnDataSource(data=dict(x=[0], y=[0], w=[int(width)], h=[int(height)], url=[ICAmap_default_path]))
xdr                  = Range1d(start=-(int(width)/2), end=(int(width)/2))
ydr                  = Range1d(start=-(int(height)/2), end=(int(height)/2))
ICAmapFigure         = figure(tools=[],title="ICA maps", x_range=xdr, y_range=ydr,width=1200, height=int((int(height)*9)/10), x_axis_type=None, y_axis_type=None, toolbar_location=None, title_text_font_size ='12pt')
ICAmapImg            = ImageURL(url="url", x="x", y="y", w="w", h="h", anchor="center")
ICAmapFigure.add_glyph(ICAmap_to_display,ICAmapImg)
ICAmapFigure.outline_line_color='#ffffff'

# ==============================================================================

# ==============================================================================
#                   JAVA SCRIPT INTERACTIVITY
 
update_ts = CustomJS(args=dict(timeseries_to_display=timeseries_to_display, 
                               comp_ts=available_timeseries, 
                               ffts_to_display=ffts_to_display, 
                               comp_fft=available_ffts,
                               ICApaths=available_ICAmaps,
                               ICAmap_to_display=ICAmap_to_display), 
       code="""
         var c            = cb_obj.get('selected')['1d'].indices
         
         var data2disp_ts = timeseries_to_display.get('data')
         x2disp_ts        = data2disp_ts['x'];
         y2disp_ts        = data2disp_ts['y'];
         var comp_ts      = comp_ts.get('data');
         ts_x             = comp_ts['x'];
         ts_y             = comp_ts['y'];
         for (i = 0; i < x2disp_ts.length; i++) {
            y2disp_ts[i]  = ts_y[c][i];
         }
github bokeh / bokeh / sphinx / source / docs / user_guide / examples / interaction_callbacks_for_selections_lasso_mean.py View on Github external
from bokeh.plotting import figure, output_file, show

output_file("callback.html")

x = [random() for x in range(500)]
y = [random() for y in range(500)]
color = ["navy"] * len(x)

s = ColumnDataSource(data=dict(x=x, y=y, color=color))
p = figure(plot_width=400, plot_height=400, tools="lasso_select", title="Select Here")
p.circle('x', 'y', color='color', size=8, source=s, alpha=0.4)

s2 = ColumnDataSource(data=dict(x=[0, 1], ym=[0.5, 0.5]))
p.line(x='x', y='ym', color="orange", line_width=5, alpha=0.6, source=s2)

s.selected.js_on_change('indices', CustomJS(args=dict(s=s, s2=s2), code="""
    const inds = s.selected.indices;
    const d = s.data;
    var ym = 0

    if (inds.length == 0)
        return;

    for (var i = 0; i < d['color'].length; i++) {
        d['color'][i] = "navy"
    }
    for (var i = 0; i < inds.length; i++) {
        d['color'][inds[i]] = "firebrick"
        ym += d['y'][inds[i]]
    }

    ym /= inds.length