Jupyter notebook 使用nbconvert将笔记本导出为HTML,并嵌入面板数据(bokeh、holoviews、hvplot)

Jupyter notebook 使用nbconvert将笔记本导出为HTML,并嵌入面板数据(bokeh、holoviews、hvplot),jupyter-notebook,bokeh,panel,holoviews,nbconvert,Jupyter Notebook,Bokeh,Panel,Holoviews,Nbconvert,我正在尝试将笔记本导出为HTML,笔记本内部有多个交互面板 import panel as pn import hvplot as hv import holoviews as hv2 from bokeh.io import output_notebook, push_notebook, show, save, output_file from bokeh.resources import INLINE def plot_ts(feature=Spends[0], target=Target

我正在尝试将笔记本导出为HTML,笔记本内部有多个交互面板

import panel as pn
import hvplot as hv
import holoviews as hv2
from bokeh.io import output_notebook, push_notebook, show, save, output_file
from bokeh.resources import INLINE

def plot_ts(feature=Spends[0], target=Target[0]):
    fig = df[target].plot.line(title=target)
    fig += df[feature].plot.area(title=feature)
    
    return fig.cols(1)

kw = dict(feature=sorted(list(Spends + Misc)))
panel1 = pn.interact(plot_ts, **kw)
panel1
现在我正在运行以下行:

!jupyter nbconvert --to html index.ipynb --no-input --no-prompt
问题是我的面板过时了(相应的数据没有嵌入到面板中)。
如果我用下面的行一个一个地保存面板,我会得到带有嵌入数据的面板

panel1.save('test.html', embed=True, resources=INLINE)
我尝试以这种方式保存所有面板,然后使用Selenium合并不同的HTML文件,但没有成功

我试着把面板相互连接起来

all_panels.append(panel1).append(panel2).append(panel3)
all_panels.save("all_panels.html", embed=True)
生成的HTML文件有缺陷,有些面板可以工作,有些则不能

如果有人能想到如何使这项工作成功,那将是令人惊讶的。

谢谢

使用BeautifulSoup,我成功创建了一个HTML文件,其中包含不同面板,并将其嵌入数据保存为standalones HTML文件。
我认为它可能对其他人有用,因此下面是代码:

# Imports
from bs4 import BeautifulSoup, Tag
import glob

# List the filepaths corresponding to panels exported to html using 'panel_object.save("panel_name.html", embed=True)'
panels = glob.glob("/Users/username/Documents/data_exploration/panels/*.html")
panels_soup = []

# Read files and append their soup to a list
for panel in panels:
    with open(panel) as fp:
        panel = BeautifulSoup(fp, 'html.parser')
        panels_soup.append(panel)

# HTML base
soup = BeautifulSoup()
html = soup.new_tag("html")
soup.append(html)

# Append the head tag of one of the panels (it includes the bokeh/holoviews scripts)
soup.html.append(panels_soup[0].find("head"))

# Append an empty body tag
body = soup.new_tag("body")
soup.html.append(body)

# Loop on soups
for panel in panels_soup:
    divs = panel.find_all("div", attrs={"class":"bk-root"})
    data = panel.find_all('script')[-2]
    script = panel.find_all('script')[-1]
    panels_dict.append({"div_1":divs[0], "div_2":divs[-1], "data":data, "script":script})

# Append panels divs to the body of the page
for panel in panels_dict:
    soup.body.insert(1, panel["div_2"])
    soup.body.insert(1, panel["div_1"])

# Append the data at the end of the page
for panel in panels_dict:
    soup.body.insert(len(soup.body.contents), panel["data"])

# Append the scripts at the end of the page  
for panel in panels_dict:
    soup.body.insert(len(soup.body.contents), panel["script"])

# Export HTML file containing all the panels
with open("index.html", "w") as fp:
    fp.write(soup.prettify(formatter="html"))

你没有提供足够的信息进行推测。如果存在依赖于真实Python回调的交互,那么它们就无法在导出的HTML中运行。Python回调支持的交互需要一个运行的内核。我想要的是一个独立的HTML页面,包括css、脚本和嵌入数据,这样我就可以与面板交互。这一行让我得到我想要的单个面板:panel1.save('test.html',embed=True,resources=INLINE)。我想在一个HTML页面上的所有面板基本上。