1

I want to programmatically create several markdown cells in a Jupyter notebook.

the following function works for code cells, but not markdown cells.

def create_new_cell(contents):
    from IPython.core.getipython import get_ipython
    shell = get_ipython()

    payload = {
        "source": "set_next_input",
        "text": contents,
        "replace": False
    }
    shell.payload_manager.write_payload(payload, single=False)

I try to add metadata setting, but it doesn't work.

def create_new_cell(contents):
    from IPython.core.getipython import get_ipython
    shell = get_ipython()

    payload = {
        "source": "set_next_input",
        "text": contents,
        "metadata": {"cell_type": "markdown"},
        "replace": False
    }
    shell.payload_manager.write_payload(payload, single=False)

Is there any other solution?

2
  • Please cite/ link the source of your first code block. (Maybe from here?) Thanks. Commented Nov 28, 2023 at 18:56
  • Given this exchange 'How to programmatically create a markdown cell?' I don't think it is possible directly running it in the notebook itself. ipylab may be a way (?), see under 'Execute Commands' here, but I haven't found the magic combination yet. Commented Nov 28, 2023 at 19:23

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.