【问题标题】:JupyterLab Export to HTML with hide code Button Security Issues?JupyterLab 导出到 HTML 并隐藏代码按钮安全问题?
【发布时间】:2021-04-10 11:56:57
【问题描述】:

存在用于在 jupyter 笔记本中插入切换/隐藏代码按钮的代码(请参阅 How to hide code from cells in ipython notebook visualized with nbviewer?),并且可以在将报告从 Jupyter 导出为 HTML 时使用。

但是,此代码不再适用于 JupyterLab。似乎 JupyterLab 更改了相对于 Jupyter 的容器名称。

对于 Jupyter 容器名称是

对于代码单元。 对于 JupyterLab,最接近的模拟看起来像

使用这个假设,我编写了一个似乎在 JupyterLab 中工作的更新 javascript。 我在网上找不到任何类似的解决方案,而且有些网站似乎暗示出于安全原因而为 Jupyterlab 停用了 JavaScript (https://github.com/jupyterlab/jupyterlab/issues/3118#)。

我对 javascript 和 html 的理解是初级的,所以我想知道这个解决方案是否存在任何安全问题和/或重新编码的更好方法。这是我在 JupyterLab 的原始代码单元中插入的代码:

<script>
code_show="a"
var r = document.querySelector(':root')
if (code_show === "a") {
      code_show="flex"
  }
function code_toggle() {

    var v = r.getElementsByClassName("jp-Cell-inputWrapper")

    if (v.item(0).style.display === "") {
        if (code_show === "none") {
          for (i = 1; i < v.length-1; i++) {
              v.item(i).style.display = "flex";
          }
          code_show = "flex"
        } else {
          for (i = 1; i < v.length-1; i++) {
              v.item(i).style.display = "none";
          }
          code_show = "none"
        }       
    }
} 
</script>
<form action="javascript:code_toggle()"><input type="submit" value="TOGGLE CODE IN REPORT VIEW."></form>

【问题讨论】:

    标签: javascript html security show-hide jupyter-lab


    【解决方案1】:

    将ipynb代码文件转换为没有代码的pdf文件(使用Python)::

    第 1 步:假设您的文件 Untitled.ipynb 保存在笔记本电脑的“下载”文件夹中。

    Step2:打开 Anaconda 提示符或 Cmd ,粘贴以下命令隐藏代码并将文件保存为 Untitled.pdf:

    • cd 下载
    • jupyter nbconvert Untitled.ipynb --to=pdf --TemplateExporter.exclude_input=True

    注意:Untitled.ipynb 的路径可能不同,需要在我们使用 cd Downloads 的位置更改为 cd new_path。

    如果pdf转换出错,先转换成Untitled.html文件

    • cd 下载
    • jupyter nbconvert Untitled.ipynb --to=html --TemplateExporter.exclude_input=True
    • 在网络浏览器中打开 Untitled.html 并保存为(Crtl+P) pdf 文件

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-27
      • 1970-01-01
      • 2015-04-01
      • 1970-01-01
      相关资源
      最近更新 更多