Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Bokeh HTML visualizations to show on WordPress?

I have a Bokeh dashboard of visualization that I created in Python. I have the HTML file saved. When I open the file with my browser, it brings up my graphs (like it is supposed to!). However, I am trying to display this dashboard on my WordPress site. Pasting the HTML code into the HTML page option on WordPress doesn't work. Does anyone know the trick for this, or can point me to the correct resources?

like image 852
jdbul33 Avatar asked Mar 06 '23 16:03

jdbul33


2 Answers

You can make a shortcode to include the code. You can either save the html to a file and get the contents of it, or just copy & paste the code into your shortcode. Add something like this to your functions.php file:

function get_my_bokeh() {
    return file_get_contents( "http://www.example.com/path/to/bokeh.html" );
}
add_shortcode( 'print_bokeh', 'get_my_bokeh' );

/* OR */

function get_my_bokeh() { 
    ob_start(); ?>

    <!-- paste your HTML code here --> 

    <?php return ob_get_clean();
}
add_shortcode( 'print_bokeh', 'get_my_bokeh' );

Then add the shortcode [print_bokeh] to the text editor.

In both examples above you should clean out all of the <html> <head> & <body> elements that make up a whole HTML document and just have the content.

like image 141
Peter HvD Avatar answered Mar 23 '23 06:03

Peter HvD


For those who are less familiar with php than with bokeh, let me expand Peter HvD's answer to get a reusable solution. The code in your theme's functions.php should be

function include_bokeh( $atts = array() ) {
    extract(shortcode_atts(array(
      'path' => '' 
    ), $atts));

    return file_get_contents( $path );
}
add_shortcode( 'show_bokeh', 'include_bokeh' );

And you call it in WordPress editor as

[show_bokeh path="http://www.example.com/path/to/bokeh.html"]
like image 36
Jan Vorac Avatar answered Mar 23 '23 07:03

Jan Vorac