HTML中的Bokeh图(带循环)

2024-09-29 06:31:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图根据从SQL查询中提取的数据,用HTML动态呈现bokeh图。在

我可以通过Jupyter制作图形并将它们呈现在单独的HTML文件中,但我很难从实际代码中实现这一点。在

这里是Jupyter的工作代码:

materialcode=cursor.fetchall()

    for number in range(0,len(materialcode),1):
        materialcode_list=materialcode[base_number+int(number)]['Material_Code']

        with connection.cursor() as cursor:
            sql = "Select Delivey_Date, Unit Price from report GROUP BY Delivey_Date";
            with connection.cursor(pymysql.cursors.DictCursor) as cursor:
                    cursor.execute(sql)
            linechart=cursor.fetchall()

        df=pd.DataFrame(linechart)

        x = df['Delivey_Date']
        y = df['Unit Price']

        p = figure(plot_width=750, plot_height=300, x_axis_type="datetime")
        p.line(x, y, line_width=2)
        p.title.text = df['Material_Name'][0]

        output_file("line"+str(number)+".html")
        show(p)

这将生成多个图,每个图都有自己的HTML文件(每个都会弹出一个新窗口)。 现在,我想呈现在我的HTML网页,所有的图形在网页上预定的位置一个接一个地在下面。在

对于一个图,除了以bokehjs脚本和script1=script1,div1=div1引用结尾外,这是一个简单的代码。在

^{pr2}$

我无法理解的是如何在循环中包含脚本,以便使用自己的脚本引用自动呈现每个图形。我想一定有一种方法可以从Jupyter示例中构造出web页面所需的script1=script1,div1=div1的输出文件(“line”+str(number)+“.html”)。在

谢谢你的帮助。在


Tags: 文件代码脚本图形numberdfdatehtml
1条回答
网友
1楼 · 发布于 2024-09-29 06:31:00

components一次可以接受多个对象:

components({"Plot 1": plot_1, "Plot 2": plot_2})
#=> (script, {"Plot 1": plot_1_div, "Plot 2": plot_2_div})

有关详细信息,请参阅documentation。在

相关问题 更多 >