From ba25047893c73c52253a09d12ea73fc0610f823b Mon Sep 17 00:00:00 2001 From: Arne Date: Sat, 12 Oct 2019 14:58:51 +0200 Subject: [PATCH] wrap to_plotly_json and turn datetimes into strings (#688) --- panel/pane/plotly.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/panel/pane/plotly.py b/panel/pane/plotly.py index 97c55af40c..83c3356a9c 100644 --- a/panel/pane/plotly.py +++ b/panel/pane/plotly.py @@ -13,6 +13,7 @@ import param from .base import PaneBase +from ..util import isdatetime class Plotly(PaneBase): @@ -153,6 +154,21 @@ def _update_data_sources(self, cds, trace): return update_sources + @staticmethod + def _plotly_json_wrapper(fig): + """Wraps around to_plotly_json and applies necessary fixes. + + For #382: Map datetime elements to strings. + """ + json = fig.to_plotly_json() + data = json['data'] + + for idx in range(len(data)): + for key in data[idx]: + if isdatetime(data[idx][key]): + data[idx][key].astype(str) + return json + def _get_model(self, doc, root=None, parent=None, comm=None): """ Should return the bokeh model to be rendered. @@ -172,7 +188,7 @@ def _get_model(self, doc, root=None, parent=None, comm=None): json, sources = {}, [] else: fig = self._to_figure(self.object) - json = fig.to_plotly_json() + json = self._plotly_json_wrapper(fig) sources = Plotly._get_sources(json) model = PlotlyPlot(data=json.get('data', []), layout=json.get('layout', {}), @@ -209,7 +225,7 @@ def _update(self, model): return fig = self._to_figure(self.object) - json = fig.to_plotly_json() + json = self._plotly_json_wrapper(fig) traces = json['data'] new_sources = []