dsmit
dsmit

Reputation: 1

Problems executing get_ipython code in Notebook with Papermill

I have a notebook that re-formats results from an exploratory analysis notebook for exporting to slides. This notebook creates new cells based on how many "part types" and "parameters" are passed in. I used the following code to generate a new cell and it works when I tested in the notebook:

def create_code_cell(cell_content, replace=False,):
    shell = get_ipython()
    payload = {
        "source":"set_next_input",
        "text":cell_content,
        "replace":replace,
    }
    shell.payload_manager.write_payload(payload, single=False)

The problem I'm running into is that this function is not creating the cells when I execute with Papermill.

If I open the new notebook that is created from Papermill and click Cells=>Run All the cells are generated.

Thank you for your help!

Upvotes: 0

Views: 278

Answers (1)

cwalvoort
cwalvoort

Reputation: 1967

Papermill uses nbConvert under the hood, which is running the IPython shell without any of the web browser messaging protocols initiated. If you run IPython within a shell and run your commands you have given, no output is generated. The messaging protocols that are set up by running the notebook in the browser are what is listening for these payload updates and creating a new cell from it.

The only other solutions I have seen for programatically creating cells in the notebook relies on JavaScript to run, which won't be executed if your not running within a browser either, aka nbConvert.

Maybe this is just what you already have, but could you set up the generated notebook in such a way that when the user runs it for the first time in a browser, the notebook "expands itself out"?

Upvotes: 1

Related Questions