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

Matthias BUSSONNIER bussonniermatthias at gmail.com
Wed Dec 5 10:23:25 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.



Le 5 déc. 2012 à 11:42, epi a écrit :

> Hi,
> 
> 
> i'm used to have my "c.NotebookManager.notebook_dir" under version control, for now SVN … i should switch to GIT
> 
> i was playing with js in a custom.js file,  in order to add some "buttons"  to "COMMITT"  change to a notebook into my repository
> (the plan was to add a custom toolbar to execute simple svn commands)
> 
> an example to store the results of svn info in a dict can be like :
> 
> 
> 
> 		IPython.toolbar.add_buttons_group([
> 		    {
> 		         'label'   : 'SVN Info',
> 		         'icon'    : 'ui-icon-info', 
> 		         'callback': function(){IPython.notebook.kernel.execute('svninfolist = !svn info'),
> 				                        IPython.notebook.kernel.execute('svninfo = {}'),
> 				                        IPython.notebook.kernel.execute('for i in svninfolist[:-1]: j=i.split(":") ; svninfo[j[0]]=j[1].strip()')
> 										}
> 		    }
> 		    ]);
> 
> this give me a dict like :
> 
> 2]:
> 
> svninfo
> Out[2]:
> {'Last Changed Author': 'epifanio',
>  'Last Changed Date': '2012-11-20 15:30:34 -0500 (Tue, 20 Nov 2012)',
>  'Last Changed Rev': '7847',
>  'Node Kind': 'directory',
>  'Path': '.',
>  'Repository Root': 'https://myuser@myserver.org/svn',
>  'Repository UUID': 'e8c77636-c7aa-4457-b6c1-051e789c14f2',
>  'Revision': '7877',
>  'Schedule': 'normal',
>  'URL': 'https://myuser@myserver.org/svn/path/to/dir'
> '}
> 
> while in order to "commit" changes, i was looking for something like :
> 
> - 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.

> - clean all the output of the "notebook"

this can be done progrmatically on server side with a custom script on the ipynb file.

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

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.

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

> 
> Massimo
> 
> 
> _______________________________________________
> 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/20121205/9e7fedfb/attachment.html>


More information about the IPython-dev mailing list