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

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

问题描述

在IPython笔记本(例如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笔记本下使用custom.js文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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