Skip to content

Commit

Permalink
Table finalize hook fix and example (#1711)
Browse files Browse the repository at this point in the history
* #1708

* Minor tweak to notebook

* add additional URL
* committing mostly to test failing Travis build

* move file to new directory per @jlstevens suggestion
  • Loading branch information
jordansamuels authored and jlstevens committed Jul 14, 2017
1 parent 1f410b9 commit 3035c7d
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
77 changes: 77 additions & 0 deletions examples/reference/features/bokeh/table_hooks_example.ipynb
@@ -0,0 +1,77 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import holoviews as hv\n",
"from bokeh.models import HTMLTemplateFormatter\n",
"hv.extension('bokeh')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Declare Data"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"name = ['homepage', 'github', 'chat']\n",
"link = ['http://holoviews.org', 'https://github.com/ioam/holoviews', 'https://gitter.im/ioam/holoviews']\n",
"table = hv.Table({'Name':name, 'Link':link}, vdims = ['Name', 'Link'], kdims=[])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Plot"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%opts Table [width=500]\n",
"def apply_format(plot, element):\n",
" plot.handles['plot'].columns[1].formatter=HTMLTemplateFormatter(template='<a href=\"<%= value %>\"><%= value %></a>')\n",
"\n",
"table.opts(plot=dict(finalize_hooks=[apply_format]))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
17 changes: 17 additions & 0 deletions holoviews/plotting/bokeh/tabular.py
Expand Up @@ -17,6 +17,11 @@ class TablePlot(BokehPlot, GenericElementPlot):
style_opts = ['row_headers', 'selectable', 'editable',
'sortable', 'fit_columns', 'width', 'height']

finalize_hooks = param.HookList(default=[], doc="""
Optional list of hooks called when finalizing a column.
The hook is passed the plot object and the displayed
object, and other plotting handles can be accessed via plot.handles.""")

_update_handles = ['source', 'glyph']

def __init__(self, element, plot=None, **params):
Expand All @@ -27,6 +32,17 @@ def __init__(self, element, plot=None, **params):
self.callbacks = [] # Callback support on tables not implemented


def _execute_hooks(self, element):
"""
Executes finalize hooks
"""
for hook in self.finalize_hooks:
try:
hook(self, element)
except Exception as e:
self.warning("Plotting hook %r could not be applied:\n\n %s" % (hook, e))


def get_data(self, element, ranges=None, empty=False):
dims = element.dimensions()
data = {d: np.array([]) if empty else element.dimension_values(d)
Expand Down Expand Up @@ -59,6 +75,7 @@ def initialize_plot(self, ranges=None, plot=None, plots=None, source=None):
width=self.width, **properties)
self.handles['plot'] = table
self.handles['glyph_renderer'] = table
self._execute_hooks(element)
self.drawn = True

return table
Expand Down

0 comments on commit 3035c7d

Please sign in to comment.