更改散景表行数

时间:2019-04-23 20:32:25

标签: bokeh

下面是散景表的工作示例,其中填充了散点图中的选择。

表首次初始化后,总是显示很多行(太多或太少)。

有没有一种方法可以使行数动态变化以适应所选记录的数量?

谢谢

import numpy as np
import pandas as pd
from bokeh.layouts import  row
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure, curdoc, show
from bokeh.models.widgets import DataTable, DateFormatter, TableColumn


#Plotting points on chart.
initial_df = pd.DataFrame(np.random.randint(0,100,size=(500, 2)), 
                          columns=["X","Y"], 
                          index=[str(i) for i in range(1,500+1)])

pointchart=figure(plot_width=800, plot_height=700, 
                  tools=['lasso_select','box_select'],
                  title="Points for selection")

pointchart_source= ColumnDataSource(initial_df )
pointchart_glyph= pointchart.circle("X","Y",source=pointchart_source,size=3.5)


#Source for table
source_df=initial_df 
source_df['ID']=source_df.index

#Making initial table source from dataframe. The table will always have this number of rows.
initial_source_for_table = ColumnDataSource(source_df)

columns =  [TableColumn(field='ID', title="Col1"),
           TableColumn(field="X", title="Col2"), 
           TableColumn(field="Y", title="Col3")]

global data_table #lets you access it in the callback.
data_table = DataTable(source=initial_source_for_table, columns=columns, width=800, height=400)


def on_selection_change(attr, old, new):
    newdataframe= pd.DataFrame(pointchart_source.data).loc[new]
    newdataframe['ID']=newdataframe.index
    newsource=ColumnDataSource(newdataframe[['ID',"X","Y"]].dropna(how='all'))


    data_table.source=newsource
    data_table.width=500
    data_table.height=500


pointchart_glyph.data_source.selected.on_change('indices',on_selection_change) 


#Show
layout=row(pointchart,data_table)
curdoc().add_root(layout)
!powershell -command {'bokeh serve --show Test_Table.ipynb'}

1 个答案:

答案 0 :(得分:2)

我没有Jupyter Notebook,但此示例应为您提供帮助。 只需计算所选点的数量,然后使用options(guiToolkit = "RGtk2") win <- gwindow("TOOLS FOR QUANTITATIVE GENETIC ANALYSES",width = 502, height=370,expand=NULL,fill=NULL) getToolkitWidget(win)$modifyBg(GtkStateType["normal"], "white") ## it's ok #### gmenu mbl <- list() mbl$File$"Allelic Genotyping Merge"$handler = Merge_Geno mbl$File$"Allelic Genotyping Merge"$icon = "open" mb <- gmenu(mbl, container=win) getToolkitWidget(mb)$modifyBg(GtkStateType["normal"], "white") ## it's ok for the menu band getToolkitWidget(mb)$getChildren()[[1]]$modifyBg(GtkStateType["normal"], "white") ## doesn't work on the items of gmenu #### gbutton gcb <- gbutton("Download Genetic Grp",handler=Download_GrpGen,expand=FALSE,fill=FALSE,cont=win) getToolkitWidget(gcb)$modifyBg(GtkStateType["normal"], "white") ## doesn't work getToolkitWidget(gcb)$modifyBase(GtkStateType["normal"], "white") ## doesn't work getToolkitWidget(gcb)$getChildren()[[1]]$modifyBg(GtkStateType["normal"], "white") ## doesn't work #### gnotebook nb <- gnotebook(cont=win,expand=FALSE,fill=FALSE) nBKIN <- ggroup(label="KINSHIPS",horizontal=FALSE,cont=nb,expand=FALSE,fill=FALSE) gcb0KIN <- gcheckboxgroup(NameKin,cont=nBKIN,checked = FALSE,expand=FALSE,fill=FALSE) getToolkitWidget(nb)$modifyBg(GtkStateType["normal"], "white") ## it's ok for gcheckboxgroup getToolkitWidget(nBKIN )$modifyBg(GtkStateType["normal"], "white") ## doesn't work for background of label "DISTANCE" getToolkitWidget(nBKIN )$"parent"$modifyBg(GtkStateType["normal"], "white") ## doesn't work for background of label "DISTANCE" getToolkitWidget(nBDIST)$getChildren()[[1]]$modifyBg(GtkStateType["normal"], "white") ## doesn't work for background of label "DISTANCE" getToolkitWidget(nb)$getChildren()[[1]]$modifyBg(GtkStateType["normal"], "white") ## doesn't work for background of label "DISTANCE" 更改表行的数量。使用以下代码运行代码:table.height = number_points * 25

bokeh serve --show app.py

顺便说一句:您不应在回调中替换整个ColumnDataSource,而应像在我的示例中那样为它分配一个新数据,即:

from bokeh.io import curdoc, show
from bokeh.layouts import widgetbox
from bokeh.models import ColumnDataSource, Slider, DataTable, TableColumn

max_i = 200
init_i = 6

def get_square(n):
    return dict(x = list(range(n)), y = [x ** 2 for x in range(n)])

source = ColumnDataSource(get_square(init_i))
columns = [
    TableColumn(field = "x", title = "x"),
    TableColumn(field = "y", title = "x**2"),
]

table = DataTable(source = source, columns = columns, width = 320)
slider = Slider(start = 1, end = max_i, value = init_i, step = 1, title = "i", width = 300)

def update_data(attrname, old, new):
    i = slider.value
    table.source.data = get_square(i)
    table.height = i * 25 + 25
    table.update()

slider.on_change('value', update_data)

layout = widgetbox(slider, table)
curdoc().add_root(layout)

代替:

table.source.data = new_data