Theme for both show() and export_png

Hi, I can use a curdoc theme that affects how charts appears in the browser, but is it possible to get that theme to also apply to charts generated via export_png? At the moment I can’t do both.

Here is some example code:

dates = pd.date_range(“20210101”, periods=6)
df = pd.DataFrame(np.random.randn(6, 2), index=dates, columns=list(“AB”))
df.rename_axis(‘Date’, axis=0, inplace=True)

source = ColumnDataSource(df)
curdoc().theme = Theme(filename=“global_theme.json”)

s1 = figure(width=972, height=589)
s1.title.text = “A”
s1.line(x=‘Date’,
y=‘A’,
source = source,
color=‘green’,
legend_label=‘Overall sentiment’)

s2 = figure(width=972, height=589)
s2.title.text = ‘B’
s2.line(x=‘Date’,
y=‘B’,
source = source,
color=‘green’,
legend_label=‘Sales’)

charts = [s1, s2]

def format(chart):
chart.yaxis.formatter = NumeralTickFormatter(format=“0”)
chart.xaxis.formatter = DatetimeTickFormatter(months="%b %Y")
export_png(chart, filename = +chart.title.text+’.png’)
return chart

s1, s2, = [format(chart) for chart in charts]

show(s1)

And this is the theme json:

{
“attrs”:{
“Axis”: {
“axis_label_text_font”: “Georgia”,
“axis_label_text_font_size”: “15px”,
“major_label_text_font” : “Georgia”,
“major_label_text_font_size” : “15px”
},
“Title”: {
“text_font_style”: “bold”,
“text_font”: “Georgia”,
“text_font_size” : “25px”
},
“Legend”: {
“label_text_font”: “Georgia”,
“label_text_font_size”: “20pt”
}
}
}

Hi @paul_c please edit your post to use code formatting so that the code is intelligible (either with the </> icon on the editing toolbar, or triple backtick ``` fences around the code blocks)

Also please make sure the code is a compete, entire, self-contained Minimal Reproducible Example that can actually be run directly for investigation.

Apologies. Here it is:

import pandas as pd
from bokeh.plotting import figure, curdoc
from bokeh.models import ColumnDataSource
from bokeh.models import DatetimeTickFormatter, NumeralTickFormatter
from bokeh.plotting import figure
from bokeh.io import show
from bokeh.io import export_png
from bokeh.themes import Theme
import numpy as np

dates = pd.date_range("20210101", periods=6)
df = pd.DataFrame(np.random.randn(6, 2), index=dates, columns=list("AB"))
df.rename_axis('Date', axis=0, inplace=True)

source = ColumnDataSource(df)
curdoc().theme = Theme(filename="setting.json")

s1 = figure(width=972, height=589)
s1.title.text = "A"
s1.line(x='Date',
        y='A',
        source = source,
        color='green',
        legend_label='Overall sentiment')

s2 = figure(width=972, height=589)
s2.title.text = 'B'
s2.line(x='Date',
        y='B',
        source = source,
        color='green',
        legend_label='Sales')

charts = [s1, s2]

def format(chart):
    chart.yaxis.formatter = NumeralTickFormatter(format="0")
    chart.xaxis.formatter = DatetimeTickFormatter(months="%b %Y")
    export_png(chart, filename = +chart.title.text+'.png')
    return chart

s1, s2, =  [format(chart) for chart in charts]       

show(s1)

setting.json

{ 
  "attrs":{
    "Axis": {
      "axis_label_text_font": "Georgia",
      "axis_label_text_font_size": "15px",
      "major_label_text_font" : "Georgia",
      "major_label_text_font_size" : "15px"
    },
    "Title": {
      "text_font_style": "bold",
      "text_font": "Georgia",
      "text_font_size" : "22px"
    },
    "Legend": {
      "label_text_font": "Georgia",
      "label_text_font_size": "17pt"
    }
  }
}

@paul_c This appears to be a bug, still an open issue:

At present I can only recommend applying the settings programmatically (i.e without using a theme)

1 Like

OK, thanks the response.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.