简体   繁体   中英

IPython 3 to Jupyter migration, custom.js and events

in IPython3 my 'custom.js' was listening for the event :

$([IPython.events]).on("app_initialized.NotebookApp", function() { ... })

After having a look at this post , I'm listening for the 'notebook_loaded.Notebook' event.

require(['base/js/namespace', 'base/js/events'], function(IPython, events) {
  events.on('notebook_loaded.Notebook', function() { ... });
});

But I was using 'app initialized' event to do some stuff including hiding the header, ie:

$('#header').hide()

And now, with this event, it's hiding, but just after a short display... I think there is an event before notebook loaded, just like in IPython3 notebook

I didn't find any docs about those events..

Does it still have any event fired at the app initialization?

Thanks,

For some reason, you can't listen on the app_initialized.NotebookApp event when you use the require('base/js/events') function.

The app_initialized.NotebookApp event is still fired if you look the notebook static/notebook/js/main.js code.

but you have to listen at this event using the define() function from requireJS:

define([
  'base/js/namespace',
  'base/js/events'
], function(IPython, events) {
   events.on('app_initialized.NotebookApp', function() {
     // Your Code
   });
});

Here your callback will be executed.

If you put in your custom.js :

 require(['base/js/namespace', 'base/js/events'], function(IPython, events) {
     events.on('notebook_loaded.Notebook', function() {
        console.log('require & notebook_loaded.Notebook');
    });
     events.on('app_initialized.NotebookApp', function() {
        console.log('require & app_initialized.NotebookApp');
     });
 });

 define(['base/js/namespace', 'base/js/events'], function(IPython, events) {
     events.on('notebook_loaded.Notebook', function() {
        console.log('define & notebook_loaded.Notebook');
    });
     events.on('app_initialized.NotebookApp', function() {
         console.log('define & app_initialized.NotebookApp');
     });
 });

The result in the console will be :

define() & app_initialized.NotebookApp
define() & notebook_loaded.Notebook
require() & notebook_loaded.Notebook

I guess that with the require() you register to an event that had already happen...

require() is waiting for all dependencies and submodules to be intialized... which might be too late for the app_initialized.NotebookApp event.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM