[IPython-dev] custom JS and IPython.notebook.kernel usage (svn example)

Matthias BUSSONNIER bussonniermatthias@gmail....
Sun Dec 9 05:44:30 CST 2012


Le 9 déc. 2012 à 07:26, epi a écrit :
> 
> Thanks a lot Matthias,
> 
> So for now i'll try to work on a "svn info" button as test case (no input required and no needs to check the notebook name)
> 
> what it should do :
> 
> 1) run a system call that print some text as log
> 2) parse the log in a dict
> 3) have the dict printed in a new cell 
> 
> i'm aware that's my lack of JS knowledge, below some questions to better understand what you suggest me :
> 
> Il giorno 05/dic/2012, alle ore 10:23, Matthias BUSSONNIER <bussonniermatthias@gmail.com> ha scritto:

>> I'm not sure this can be easily accessible, but it could be a request for enhancement,
>> what you can do is : 
>> IPython.notebook.notebook_name
>> To lowercasse, and space to _ then add ipynb.
> 
> i didn't understand this steps :/

Ok,
This, should give you the name of the ipynb file (more or less)

IPython.notebook.notebook_name.toLowerCase().replace(' ','_')+'.ipynb'

>> 
>>> - clean all the output of the "notebook"
>> 
>> this can be done progrmatically on server side with a custom script on the ipynb file.
> 
> I saw the examples on the wiki page, that's great i can follow the example "remove_outputs" but this still require to know the notebook name
> 
> 
> i changed the code of the svninfo button to :
> 
> 
> IPython.toolbar.add_buttons_group([
> 	{
> 		'label'   : 'SVN Info',
> 		'icon'    : 'ui-icon-info', 
> 		'callback': function(){IPython.notebook.kernel.execute('a = !svn info'), 
> 		                       IPython.notebook.kernel.execute('svninfo = {i.split(": ")[0]:i.split(": ")[1] for i in a[:-1]}',
> 							                                          {'execute_reply': function(data){console.log('data:',data)}},
> 												  {'user_variables':['svninfo']})
> 							  }
> 	}
> ]);
> 
> 
> i'm not yet able to print out the dict in a new cell … 
> 
> the svninfo dictionary is available in the notebook's code cell (after i press the info button), 
> so the IPython.notebook.kernel.execute works as aspected and the 
> 
> svninfo is also available in the JS console.
> 
> but trying to add the svninfo in the "last" cell adding a line in the button like :
> 
> #
>> {'user_variables':['svninfo']}),
> IPython.notebook.get_cell(-1).set_text(svninfo)
> #
> 

IPython.notebook.insert_cell_at_bottom()
c = IPython.notebook.get_cell(-1)
c.set_text('foo')

does work to create a cell with 'foo' inside. 

you will have to put this in place of 

>  function(data){console.log('data:',data)}

and the text for the cell will be in data.user_variable.svninfo.


IPython.toolbar.add_buttons_group([
	{
		'label'   : 'SVN Info',
		'icon'    : 'ui-icon-info', 
		'callback': function(){IPython.notebook.kernel.execute('a = !svn info;svninfo = {i.split(": ")[0]:i.split(": ")[1] for i in a[:-1]}', 
							                                          {'execute_reply': function(data){
															console.log('data:',data)
															IPython.notebook.insert_cell_at_bottom()
															c = IPython.notebook.get_cell(-1)
															c.set_text(data.user_variable.svninfo)
															}},
												  {'user_variables':['svninfo']})
							  }
	}
]);



> but i have errors .. i guess i should not pass the python dict to set_text()
> 
> Uncaught ReferenceError: svninfo is not defined 
> 
> i tried to give the JS object :
> 
> #
>> {'user_variables':['svninfo']}),
> IPython.notebook.get_cell(-1).set_text(data.user_variables.svninfo)


> ...
>> 
>>> - detect the name for the active notebook
>>> - clear all the output for the active notebook
>>> 
>>> 
>>> Thanks a lot for you help,
>> 
>> click on button -> callback a
>> 
>> callback a fetch info on the kernel trigger callback b on the response
>> 
>> callback b -> show a dialog to ask for commit message
>> 
>> click on ok trigger the correct information to be send on the kernel to commit.
>> 
> 
> does this means i can have more "callback"  executed when the same "button" is triggered ?

It is possible, but not what I was meaning. 


>> 
>> Does it help ? 
>> We probably want to do something like that in a more generic way embedded into IPython notebook. 
>> 
>> We'll be happy to get your feedback on that !
>> 
>> -- 
>> Matthias
> 
>  i'm a bit slow in learning how the system works (my fault)
> it is helping me a lot!

No it's not your fault,  the way javascript works is quite a bit different from python. 
It took me quite some time to figure out this also. 

The problem is you are not used to use unnamed callback which people called 'callback hell'.
It is quite possible to do those with named function, even if it is sometime more convenient to do them anonymously for closure. 

What you can do is open an issue or even a Pull request, so that we can see your full code, 
and comment inline on how to do things. 

Cheers, 
-- 
Matthias

> 
> Thanks!
> 
> --MAssimo.
> 
> 
> _______________________________________________
> IPython-dev mailing list
> IPython-dev@scipy.org
> http://mail.scipy.org/mailman/listinfo/ipython-dev



More information about the IPython-dev mailing list