SHARE
TWEET

Untitled

a guest Jun 18th, 2019 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from random import randint
  2. from datetime import date
  3. from bokeh.models import ColumnDataSource, TableColumn, DateFormatter, DataTable, CustomJS
  4. from bokeh.layouts import column
  5. from bokeh.models.widgets import TextInput
  6. from bokeh.plotting import curdoc
  7.  
  8. source = ColumnDataSource(dict(dates = [date(2014, 3, i + 1) for i in range(20)], downloads = [randint(0, 100) for i in range(20)]))
  9. columns = [TableColumn(field = "dates", title = "Date", formatter = DateFormatter()), TableColumn(field = "downloads", title = "Downloads")]
  10. data_table = DataTable(source = source, columns = columns, width = 400, height = 280, editable = True, reorderable = False)
  11.  
  12. text_row = TextInput(value = None, title = "Row index:", width = 420)
  13. text_column = TextInput(value = None, title = "Column Index:", width = 420)
  14. text_date = TextInput(value = None, title = "Date:", width = 420)
  15. text_downloads = TextInput(value = None, title = "Downloads:", width = 420)
  16. test_cell = TextInput(value = None, title = "Cell Contents:", width = 420)
  17.  
  18. source_code = """
  19. var grid = document.getElementsByClassName('grid-canvas')[0].children;
  20. var row, column = '';
  21.  
  22. for (var i = 0,max = grid.length; i < max; i++){
  23.     if (grid[i].outerHTML.includes('active')){
  24.         row = i;
  25.         for (var j = 0, jmax = grid[i].children.length; j < jmax; j++)
  26.             if(grid[i].children[j].outerHTML.includes('active'))
  27.                 { column = j }
  28.     }
  29. }
  30. text_row.value = String(row);
  31. text_column.value = String(column);
  32. text_date.value = String(new Date(source.data['dates'][row]));
  33. text_downloads.value = String(source.data['downloads'][row]);
  34. test_cell.value = column == 1 ? text_date.value : text_downloads.value; """
  35.  
  36. def py_callback(attr, old, new):
  37.     print(test_cell.value)
  38.     print(text_date.value)
  39.     source.selected.update(indices = [])
  40.  
  41. source.selected.on_change('indices', py_callback)
  42. callback = CustomJS(args = dict(source = source, text_row = text_row, text_column = text_column, text_date = text_date, text_downloads = text_downloads, test_cell = test_cell), code = source_code)
  43. source.selected.js_on_change('indices', callback)
  44. curdoc().add_root(column(data_table, text_row, text_column, text_date, text_downloads, test_cell))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top