Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do (can) I use a custom.js file under Jupyter notebook?

Tags:

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"); }); 

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

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?

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?

Edit

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?

like image 852
Mike Avatar asked Aug 23 '15 18:08

Mike


People also ask

Where is the custom JS in Jupyter?

custom. js can be found in the ~/. jupyter/custom/custom.

Can you use Javascript in Jupyter Notebook?

Jupyter Notebooks are documents that contain a mix of live code (Python, R, Julia, JavaScript, and more), visualizations, and narrative text (Markdown). They're useful for breaking down concepts in a story telling form, where you can give some context and show the code below along with interactive visualizations.


1 Answers

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

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

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") 
like image 57
Matthias Winkelmann Avatar answered Sep 20 '22 14:09

Matthias Winkelmann