Search code examples
bokeh

Can I get Bokeh to NOT generate javascript all on one line?


I've never worked with Bokeh before, so apologies if this question doesn't make sense. I have a plot that someone else generated using Bokeh and it has a huge JSON object (docs_json) that is all on the same line. I need to import this plot into a WordPress site which has a line length limit. Is there any way I can get Bokeh to generate that JSON in a prettified form rather than squishing the whole thing onto one line?

I'm in a situation where I'm working with 100s of plots being generated continuously, so I'd prefer not to have to manually touch each plot after it is generated.


Solution

  • Yes, the simplest way is to use the BOKEH_PRETTY environment variable when running any Bokeh code, e.g.

    BOKEH_PRETTY=true python iris.py
    

    Results in HTML output with embedded JSON that looks like:

    <script type="application/json" id="4074acb8-0b70-4591-8d43-99873a9e1bc4">
      {
        "9d745210-1f6a-4c22-b8ca-c3d2b3829a8f": {
          "roots": {
            "references": [
              {
                "attributes": {
                  "bottom_units": "screen",
                  "fill_alpha": {
                    "value": 0.5
                  },
    
                  ...
    
            ],
            "root_ids": [
              "ce2c1a38-e3e5-4155-9a80-6860dc284dbc"
            ]
          },
          "title": "Bokeh Application",
          "version": "0.12.15dev1"
        }
      }
    </script>
    

    All of Bokeh's settings and their associated environment variables are documented in the reference guide:

    https://docs.bokeh.org/en/latest/docs/reference/settings.html