Search code examples
python-3.xrobotframeworkbokeh

How to include bokeh chart into robot framework html report


I try to include a bokeh chart into Robot Framework HTMl report.

I use logger.info() from from robot.api import logger to inject the code into the report. I can include a simple html tag like , but the trick does not work with bokeh.

from robot.api import logger
from bokeh.plotting import figure

class customreport(object):
    def my_test_keyword(self, message):
        p = figure()
        p.circle([1, 2, 3], [4, 5, 6])
        script, div = bokeh.embed.components(p)
        logger.info(script, html=True)
        logger.info(div, html=True)

        

The script is in the code, but won't load.


Solution

  • Due to being curious, i made a small proof of concept to just check if and how this is possible.

    Here's a small robot suite:

    *** Settings ***
    Library    bkdemo
    
    *** Test Cases ***
    Bokeh Demo
      Generate Figure
    

    And keyword library bkdemo.py

    from robot.api import logger
    import bokeh
    from bokeh.plotting import figure
    
    def generate_figure():
        p = figure()
        p.circle([1, 2, 3], [4, 5, 6])
        script, div = bokeh.embed.components(p)
    
        # Get Rid of the script tags
        script = script.replace('<script type="text/javascript">','')
        script = script.replace('</script>','')
    
        # Where to load the bokeh
        url = "https://cdn.bokeh.org/bokeh/release/bokeh-3.3.1.min.js"
        # use jquery getScript to get a callback once bokeh is loaded and then
        # run the script from bokeh.embed.components
        force_load_bokeh = f"$.getScript('{url}', function(){{{script}}});"
        logger.info(f'<script type="text/javascript">{force_load_bokeh}</script>', html=True)
    
        # and then print the location where bokeh does rendering
        logger.info(div, html=True)
    

    Now, if robot framework would have possibility to customize its html templates, and as mentioned in comments, most of the "hacks" (eg, use of jquery and stripping returned script) would be unnecessary when one could just add the script tag to load or embed bokeh into top level html ..

    But atleast i can see the chart being rendered now ;)