Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Table finalize hook fix and example #1711

Merged
merged 3 commits into from
Jul 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions examples/reference/features/bokeh/table_hooks_example.ipynb
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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