Ejemplo n.º 1
0
    def doRenderChart(self):
        def genMarkup(chartFigure):
            return self.env.from_string("""
                    <script class="pd_save">
                    if ( !window.Bokeh && !window.autoload){{
                        window.autoload=true;
                        {loadJS}  
                    }}
                    </script>
                    {chartFigure}
                    {{%for message in messages%}}
                        <div>{{{{message}}}}</div>
                    {{%endfor%}}
                """.format(
                chartFigure=chartFigure,
                loadJS=self.getLoadJS())).render(messages=self.messages)

        if BokehBaseDisplay.bokeh_version < (0, 12):
            raise Exception("""
                <div>Incorrect version of Bokeh detected. Expected {0}, got {1}</div>
                <div>Please upgrade by using the following command: <b>!pip install --user --upgrade bokeh</b></div>
            """.format((0, 12), BokehBaseDisplay.bokeh_version))
        clientHasBokeh = self.options.get("nostore_bokeh", "false") == "true"
        if not clientHasBokeh:
            output_notebook(hide_banner=True)
        charts = self.createBokehChart()

        if not isinstance(charts, list):
            charts.add_tools(ResizeTool())
            #bokeh 0.12.5 has a non backward compatible change on the title field. It is now of type Title
            #following line is making sure that we are still working with 0.12.4 and below
            if hasattr(charts, "title") and hasattr(charts.title, "text"):
                charts.title.text = self.options.get("title", "")
            else:
                charts.title = self.options.get("title", "")
            charts.plot_width = int(self.getPreferredOutputWidth() - 10)
            charts.plot_height = int(self.getPreferredOutputHeight() - 10)
            charts.grid.grid_line_alpha = 0.3
            return genMarkup(notebook_div(charts))
        else:
            from bokeh.layouts import gridplot
            ncols = 2
            nrows = len(charts) / 2 + len(charts) % 2

            w = self.getPreferredOutputWidth() / ncols if len(
                charts) > 1 else self.getPreferredOutputWidth()
            h = w * self.getHeightWidthRatio() if len(
                charts) > 1 else self.getPreferredOutputHeight()
            for chart in charts:
                chart.plot_width = int(w - 5)
                chart.plot_height = int(h - 5)

            return genMarkup(
                notebook_div(gridplot(charts, ncols=ncols, nrows=nrows)))
Ejemplo n.º 2
0
    def __init__(self,
                 bokeh_plot,
                 callback,
                 delay=200,
                 timeout=2000,
                 throttle=None,
                 **kwargs):
        self.p = bokeh_plot
        self.callback = callback
        self.kwargs = kwargs
        self.ref = str(uuid.uuid4())
        self.comms_handle = None
        self.delay = delay
        self.timeout = timeout
        if throttle:
            print(
                "Warning: throttle parameter no longer supported; will not be accepted in future versions"
            )

        # Initialize the image and callback
        self.ds, self.renderer = self._init_image()
        callback = self._init_callback()
        self.p.x_range.callback = callback
        self.p.y_range.callback = callback

        # Initialize document
        doc_handler = add_to_document(self.p)
        with doc_handler:
            self.doc = doc_handler._doc
            self.div = notebook_div(self.p, self.ref)
