Search code examples
pythonbokeh

Background color of bokeh layout


I'm playing around with the Bokeh sliders demo (source code here), and I'm trying to change the background color of the entire page. Though changing the color of the figure is easy using background_fill_color and border_fill_color, the rest of the layout still appears on top of a white background. Is there an attribute I can add to the theme that will allow me to set the color via curdoc().theme?


Solution

  • There's not currently any Python property that would control the HTML background color. HTML and CSS is vast territory, so instead of trying to make a corresponding Python property for every possible style option, Bokeh provides a general mechanism for supplying your own HMTL templates so that any standard familiar CSS can be applied.

    This is most easily accomplished by adding a templates/index.html file to a Directory-style Bokeh App. The template should be Jinja2 template. There are two substitutions required to be defined in the <head>:

    • {{ bokeh_css }}
    • {{ bokeh_js }}

    as well as two required in <body>:

    • {{ plot_div }}
    • {{ plot_script }}

    The app will appear wherever the plot_script appears in the template. Apart from this, you can apply whatever HTML and CSS you need. You can see a concrete example here:

    https://github.com/bokeh/bokeh/blob/master/examples/app/crossfilter

    A boiled down template that changes the page background might look like this:

    <!DOCTYPE html>
    <html lang="en">
        <head>
            <style>
                body { background: #2F2F2F; }
            </style>
            <meta charset="utf-8">
            {{ bokeh_css }}
            {{ bokeh_js }}
        </head>
        <body>
            {{ plot_div|indent(8) }}
            {{ plot_script|indent(8) }}
        </body>
    </html>