我如何(可以)在 Jupyter notebook 下使用 custom.js 文件? [英] How do (can) I use a custom.js file under Jupyter notebook?

查看:39
本文介绍了我如何(可以)在 Jupyter notebook 下使用 custom.js 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 IPython notebook(例如 v3.1)中,我可以添加一个 ~/.ipython/profile_default/static/custom/custom.js 文件来执行一些自定义 JavaScript.例如,我可以这样做:

In the IPython notebook (v3.1, for example), I could add a ~/.ipython/profile_default/static/custom/custom.js file to execute some custom JavaScript. For example, I could do something like this:

require(['base/js/namespace', 'base/js/events'], function(IPython, events) {
    console.log("A");
    events.on('app_initialized.NotebookApp', function() {
        console.log("B");
    });
    console.log("C");
});

然后,在 JS 控制台中,我会看到 A,然后是 B,然后是 C.

Then, in the JS console, I would see A, followed by B, followed by C.

现在,从 4.0 版开始,他们已将其拆分为 Jupyter 笔记本.相同的文件被加载(尽管它在 ~/.ipython 下,而不是在 ~/.jupyter 下),然后代码被执行.但是,我不再看到 B 行.我猜应用程序没有初始化.我仍然看到它在 源中被触发代码,但它是稍后出现的,还是只是不起作用?

Now, as of version 4.0, they've split it out into the Jupyter notebook. The same file gets loaded (despite the fact that it's under ~/.ipython, rather than under ~/.jupyter), and the code gets executed. However, I no longer see the B line. I guess the app isn't getting initialized. I still see it get triggered in the source code, but does that comes later, or is it just not working?

我如何让事情恢复正常?我不需要再等待 app_initialized 了吗?是否在某处记录了这些内容?

How do I get things working again? Do I just not need to wait for app_initialized any more? Is any of this documented somewhere?

这个页面 似乎暗示现在的做法是创建自定义扩展并将所有操作放入 load_ipython_extension 函数中.是对的吗?如果是这样,那么 mathjax 怎么样?和 CodeMirror 选项?

This page seems to suggest that the way to do it nowadays is to create a custom extension and put all the action in the load_ipython_extension function. Is that right? If so, how about mathjax? And CodeMirror options?

推荐答案

使用 custom.js 仍然适用于我,但它似乎移动了一些.

Using custom.js still works for me, but it seems to move around a fair bit.

当前(版本 4.2.3)以及即将发布的 5.0 版本的文档中,它位于 ~/.jupyter/custom/custom.js.请参阅 http://jupyter-notebook.readthedocs.io/en/latest/examples/Notebook/JavaScript%20Notebook%20Extensions.html#custom.js

Currently (version 4.2.3) as well as in the documentation for the upcoming 5.0 release, it's at ~/.jupyter/custom/custom.js. See the documentation at http://jupyter-notebook.readthedocs.io/en/latest/examples/Notebook/JavaScript%20Notebook%20Extensions.html#custom.js

您可以通过在笔记本中执行此代码段来显示 custom.js 的路径和内容:

You can show the path to and content of custom.js by executing this snippet in a notebook:

from jupyter_core.paths import jupyter_config_dir
jupyter_dir = jupyter_config_dir()
import os.path
custom_js_path = os.path.join(jupyter_dir, 'custom', 'custom.js')
print("searching for custom.js in ", custom_js_path)
#  my custom js
if os.path.isfile(custom_js_path):
    with open(custom_js_path) as f:
        print(f.read())
else:
    print("You don't have a custom.js file")

这篇关于我如何(可以)在 Jupyter notebook 下使用 custom.js 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