[IPython-dev] Moving IPython javascript methods from cells to custom.js

Matthias Bussonnier bussonniermatthias at gmail.com
Sun Jun 14 17:10:03 EDT 2015


> On Jun 14, 2015, at 13:31, taleb brost <mkpaustin at gmail.com> wrote:
> 
> I would like to make two keyboard shortcuts posted in
> http://stackoverflow.com/questions/28309430/edit-ipython-cell-in-an-external-editor <http://stackoverflow.com/questions/28309430/edit-ipython-cell-in-an-external-editor>  default mappings for all my notebooks.  Can someone point
> me to the correct syntax to append to static/custom.js, given the
> working cell code below?   
> 

You probably need to wrap that in a :

```
require([‘base/js/events’,’base/js/namespace’], function(events, IPython){
	events.on("app_initialized.NotebookApp”, function(){
		// your code.
       }
})

[not tested]

the outer block, making sure the code is triggered only once events and IPython are available, 
the inner block making sure this is triggered 


That being said, I would look at how to write your own extension, that can be activated/deactivated programatically and shared more easily.

See following for example. 

https://github.com/ipython-contrib/IPython-notebook-extensions <https://github.com/ipython-contrib/IPython-notebook-extensions>


Which is cleaner. 
— 
M




> %%javascript
> 
> IPython.keyboard_manager.command_shortcuts.add_shortcut('g', {
>     handler : function (event) {
>         var input = IPython.notebook.get_selected_cell().get_text();
>         var cmd = "f = open('.toto.py', 'w');f.close()";
>         if (input != "") {
>             cmd = '%%writefile .toto.py\n' + input;
>         }
>         IPython.notebook.kernel.execute(cmd);
>         cmd = "import os;os.system('gvim .toto.py')";
>         IPython.notebook.kernel.execute(cmd);
>         return false;
>     }}
> );
> 
> IPython.keyboard_manager.command_shortcuts.add_shortcut('u', {
>     handler : function (event) {
>         function handle_output(msg) {
>             var ret = msg.content.text;
>             IPython.notebook.get_selected_cell().set_text(ret);
>         }
>         var callback = {'output': handle_output};
>         var cmd = "f = open('.toto.py', 'r');print(f.read())";
>         IPython.notebook.kernel.execute(cmd, {iopub: callback}, {silent: false});
>         return false;
>     }}
> );
> 
> _______________________________________________
> IPython-dev mailing list
> IPython-dev at scipy.org
> http://mail.scipy.org/mailman/listinfo/ipython-dev

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ipython-dev/attachments/20150614/1c82e17b/attachment.html>


More information about the IPython-dev mailing list