Ejemplo n.º 3
0
    def doRenderChart(self):
        def genMarkup(chartFigure):
            return self.env.from_string("""
                    {0}
                    {{%for message in messages%}}
                        <div>{{{{message}}}}</div>
                    {{%endfor%}}
                """.format(chartFigure)
            ).render(messages=self.messages)

        if BokehBaseDisplay.bokeh_version < (0,12):
            raise Exception("""
                <div>Incorrect version of Bokeh detected. Expected {0}, got {1}</div>
                <div>Please upgrade by using the following command: <b>!pip install --user --upgrade bokeh</b></div>
            """.format((0,12), BokehBaseDisplay.bokeh_version))
        clientHasBokeh = self.options.get("nostore_bokeh", "false") == "true"
        if not clientHasBokeh:          
            output_notebook(hide_banner=True)
        charts = self.createBokehChart()

        if not isinstance(charts, list):
            charts.add_tools(ResizeTool())
            charts.title = self.options.get("title", "")
            charts.plot_width = int(self.getPreferredOutputWidth() - 10 )
            charts.plot_height = int(self.getPreferredOutputHeight() - 10  )
            charts.grid.grid_line_alpha=0.3
            return genMarkup(notebook_div(charts))
        else:
            from bokeh.layouts import gridplot
            ncols = 1
            nrows = 2
            if(len(charts) > nrows):
                ncols = int(len(charts) / nrows)
                if(len(charts) % nrows != 0):
                    ncols = ncols + 1
            
            w = self.getPreferredOutputWidth()/ncols if len(charts) > 1 else self.getPreferredOutputWidth()
            h = self.getPreferredOutputWidth()/nrows if len(charts) > 1 else self.getPreferredOutputWidth()
            for chart in charts:
                chart.plot_width = int(w - 5)
                chart.plot_height = int (h - 5)

            return genMarkup(notebook_div(gridplot(charts, ncols=ncols, nrows=nrows)))
Ejemplo n.º 4
0
    def doRender(self, handlerId):
        clientHasBokeh = self.options.get("nostore_bokeh", "false") == "true"
        if not clientHasBokeh:
            output_notebook(hide_banner=True)
        data = self.entity.getNextData()
        if data is None:
            return

        x = None
        y = None

        if isinstance(data, (list, np.ndarray)):
            x = list(
                range(self.windowSize)
            ) if self.glyphRenderer is None else self.glyphRenderer.data_source.data[
                'x']
            y = data if self.glyphRenderer is None else self._concatArrays(
                self.glyphRenderer.data_source.data['y'], data)
            if len(y) < self.windowSize:
                y = [0] * (self.windowSize - len(y)) + y
            elif len(y) > self.windowSize:
                y = self._delWindowElements(y)
        elif isinstance(data, pandas.core.frame.DataFrame):
            pd = pd.drop(pd.index[[0]])
            #pd.index = list(range(len(pd.index)))
            pd['x'] = list(range(len(pd.index)))
        else:
            x = data[0]
            y = data[1]

        if self.glyphRenderer is None:
            self.glyphRenderer = self.createGlyphRenderer(self.figure, x, y)
        else:
            self.updateGlyphRenderer(self.figure, self.glyphRenderer)

        if self.glyphRenderer is None:
            print("Error: no glyphRenderer found")
            return

        self.glyphRenderer.data_source.data['x'] = x
        self.glyphRenderer.data_source.data['y'] = y

        if not self.handleId:
            self.handleId = make_id()
            if self.figure not in _state.document.roots:
                _state.document.add_root(self.figure)
            target = notebook_div(self.figure, self.handleId)
            from IPython.display import display as ipythonDisplay, HTML, Javascript
            ipythonDisplay(HTML(target))
            self.comms_handle = _CommsHandle(get_comms(self.handleId),
                                             _state.document,
                                             _state.document.to_json())
        else:
            push_notebook(handle=self.comms_handle)
Ejemplo n.º 5
0
    def __init__(self, bokeh_plot, callback, throttle=500, **kwargs):
        self.p = bokeh_plot
        self.callback = callback
        self.kwargs = kwargs
        self.ref = str(uuid.uuid4())
        self.comms_handle = None
        self.throttle = throttle

        # Initialize the image and callback
        self.ds, self.renderer = self._init_image()
        callback = self._init_callback()
        self.p.x_range.callback = callback
        self.p.y_range.callback = callback

        # Initialize document
        doc_handler = add_to_document(self.p)
        with doc_handler:
            self.doc = doc_handler._doc
            self.div = notebook_div(self.p, self.ref)
