How to update DataTable with dynamic number of rows

Hi, I have question about update DataTables.

Attached is an example. (run by bokeh serve --show table_example.py)
The expected result is just a table of x vs x^2. x is controlled by the slider.

The table content is updated as expected. However, the number of rows is fixed to the initial value (6)
Ideally the table can change with the number of rows… Now it won’t show additional rows is x goes beyond the initial value.

Is there anyway I can get dynamic number of rows? Or at least, is there a way to fix the number of rows in the beginning?

Thank you !

table_example.py (748 Bytes)

Do you mean something like this (added table.height)?

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)
i_slider = Slider(start = 1, end = max_i, value = init_i, step = 1, title = “i”, width = 300)

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

i_slider.on_change(‘value’, update_data)
layout = widgetbox(i_slider, table)
curdoc().add_root(layout)

``

···

On Friday, November 2, 2018 at 10:49:07 PM UTC+1, Yizhou Zhu wrote:

Hi, I have question about update DataTables.

Attached is an example. (run by bokeh serve --show table_example.py)
The expected result is just a table of x vs x^2. x is controlled by the slider.

The table content is updated as expected. However, the number of rows is fixed to the initial value (6)
Ideally the table can change with the number of rows… Now it won’t show additional rows is x goes beyond the initial value.

Is there anyway I can get dynamic number of rows? Or at least, is there a way to fix the number of rows in the beginning?

Thank you !

Hi,

I'll reiterate the comment that the table height is fixed and determined by table.height, so you can either dynamically update it as the other user suggested, or simply give it a "big enough" height to begin with.

However, I did want to point out something else. You should avoid creating new ColumnDataSource objets. Instead, best practice is much better to update the .data attribute of existing ones:

    def update_data(attrname, old, new):
        i = i_slider.value
        table.source.data = get_square(i)

Thanks,

Bryan

···

On Nov 22, 2018, at 13:18, [email protected] wrote:

Do you mean something like this (added table.height)?

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)
i_slider = Slider(start = 1, end = max_i, value = init_i, step = 1, title = "i", width = 300)

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

i_slider.on_change('value', update_data)
layout = widgetbox(i_slider, table)
curdoc().add_root(layout)

On Friday, November 2, 2018 at 10:49:07 PM UTC+1, Yizhou Zhu wrote:
Hi, I have question about update DataTables.

Attached is an example. (run by bokeh serve --show table_example.py)
The expected result is just a table of x vs x^2. x is controlled by the slider.
The table content is updated as expected. However, the number of rows is fixed to the initial value (6)
Ideally the table can change with the number of rows... Now it won't show additional rows is x goes beyond the initial value.

Is there anyway I can get dynamic number of rows? Or at least, is there a way to fix the number of rows in the beginning?

Thank you !

--
You received this message because you are subscribed to the Google Groups "Bokeh Discussion - Public" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/bokeh/4e58d26a-e7c8-4ad5-bddc-aeca5b077975%40continuum.io\.
For more options, visit https://groups.google.com/a/continuum.io/d/optout\.