散景主题与Yaml文件 [英] Bokeh Themes with yaml file

查看:101
本文介绍了散景主题与Yaml文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python和Bokeh的新手.尝试使用单独的yaml文件将简单主题应用于简单的折线图.不确定如何执行此操作.如果这是我Juypter笔记本中的手机:

Am new to Python and Bokeh. Trying to apply a simple theme to a simple line graph using a separate yaml file. Unsure as to how to do this. If this is my cell in my Juypter notebook:

import pandas as pd
from bokeh.plotting import show, figure, output_notebook
from bokeh.palettes import Spectral
from bokeh.themes import Theme
from bokeh.document import Document

output_notebook()

x_f = [1.5, 2, 9]
y_f = [3, 3, 3.1]

p = figure(plot_width=400, plot_height=400)
p.line(x_f, y_f, line_width=3, color=Spectral[4][0])

show(p)


这是一个单独的文件lp.yaml


And this is a separate file, lp.yaml

### Default attribute of Bokeh line chart
attrs:
    Figure:
        background_fill_color: "whitesmoke"
        background_fill_alpha: 0.5

如何使用.yaml文件中的属性覆盖默认主题?

How do I override the default themes with the attributes in the .yaml file?

推荐答案

从代码中,您可以让您的应用从默认位置读取主题(如@tony所建议),或者您可以存储多个自定义主题YAML格式的主题文件,并使用以下内容进行阅读:

From your code, you can either have your app reading a theme from the default location (as suggested by @tony) or if you like you can store several custom themes files in YAML format and read them with:

curdoc().theme = Theme(filename='path_to_theme.yaml)

假设目录结构如下:

myapp
   |
   +---main.py
   +---theme.yaml
   +---templates
        +---index.html
   +---static
        +---css
            +---styles.css
        +---themes
            +---test_theme.yaml

path_to_theme.yaml可能是: os.path.join(os.path.dirname(__file__),'static', 'themes', 'test_theme.yaml')

如果您想提供几种布局,则可以添加一个小部件以在主题之间切换,例如:

If you like to offer several layouts, you can add a widget to switch between themes, for example:

# mybokeh_themes.py

from bokeh.plotting import figure, curdoc
from bokeh.palettes import Spectral
from bokeh.themes import Theme
from bokeh.document import Document

# import some layout elements, a select widget, and the built_in_themes
from bokeh.layouts import column
from bokeh.models import Select
from bokeh.themes import Theme, built_in_themes

def switch_theme(value, old, new):
    curdoc().theme = new


x_f = [1.5, 2, 9]
y_f = [3, 3, 3.1]

p = figure(plot_width=400, plot_height=400)
p.line(x_f, y_f, line_width=3, color=Spectral[4][0])


theme_select = Select(title='Theme', options=['caliber',
                                              'dark_minimal', 
                                              'light_minimal', 
                                              'night_sky', 
                                              'contrast'])
theme_select.on_change('value', switch_theme)

curdoc().add_root(column(theme_select, p))

您可以使用以下命令对其进行测试:bokeh serve mybokeh_themes.py

you can test it with: bokeh serve mybokeh_themes.py

这篇关于散景主题与Yaml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