Ejemplo n.º 6
0
    def __init__(self, bokeh_plot, callback, throttle=500, **kwargs):
        self.p = bokeh_plot
        self.callback = callback
        self.kwargs = kwargs
        self.ref = str(uuid.uuid4())
        self.comms_handle = None
        self.throttle = throttle

        # Initialize the image and callback
        self.ds, self.renderer = self._init_image()
        callback = self._init_callback()
        self.p.x_range.callback = callback
        self.p.y_range.callback = callback

        # Initialize document
        doc_handler = add_to_document(self.p)
        with doc_handler:
            self.doc = doc_handler._doc
            self.div = notebook_div(self.p, self.ref)
Ejemplo n.º 7
0
    def __init__(self, bokeh_plot, callback, delay=200, timeout=2000, throttle=None,
                 **kwargs):
        self.p = bokeh_plot
        self.callback = callback
        self.kwargs = kwargs
        self.ref = str(uuid.uuid4())
        self.comms_handle = None
        self.delay = delay
        self.timeout = timeout
        if throttle:
            print("Warning: throttle parameter no longer supported; will not be accepted in future versions")
            
        # Initialize the image and callback
        self.ds, self.renderer = self._init_image()
        callback = self._init_callback()
        self.p.x_range.callback = callback
        self.p.y_range.callback = callback

        # Initialize document
        doc_handler = add_to_document(self.p)
        with doc_handler:
            self.doc = doc_handler._doc
            self.div = notebook_div(self.p, self.ref)
Ejemplo n.º 8
0
    def doRender(self, handlerId):
        clientHasBokeh = self.options.get("nostore_bokeh", "false") == "true"
        if not clientHasBokeh:
            output_notebook(hide_banner=True)
        data = self.entity.getNextData()
        if data is None:
            return

        x = None
        y = None

        if isinstance(data, (list, np.ndarray)):
            x = list(
                range(self.windowSize)
            ) if self.glyphRenderer is None else self.glyphRenderer.data_source.data[
                'x']
            y = data if self.glyphRenderer is None else self._concatArrays(
                self.glyphRenderer.data_source.data['y'], data)
            if len(y) < self.windowSize:
                y = [0] * (self.windowSize - len(y)) + y
            elif len(y) > self.windowSize:
                y = self._delWindowElements(y)
        elif isinstance(data, pandas.core.frame.DataFrame):
            pd = pd.drop(pd.index[[0]])
            #pd.index = list(range(len(pd.index)))
            pd['x'] = list(range(len(pd.index)))
        else:
            x = data[0]
            y = data[1]

        if self.glyphRenderer is None:
            self.glyphRenderer = self.createGlyphRenderer(self.figure, x, y)
        else:
            self.updateGlyphRenderer(self.figure, self.glyphRenderer)

        if self.glyphRenderer is None:
            print("Error: no glyphRenderer found")
            return

        self.glyphRenderer.data_source.data['x'] = x
        self.glyphRenderer.data_source.data['y'] = y

        if not self.handleId:
            self.handleId = make_id()
            if self.figure not in _state.document.roots:
                _state.document.add_root(self.figure)
            activesStreamingEntities[self.getPrefix()] = self
            target = """
<div pd_refresh_rate="2000">
    <pd_script>
from pixiedust.display.streamingDisplay import *
displayHandler = activesStreamingEntities["{prefix}"]
displayHandler.render()
    </pd_script>
</div>
            <div id="target{prefix}">
            {chart}
            </div>            
            """.format(chart=notebook_div(self.figure, self.handleId),
                       prefix=self.getPrefix())
            from IPython.display import display as ipythonDisplay, HTML
            ipythonDisplay(HTML(target))
            self.comms_handle = _CommsHandle(get_comms(self.handleId),
                                             _state.document,
                                             _state.document.to_json())
        else:
            push_notebook(handle=self.comms_handle)
Ejemplo n.º 9
0
 def _repr_html_(self):
     self.doc = Document()
     for m in self.p.references():
         m._document = None
     self.doc.add_root(self.p)
     return notebook_div(self.p, self.ref)