gpt4 book ai didi

javascript - Jupyter笔记本: button onclick doesn't work within running notebook

转载 作者:太空宇宙 更新时间:2023-11-04 15:27:49 26 4
gpt4 key购买 nike

我有一种方法可以使用 IPython.display.Javascript 在单独的选项卡中同时打开多个源文档,以帮助简化错误分析工作流程。

# This opens all links in new tabs upon cell execution

def open_all(links):
# Set up onclick function
javascript = "\n".join(['window.open("{}");'.format(link) for link in links])
return javascript

js = open_all_dockets(links)
display(Javascript(js))

我真正想做的是能够将新选项卡中这些链接的打开绑定(bind)到按钮。这将允许笔记本用户执行所有单元格,并且仍然控制通过单击打开哪组源文档

问题是这仅在笔记本渲染为 HTML 后才有效。

# The button opens links in new tabs *only* once the notebook is rendered to HTML

def open_all_button(links):
# Set up onclick function
onclick = "function open() {\n"
onclick += "\n".join([" "*4 + 'window.open("{}");'.format(link) for link in links])
onclick += "}\n"
# Create button
button = "element.append('<button onclick={}>one click open source docs</button>')".format('"open()"')
# Assemble JS
javascript = onclick + button
return javascript

display(Javascript(open_all_button(links)))

如何让它在正在运行的笔记本中工作?

谢谢。

最佳答案

我遇到了麻烦,这似乎适用于实时笔记本和输出为 html 的情况。我猜想单独定义 open() 函数会把事情搞砸,所以我们只是在按钮本身的 onclick 属性中定义它。

# The button opens links in new tabs *only* once the notebook is rendered to HTML
import IPython.display as display
def open_all_button(links):
# Set up onclick function
onclick = "".join(['window.open("{}");'.format(link) for link in links])
# Create button
javascript = "element.append('<button onclick=" + onclick +">one click open source docs</button>')"
return javascript

display.Javascript(open_all_button(["https://www.google.com","https://www.google.co.uk"]))

关于javascript - Jupyter笔记本: button onclick doesn't work within running notebook,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45086066/

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