亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從帶有布局的圖表創建獨立的 HTML

從帶有布局的圖表創建獨立的 HTML

慕萊塢森 2022-01-18 15:47:24
我想創建一個獨立的 HTML,其中包含幾個基于布局的 Bokeh 交互式圖形(如果可能)。我已經成功地將每個圖表創建為自己的 HTML 或 PNG。我的圖表是從函數單獨生成的。這是一個例子。from bokeh.plotting import figure, output_file, showdef create_scatter():    # output to static HTML file    output_file("test_scatter.html")    p = figure(plot_width=400, plot_height=400, title='This is a Scatter chart')    # add a circle renderer with a size, color, and alpha    p.circle([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], size=20, color="navy", alpha=0.5)    p.title.text_font_style = "italic"    # show the results    show(p)    # Attemp to return the chart    return pdef create_line():    # output to static HTML file    output_file("line.html")    p = figure(plot_width=400, plot_height=400, title='This is a Line chart')    # add a line renderer    p.line([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], line_width=2)    p.title.text_color = "olive"    p.toolbar.logo = None # Hide Bokeh Logo    # show the results    show(p)    # Attemp to return the chart    return pdef create_HTML(f1, f2):    passdef main():    f1 = create_scatter()    f2 = create_line()    create_HTML(f1, f2)if __name__ == '__main__':    main()但是,我找不到將它們全部放在一個 HTML 文件中的方法。每個圖表都有自己獨特的標題、屬性、圖例和工具。我的預期結果是基于布局創建一個獨立的 HTML,可能可以修改其背景等。這是我的預期結果。先感謝您更新我想我需要返回圖表的組件script, div = components(p, CDN)return script, div然后我需要將它們放入 HTML 字符串中。最后,輸出文件。
查看完整描述

1 回答

?
天涯盡頭無女友

TA貢獻1831條經驗 獲得超9個贊

你已經給自己答案了。使用components是實現它的一種方式。請查看Bokeh 嵌入文檔以了解所有選項。


如果您想要一個可以使用此方法的單個文件實現(使用 Bokeh 1.1.0 測試):


from jinja2 import Template

from bokeh.plotting import figure

from bokeh.embed import file_html

from bokeh.models import Div, Paragraph, Row, Column

from bokeh.resources import CDN

from bokeh.util.browser import view


template = Template("""

<!DOCTYPE html>

<html>

    <head>

        <meta charset="utf-8">

        <title>{{ title if title else "Bokeh Plot" }}</title>

        {{ bokeh_css | safe }}

        {{ bokeh_js | safe }}

    </head>

    <body>

        {{ plot_div | safe }}

        {{ plot_script | safe }}

    </body>

</html> """)


p1 = figure(plot_width = 400, plot_height = 400)

p2 = figure(plot_width = 400, plot_height = 400)

p3 = figure(plot_width = 800, plot_height = 400)

p1.circle([1, 2, 3], [4, 5, 6])

p2.line([1, 2, 3], [4, 5, 6])

p3.line([1, 2, 3], [4, 5, 6])


html = file_html(Column(Row(p1, p2), Row(p3)), template = template, resources = CDN)


output_file = 'css_classes.html'

with open(output_file, 'w') as f:

    f.write(html)

view(output_file)

結果:


http://img1.sycdn.imooc.com//61e670bd000149e412981278.jpg

查看完整回答
反對 回復 2022-01-18
  • 1 回答
  • 0 關注
  • 189 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號