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

epi massimodisasha at gmail.com
Sun Dec 9 01:26:23 EST 2012


> Hi there !
> 
> You did already a pretty decent job !
> 
> we are starting to document javascript you can see what it looks like here :
> http://elacave.lmdb.eu/~carreau/yui/
> And if you have more do cot add we'll be happy to take your pull request.
> 

Thanks a lot Matthias,


i'm trying to go ahead,  but with some problems

i should go ahead by little steps .. when i'll be able to do something that make sense i'll be more than happy to start a PR

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 at gmail.com> ha scritto:

>> 
>> - detect the name  for the "notebook" (filename.ipnb)
> 
> 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 :/


> 
>> - 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 low the notebook name

> 
>> 
>> - save the "notebook"
> 
> IPython.notebook.save_notebook()
> 
>> 
>> - commit the saved file (here should prompt a js text input box where to add the commit notes, but i can live without it as a start)
> 
> it is doable, just a question of time to code it.
> 
>> 
>> 
>> 		IPython.toolbar.add_buttons_group([
>> 		    {
>> 		         'label'   : 'Save & Commit Notebook',
>> 		         'icon'    : 'ui-icon-circle-arrow-n', 
>> 		         'callback': function(){IPython.notebook.kernel.execute('detect the notebook name'),
>> 				                        IPython.notebook.kernel.execute('clean notebook'),
>> 							IPython.notebook.kernel.execute('save notebook'),
>> 							IPython.notebook.kernel.execute('svn commit notebook')}
>> 		    }
>> 		    ]);
>> 
>> 
>> i need an help to learn how to use "IPython.notebook.kernel.execute" from inside a JS code
>> i was looking in `docs/examples/widgets/directview` but i got lost :(
> 
> 
> So kernel.execute take callbacks, which get the data back, it's a little tricky.
> 
> let's do it for git : 
> 
> IPython.notebook.kernel.execute(
> 	'a=!git status', 
> 		{'execute_reply': function(data){console.log('data:',data)}},
> 	 	{'user_variables':['a']}
>     )
> 
> ( Note for later, to do this properly, it should be done with user_expression, but there is an issue, i don't now why)
> 
> function(data){console.log('data:',data)}
> 
> will be called with the response as parameter, 
> here login the data to the js console.
> 
> Data will be an object with a few parameters ( i'll let you explore), the one that interests us is user_variable ( we requested 'a' above) 
> 
> which itself as a 'a' key.
> 
> data.user_variables.a is the following strig :
> "['# On branch master', '# Changes not staged for commit:', '#   (use "git add <file>..." to update what will be committed)', '#   (use "git checkout -- <file>..." to discard….."
> (using user_expression would prevnt polluting the kernel with the 'a' variable but it raises.
> then you can do whatever you wish with the variable in the callback.


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)
#

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)
#

but it gave me 


Uncaught ReferenceError: data is not defined custom.js:32
IPython.toolbar.add_buttons_group.callbackcustom.js:32
f.event.dispatchjquery-1.7.1.min.js:3
h.handle.ijquery-1.7.1.min.js:3
data: 
Object {status: "ok", execution_count: 0, user_variables: Object, payload: Array[0], user_expressions: Object}
 custom.js:30




> 
> see 
> http://wiki.ipython.org/Notebook_feedback
> to strip notebook programatically ( in the gist at the end of the page) 
> 
> 
>> 
>> have you any hints on how can i use the ipython.notebook.kernel  in order to :
>> 
>> - print out some text in a new notebook cell 
> 
> IPython.notebook.get_cell(i).set_text(…)
> 
> IPython.notebook.select(n)
> IPython.notebook.insert_cell_above()
>> 
> should be of help.


i can print a string (not yet a python object) in the last cell using something like :

IPython.notebook.get_cell(-1).set_text('…some text….')

but i don't know how to "add" a new cell,

let's say i want add a cell at the end, i should select the n=-1 with :

> IPython.notebook.select(n)

then :

> IPython.notebook.insert_cell_above()

and at the end :


IPython.notebook.get_cell(-1).set_text('…some text….')

but the cell is not added


> 
>> - 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 ?


> 
> 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!

Thanks!

--MAssimo.


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


More information about the IPython-dev mailing list