gpt4 book ai didi

python - 更新 curdoc() 后从 Bokeh Fig 中删除 Label 布局

转载 作者:太空宇宙 更新时间:2023-11-03 14:05:41 24 4
gpt4 key购买 nike

我有一个多面板 Bokeh 图,使用 Bokeh 服务器(Bokeh 0.12.14)渲染。我正在运行一个带有按钮小部件 (on_click()) 的函数,该函数修改我的 gridplot 布局、添加一些标签并导出自定义的 .png 图像。在函数中,我使用 curdoc().hold() ,然后使用 curdoc().clear,对图形进行修改并构建一个新的 gridplot。然后,我将 curdoc().add_root(gridplot) 与修改后的 gridplot 布局一起使用,并运行 export_png(curdoc())

导出后,我再次运行 curdoc().clear(),重建原始 gridplot 布局,并使用 curdoc() 恢复原始文档.add_root(gridplot)。最后以 curdoc().unhold() 结束。

一切正常,除了我不知道如何删除为 .png 导出创建的自定义标签。我添加了几个标签,例如:

  season_label = Label(x=start_display, y=35, x_units='data', y_units='data',
text= '%s - %s' % (starting_year, starting_year+1),
render_mode='css', text_font_style='bold',
text_font_size='14px', text_baseline='middle', text_align='left')

newsnow_extra_fig.add_layout(season_label)

有没有办法从图中删除布局?

最佳答案

作为一种解决方法,我发现可以将文本注释添加为 Div widgets ,而不是 Labels 。然后,我使用 gridplot() 在预期图形的上方或下方添加小部件。例如:

div_1 = bkm.widgets.Div(text="""My text annotation {} - {}""".format(starting_year, starting_year+1),
width=1400, height=27, style={'background-color':'white'})

div_wb_1 = widgetbox(div_1)

grid = gridplot(div_wb_1,
fig_1,
fig_2,
div_wb_2,
ncols=1)

导出 .png 后,我可以通过重新定义没有 div 小部件框的 grid 来重建原始文档:

grid = gridplot(fig_1,
fig_2,
ncols=1)

但是,当我将 gridplot() (包括 div)添加到 curdoc() ,然后 export_png(curdoc()) ,div 小部件和图形之间的背景是透明的(即使在 html 中定义了 'background-color':'white')——我需要整个 .png 的白色背景。为了解决这个问题,我打开图像,使用 PIL(Python 图像库)将背景编辑为白色,然后重新保存图像。对于点击“导出图像”小部件按钮的客户端来说,这使得整个服务器端过程比我希望的要慢得多。

我将其发布为我自己问题的答案,因为它解决了问题,但是如果可能的话,简单地从图中删除标签布局会很棒!

仅供引用,here是我的网络应用程序,使用此答案来实现“导出图像”按钮的功能。

关于python - 更新 curdoc() 后从 Bokeh Fig 中删除 Label 布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48912681/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com