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

Unicode fixes for SelectionWidget #376

Merged
merged 1 commit into from
Dec 20, 2015
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
21 changes: 18 additions & 3 deletions holoviews/plotting/widgets/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import unicode_literals

import os, uuid, json, math

import param
Expand All @@ -18,6 +20,17 @@ def isnumeric(val):
except:
return False


def escape_list(vals):
"""
Escapes a list of values to a string, converting to
unicode for safety.
"""
vals = [safe_unicode(v) if isinstance(v, basestring) else str(v)
for v in vals if v is not None]
return "['" + "', '".join(vals) + "']"


subdirs = [p[0] for p in os.walk(os.path.join(os.path.split(__file__)[0], '..'))]

class NdWidget(param.Parameterized):
Expand Down Expand Up @@ -244,6 +257,7 @@ def get_widgets(self):
if not isinstance(dim_range, int) or int_type:
step = 10**(round(math.log10(dim_range))-3)
init_dim_vals.append(dim_vals[0])
dim_vals = escape_list(dim_vals)
else:
if next_vals:
dim_vals = next_vals[init_dim_vals[idx-1]]
Expand All @@ -266,7 +280,7 @@ def get_widgets(self):
widget_type = 'dropdown'
visible = len(dim_vals) > 1
init_dim_vals.append(dim_vals[0])
dim_vals = repr([v for v in dim_vals if v is not None])
dim_vals = escape_list(dim_vals)
dim_str = safe_unicode(dim.name)
visibility = 'visibility: visible' if visible else 'visibility: hidden; height: 0;'
widget_data = dict(dim=dimension_sanitizer(dim_str), dim_label=dim_str,
Expand All @@ -275,6 +289,7 @@ def get_widgets(self):
next_vals=next_vals)
widgets.append(widget_data)
dimensions.append(dim_str)
init_dim_vals = escape_list(init_dim_vals)
return widgets, dimensions, init_dim_vals


Expand All @@ -283,8 +298,8 @@ def get_key_data(self):
key_data = OrderedDict()
for i, k in enumerate(self.mock_obj.data.keys()):
key = [("%.1f" % v if v % 1 == 0 else "%.10f" % v)
if isnumeric(v) else v for v in k]
key = str(tuple(key))
if isnumeric(v) else safe_unicode(v) for v in k]
key = "('" + "', '".join(key) + ("',)" if len(key) == 1 else "')")
key_data[key] = i
return json.dumps(key_data)

Expand Down
2 changes: 1 addition & 1 deletion holoviews/plotting/widgets/jsslider.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@
</script>
{% elif widget_data['type']=='dropdown' %}
<div class="form-group control-group" style='{{ widget_data['visibility'] }}'>
<label for="textInput{{ id }}_{{ widget_data['dim'] }}"><strong>{{ widget_data['dim'] }}:</strong></label>
<label for="textInput{{ id }}_{{ widget_data['dim'] }}"><strong>{{ widget_data['dim_label'] }}:</strong></label>
<select class="holoselect form-control" id="_anim_widget{{ id }}_{{ widget_data['dim'] }}" >
</select>
</div>
Expand Down