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

Support Highcharts Python in Panel #53

Open
rdp1414 opened this issue Jun 6, 2023 · 3 comments
Open

Support Highcharts Python in Panel #53

rdp1414 opened this issue Jun 6, 2023 · 3 comments
Assignees
Labels
enhancement New feature or request

Comments

@rdp1414
Copy link

rdp1414 commented Jun 6, 2023

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 rdp1414 added the bug Something isn't working label Jun 6, 2023
@hcpchris hcpchris added enhancement New feature or request and removed bug Something isn't working labels Jun 6, 2023
@IanG-21
Copy link

IanG-21 commented Oct 25, 2023

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

@hcpchris
Copy link
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.

@hcpchris
Copy link
Collaborator

hcpchris commented Nov 8, 2023

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
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants