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

标签 javascript jupyter-notebook

我有一种方法可以使用 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/

相关文章:

javascript正则表达式验证强制使用字母字符或字母数字字符,但不使用独立数字

javascript - Angular 4 验证码不工作

Windows : AttributeError: Can't get attribute "abc" 上 Jupyter 中的 python 多处理

ipython - 将 IPython 控制台与 IPython 笔记本一起使用

linux - 为什么 sklearn 中的核心事件在时间 RFECV/LogisticRegression 后减少

python - 如何从 IPython shell 执行 Jupyter Notebook?

javascript - 使用 JQuery 在 DIV 中包含 php 页面

javascript - 在 d3 中,模糊事件仅在 Firefox 中不起作用

javascript - vuejs 2 中可以合并数据根或更改整个数据状态吗

jupyter-notebook - 如何从另一个 ipython 笔记本导入?