highcharts-core icon indicating copy to clipboard operation
highcharts-core copied to clipboard

Support `Highcharts Python` in `Panel`

Open rdp1414 opened this issue 1 year ago • 3 comments

This is a feature request for supporting Highcharts Python in Panel.

Your Environment:

  • OS: Windows, Jupyter Lab (notebook).
  • Python Version: 3.10.4
  • Highcharts Version: Installed Highcharts Python using pip install highcharts-core command.

rdp1414 avatar Jun 06 '23 08:06 rdp1414

This is relatively simple to achieve by extending the Panel ReactiveHTML class with the latest version of Panel. The trickier part is that Panel now uses a Shadow DOM, so needs some JS to work, something like this should work:

from panel.reactive import ReactiveHTML
import param

class PanelHighchart(ReactiveHTML):
    chart_js: dict = param.String()

    _template: str = """<div id="phighchart"></div>"""
    _scripts: dict = {}
    __javascript__ = ["https://code.highcharts.com/highcharts.js", "https://code.highcharts.com/modules/exporting.js"]
    prefix: str = "document.addEventListener('DOCContentLoaded', function() {"
    suffix: str = "});"
    
    def __init__(self, **params):
        super().__init__(**params)
        self._scripts = self._generate_highchart(self.chart_js)

    def _generate_highchart(self, chart_js: str):
        # remove the prefix and suffix
        chart_js_literal: str = chart_js[len(PanelHighchart.prefix):len(chart_js)-len(PanelHighchart.suffix)]
        
        # and replace null with the phighcharts div, which we can reference in the scripts as if the variable already exists
        chart_js_literal = chart_js_literal.replace("null,", "phighchart,")
        
        init: str = """console.log("Initing...");
if (phighchart !== null) {
    """ + chart_js_literal + """
} else {
    console.log("Not found...");
}"""
        render: str = f"""console.log("Rendering...");
self.init();"""
        scripts: dict = {
            "init": init,
            "render": render
        }
        return scripts

and then call with pnl_chart: PanelHighchart = PanelHighchart(chart_js=chart.to_js_literal()).

If we could optionally drop the JS event listener wrapper, that would solve having to remove it manually. The rest is just setting the HTML element to the element reference itself for highcharts to render to (and some debugging comments that can be removed!).

IanG-21 avatar Oct 25 '23 16:10 IanG-21

Wow, @IanG-21 ! Thanks! This is super helpful, looks great, and I'll be taking a closer look at it shortly as I look at the integration with Panel. In the meantime, if you'd like to clean-up and file a PR, that would be awesome - but if not, then no worries: I'll be experimenting with this a bit in preparation for hopefully the next minor release.

hcpchris avatar Oct 27 '23 21:10 hcpchris

Just by way of FYI, PR #131 has added a flag to remove the event listener prefix. While it hasn't been released yet, that PR has been merged into the release branch for the next Highcharts for Core minor version (v.1.6, in branch rc-v.1.6).

hcpchris avatar Nov 08 '23 03:11 hcpchris