how to bind a command to 'Restart and Run All'? - jupyter-notebook

I have recent build of Jupyter than has a menu action allowing you to Restart & Run All:
I would like to add a keyboard shortcut that binds to this action. I have seen the documentation for keyboard customization, but I'm still unsure how to add a keyboard shortcut.
I've built Juypter from source, so based on the help, it would appear that I need to add some code to notebook/static/custom/custom.js.
I've tried adding the following:
IPython.keyboard_manager.command_shortcuts.add_shortcut('meta-r', function (event) {
IPython.notebook.restart_kernel();
IPython.notebook.execute_run_all();
return false;
});
However, when I press [Meta-r], the kernel seems to restart but execute_run_all() does not get executed.

As of Jupyter Notebook 5.0, you can now create and edit keyboard shortcuts directly in menu options. As of now, it's Help -> Edit Keyboard Shortcuts. There's a guide at the bottom of that dialogue box.
The docs on it are here:
http://jupyter-notebook.readthedocs.io/en/latest/examples/Notebook/Custom%20Keyboard%20Shortcuts.html

Here's what I have in my custom.js. For it to work, the shortcut addition has to happen after the app is initialized:
$([Jupyter.events]).on("app_initialized.NotebookApp", function () {
Jupyter.keyboard_manager.command_shortcuts.add_shortcut('0,1', {
help: 'restart and run all',
help_index: '0,1',
handler: function (event) {
Jupyter.notebook.kernel.restart();
restartTime = 2000 // decrease this if you have a fast computer
setTimeout(function(){ Jupyter.notebook.execute_all_cells(); }, restartTime);
return false;
}}
);
});

Just in case someone stumbles upon this post looking for the same answer: you need to wait for the kernel to restart with a timeout before executing.
See this discussion on GitHub.
In your case, it would give:
IPython.keyboard_manager.command_shortcuts.add_shortcut('meta-r',
function (event) {
IPython.notebook.kernel.restart();
setTimeout(function(){ IPython.notebook.execute_all_cells(); }, 1000);
return false;
});

Related

Any way to bring back %%sql magic syntax highlighting to JupyterLab notebook?

So I just recently made a switch from an isolated Conda-based Jupyter Notebook to a JupyterLab notebook, but I noticed that there was no longer a straightforward way of adding syntax highlighting to the %%sql cell magic, since the code structure is completely different. Previously, the following code would be added to a custom.js file in ~/.jupyter/custom/:
require(['notebook/js/codecell'], function (codecell) {
codecell.CodeCell.options_default.highlight_modes['magic_text/x-mssql'] = { 'reg': [/%?%sql/] };
Jupyter.notebook.events.one('kernel_ready.Kernel', function () {
Jupyter.notebook.get_cells().map(function (cell) {
if (cell.cell_type == 'code') { cell.auto_highlight(); }
});
});
});
Now that JupyterLab extensions rely on Python, I'm not sure how I could go about implementing a similar front-end extension for the cell magic.

MessengerExtensions.requestCloseBrowser() won't close in desktop

When i try to close the webview with this code:
MessengerExtensions.requestCloseBrowser(function success() {}, function failure(err) {
alert('error closing the window: ' + err); // error closing the window:
console.log(err); // doesn't print
window.close();
});
it closes the webview in iOS, but pops an alert when i try from desktop.
My domain is white-listed, messenger_extensions = true and i enter the page from the desktop messenger and it still doesn't work.
I had before the same problem, but opposite (window closed on desktop, but not on iOS) while trying to use fetch(), and this problem started when started using $.ajax()
before MessengerExtensions.requestCloseBrowser() was invoked from a fetch's promise, and now it's invoked from ajax's success function.
I had to switch fetch with ajax because it didn't work on iOS
Any suggestions?
A day later an error code of 2071011 started to show up, again, only in desktop browsers.
I managed to find a workaround the problem by using window.top.close(); when MessengerExtensions.requestCloseBrowser() fails
It does the same trick on Chrome and asks the user before close on Edge
window.extAsyncInit = function() {
MessengerExtensions.requestCloseBrowser(function success() {
window.close(); // webview closed
}, function error(err) {print ('an error occured');}
);
};
From the documentation on https://developers.facebook.com/docs/messenger-platform/webview/extensions,
window.extAsyncInit() will be called when the Messenger Extensions JS
SDK is done loading. You can use this as a trigger to call other
functions available in the SDK.

Howto hook into 'PageLoaded' observer in SilverStripe 3?

