IT/파이썬 - Bokeh
파이썬 Bokeh 이미지 다운로드 파일명 변경
게발인생
2021. 11. 13. 17:32
https://stackoverflow.com/questions/43358427/bokeh-custom-save-tool
Bokeh Custom Save Tool
I'm Trying to create a custom Save tool in bokeh, and here's what I have: class NewSaveTool(Tool): JS_CODE = """ import * as p from "core/properties" import {ActionTool,
stackoverflow.com
본 글은 해당 스택오버플로우를 참고하여 재작성됨.
from bokeh.models import ActionTool
from bokeh.util.compiler import TypeScript
from bokeh.core.properties import String
class CustomSaveTool(ActionTool):
CUSTOM_SAVE_TS = """
import {ActionTool, ActionToolView} from "models/tools/actions/action_tool"
import * as p from "core/properties"
import {tool_icon_save} from "styles/icons.css"
export class CustomSaveToolView extends ActionToolView {
model: CustomSaveTool
async save(name: string): Promise<void> {
const blob = await this.plot_view.to_blob()
const link = document.createElement("a")
link.href = URL.createObjectURL(blob)
link.download = name // + ".png" | "svg" (inferred from MIME type)
link.target = "_blank"
link.dispatchEvent(new MouseEvent("click"))
}
doit(): void {
this.save(this.model.save_name)
}
}
export namespace CustomSaveTool {
export type Attrs = p.AttrsOf<Props>
export type Props = ActionTool.Props & {
save_name: p.Property<string>
}
}
export interface CustomSaveTool extends CustomSaveTool.Attrs {}
export class CustomSaveTool extends ActionTool {
properties: CustomSaveTool.Props
__view_type__: CustomSaveToolView
constructor(attrs?: Partial<CustomSaveTool.Attrs>) {
super(attrs)
}
static init_CustomSaveTool(): void {
this.prototype.default_view = CustomSaveToolView
this.register_alias("save", () => new CustomSaveTool())
this.define<CustomSaveTool.Props>(({String}) => ({
save_name: [ String ],
}))
}
tool_name = "Custom Save"
icon = tool_icon_save
}
"""
"""Modified save tool allowing custom file names"""
__implementation__ = TypeScript(CUSTOM_SAVE_TS)
save_name = String()
from CustomSave import CustomSaveTool
from bokeh.plotting import figure
# 소스 생략
p = figure(x_range=x_label, tools=TOOLS)
p.vbar(x='under_label', top='height', width=0.9, source=source, line_color='white', fill_color='color')
p.add_tools(CustomSaveTool(save_name='변경할 이름'))
# 소스 생략
CustomSave.py 파일을 만들고, CustomSaveTool을 불러와 add_tools를 사용하여 적용