Очень простой пример будет (вставьте код ниже в ячейке ноутбука):
%%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')
Я надеюсь, что это помогает.
OTOH, the front-end API could be not very stable and there is a lack of documentation и некоторые вещи могут измениться, и, возможно, код, указанный выше, не лучший способ сделать то, что вам нужно.
На данный момент он отлично работает. Аккуратный и чистый. Вы не используете kernel.execute, по крайней мере, явно. cell.execute может использовать его неявно. Магия создает и выбирает следующую ячейку. В противном случае он работает, но не появляется. Благодарю. – user2800464