Embedding with custom Bokeh Theme

I’m trying to use a custom Bokeh Theme with my Panel app, that I later convert to a standalone HTML, but couldn’t find any public API that would help me do so.

My best idea was to overwrite theme on the _Document type:

fig = figure()
fig.line(x=[1, 2, 3], y=[1, 1, 2])
old_theme = pn.viewable._Document.theme
pn.viewable._Document.theme = themes.Theme(
    json={"attrs": {'Line': {'line_width': 10, 'line_cap': 'round'}}}
pn.viewable._Document.theme = old_theme

It works, but is ugly and involves accessing “private” attribute and making module-wide change. Am I missing something or should I post an enhancement idea?

I know HoloViews opts.defaults is an option, but still would like to work with Bokeh directly in some cases.

Hi @chmielcode

Welcome to the community.

You can use the Template to define a custom template. See https://panel.holoviz.org/user_guide/Templates.html

For an example notebook see for example this PR https://github.com/holoviz/panel/pull/1254 and associated notebook https://github.com/holoviz/panel/blob/90f28b002fb9e40324597af927694c5d6617882a/examples/gallery/templates/material_template.ipynb

Well, hello :slight_smile:
Thank you for your reply. Templates combined with custom CSS/JS are a powerful tool and your work looks truly great. I’m aware this functionality is available since I’ve read Panel’s and Param’s user guides and code before posting a question. My thinking was that Bokeh is an internal part of this ecosystem, so there must be a function/attribute/ctxmanager I’m missing allowing me to do, what I’m looking for.

I’m going to analyze your work and try to find a generic way to apply a custom theme to my Panel apps. Before I discovered Panel I’ve maintained my own, but always missing something, panel-like library and it took me a lot of time, but also allowed me to conform to brand requirements. Personally I like crunching data, but reality is that visualization is equally (if not more) important, so I keep my fingers crossed that this part of every analytics/data-scientist’s work is also taken into account by Panel developers :slight_smile:

1 Like

Personally I’m spending a lot of time of improving templates, layout and theming for Panel because I believe that is the missing thing compared to the alternative python frameworks.

I know @philippjfr is has some general templates on the roadmap. And he is also working on enabling the use of templates within an app. I believe that will be powerfull as well. See https://github.com/holoviz/panel/pull/1343