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

Allow providing custom Download labels #130

Merged
merged 1 commit into from
Apr 14, 2021
Merged
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
23 changes: 17 additions & 6 deletions lumen/target.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
from .views import View


DOWNLOAD_FORMATS = ['csv', 'xlsx', 'json', 'parquet']


class Facet(param.Parameterized):

by = param.List(default=[], class_=FacetFilter, doc="""
Expand Down Expand Up @@ -84,10 +87,13 @@ class Download(pn.viewable.Viewer):
in a variety of formats via the Dashboard UI.
"""

labels = param.Dict(default={}, doc="""
A dictionary to override the Button label for each table.""")

filters = param.List(class_=Filter, doc="""
A list of filters to be rendered.""")

format = param.Selector(default=None, objects=['csv', 'xlsx', 'json', 'parquet'], doc="""
format = param.ObjectSelector(default=None, objects=DOWNLOAD_FORMATS, doc="""
The format to download the data in.""")

kwargs = param.Dict(default={}, doc="""
Expand All @@ -103,15 +109,17 @@ class Download(pn.viewable.Viewer):
def __init__(self, **params):
super().__init__(**params)
default_table = self.tables[0] if self.tables else None
button_params = {'filename': f'{default_table}.{self.format}'}
if default_table in self.labels:
button_params['label'] = self.labels[default_table]
self._select_download = pn.widgets.Select(
name='Select table to download', options=self.tables, value=default_table,
sizing_mode='stretch_width'
)
self._select_download.param.watch(self._update_filename, 'value')
self._select_download.param.watch(self._update_button, 'value')
self._download_button = pn.widgets.FileDownload(
callback=self._table_data, align='end',
filename=f'{default_table}.{self.format}',
sizing_mode='stretch_width'
sizing_mode='stretch_width', **button_params
)
self._layout = pn.Column(
pn.pane.Markdown('### Download tables', margin=(0, 5, -10, 5)),
Expand All @@ -125,8 +133,11 @@ def __init__(self, **params):
def __panel__(self):
return self._layout

def _update_filename(self, event):
self._download_button.filename = f'{event.new}.{self.format}'
def _update_button(self, event):
params = {'filename': f'{event.new}.{self.format}'}
if event.new in self.labels:
params['label'] = self.labels[event.new]
self._download_button.param.set_param(**params)

def __bool__(self):
return self.format is not None
Expand Down