I need to execute a jQuery function after a page loaded. The docs told me that it would be possible to hook into the 'PageLoaded' observer.
So I tried it like shown there. I put this function
Behaviour.register({
'#Form_ItemEditForm' : {
initialize : function() {
this.observeMethod('PageLoaded', this.pageLoaded);
this.observeMethod('BeforeSave', this.beforeSave);
this.pageLoaded(); // call pageload initially too.
},
pageLoaded : function() {
alert("You loaded a page");
},
beforeSave: function() {
alert("You clicked save");
}
}
});
into my cms.js which get's loaded in the backend. I tried it inside and outside (function($) { .. code ..}(jQuery)); and also inside the doucment.ready function inside the first function.
I always receive the same error in my console Uncaught ReferenceError: Behaviour is not defined.
Where is my mystake?
I believe you may have been looking at docs for 2.4, not 3.x
Version 3 and up are built using jQuery.entwine, where this from memory is old Prototype plugin stuff from 2.4, meaning of course that Behaviour is not defined, just as the error says.
The docs have recently been updated, so perhaps visit again, you might learn something new & much more helpful :)

Durandal: How to route away from current view within that view's activate() function?

I have the following:
function activate(routeData) {
// make dataservice call, evaluate results here. If condition is met, reroute:
if (true){
router.navigateTo("#/someRoute");
}
alert ("should not be shown");
}
The alert is getting hit however, and then the view changes.
How do I fully navigate away from the current item and prevent any further code in that vm from being hit?
Update:
I tried using guardroute but I have to activate the viewModel to call the dataservice that returns the data that determines whether or not I should re-route. Using guardroute totally prevents the dataservice from getting called (since nothing in the activate function will get hit).
I also tried returning from the if block but this still loads the view / viewAttached / etc so the UX is glitchy.
The following worked for me in Durandal 2.0:
canActivate: function() {
if(condition)
return {redirect: 'otherRoute'};
return true;
}
activate: // Do your stuff
It's mentioned in the documentation: http://durandaljs.com/documentation/Using-The-Router.html
Here's #EisenbergEffect answer to a quite similar discussion in google groups.
Implement canActivate on your view model. Return a promise of false,
then chain with a redirect.
You might want to give #JosepfGabriel's example (discussion) a try in Durandal 1.2. Check the correct router syntax for your Durandal version, you might have to substitute it with something like router.navigateTo("#/YourHash", 'replace').
canActivate: function () {
return system.defer(function (dfd) {
//if step 2 has a problem
dfd.resolve(false);
})
.promise()
.then(function () { router.navigate("wizard/step1", { trigger: true, replace: true }); });
}
However this is NOT working in Durandal 2.0 and there's a feature request https://github.com/BlueSpire/Durandal/issues/203 for it.
You can't call redirect into the active method.
You can override the guardRoute method from router, to implement redirections.
You can do somehting like that:
router.guardRoute= function(routeInfo, params, instance){
if(someConditios){
return '#/someRoute'
}
}
You can return a promise, true, false, the route to redirect... You can find more information about that in the next link: http://durandaljs.com/documentation/Router/
Rainer's answer was pretty good and works for me adding this small fix.
Inside the then() block simply call the navigation like this
setTimeout(function() { router.navigateTo('#/YOUR DESTINATION'); }, 200);
that should fix your problem. The setTimeout does the trick. Without it the newly navigated page catches the old NavigationCancel from the previous one.
Adding a return in your if (true) block should fix this.
function activate(routeData) {
if (true){
router.navigateTo("#/someRoute");
return;
}
alert ("should not be shown");
}

I can't get my added element to activate a function

I am trying to add a link to the colorbox interface as it is created. It will eventually link to the currently displayed image file but for now I just want to get the console.log to work. It adds the link correctly (#print-one) but I can't get a function to run when the link is clicked. Any help would be much appreciated!
$(document).bind('cbox_complete', function () {
// Show close button with a delay.
$('#cboxClose').css('opacity', 1);
// Add Print Button
if ($('#print-one').length ) {
// Do Nothing
}else {
$('#cboxNext').after('Print');
}
});
$('#print-one').click(function() {
console.log('Works');
});
This is all wrapped inside the $(document).ready function. I just can't get the console log to work when the link is clicked. I have been beating my head against a wall trying to figure it out. Thanks for any help!
You need to delegate the event.
$(document).on('click', '#print-one', function(){});
It seems that the link is added just after cbox is initialized, which may not be ready when the compiler gets to the click binding function a few lines below.
Try this:
...
// Add Print Button
if ($('#print-one').length ) {
// Do Nothing
}else {
$('Print').insertAfter('#cboxNext').click(function() {
console.log('Works');
});
}

Resources