Skip to content

Support Highcharts Python in Panel #53

Open

Description

@rdp1414

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.

Activity

  1. added
    bug
    on Jun 6, 2023
  2. added
    enhancement
    and removed
    bug
    on Jun 6, 2023
  3. IanG-21 commented on Oct 25, 2023

    @IanG-21

    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!).

  4. hcpchris commented on Oct 27, 2023

    @hcpchris
    Collaborator

    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.

  5. hcpchris commented on Nov 8, 2023

    @hcpchris
    Collaborator

    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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions