Un exemple très simple serait (coller le code ci-dessous dans une cellule de bloc-notes):
%%javascript
// Function that accepts a string of code
var create_and_execute_cell_below = function (code){
var nb = Jupyter.notebook
// create cell below this one
nb.insert_cell_below()
// select cell below (the one we have created)
var cell = nb.select_next().get_selected_cell()
// set text in the cell
cell.set_text(code)
// execute cell
cell.execute()
}
// run the function created above with code 'k = 1'
// and it will create a new cell, filled with 'k = 1'
// and it will execute that cell.
// if you run this cell using [ctrl] + [enter] only one cell
// will be created.
// if you run this cell using [Shift] + [enter] two cells
// will be created, the one of the [Shift] + [enter] command
// and the one of the function created above with the code.
create_and_execute_cell_below('k = 1')
J'espère que ça aide.
OTOH, the front-end API could be not very stable and there is a lack of documentation et certaines choses pourraient changer et peut-être que le code affiché ci-dessus n'est pas la meilleure façon de faire ce dont vous avez besoin.
Pour le moment, cela fonctionne très bien. Propre et net. Vous n'utilisez pas kernel.execute, au moins explicitement. cell.execute peut l'utiliser implicitement. La magie crée et sélectionne la cellule suivante. Sinon, cela fonctionne mais ne s'affiche pas. Merci. – user2800464