<jupyter_start><jupyter_text>Creating templates  Programmatically creating new cells - first takeTrying to use IPython functionallity to make new cells<jupyter_code>import IPython
def create_new_cell(contents):
    # https://stackoverflow.com/questions/54987129/
    # how-to-programmatically-create-several-new-cells-in-a-jupyter-notebook
    
    from IPython.core.getipython import get_ipython
    shell = get_ipython()

    payload = dict(
        source='set_next_input',
        text=contents,
        replace=False,
    )
    shell.payload_manager.write_payload(payload, single=False)
for j in range(3):
    r = create_new_cell(f"print('hello {j}')")
shell = IPython.core.getipython.get_ipython()
shell.starting_dir
# %notebook "new.ipynb"<jupyter_output><empty_output><jupyter_text>Result:Works only within jupyter, not jupyterlab. However, it could be used for loading just a single cell.  Testing PaperMill<jupyter_code>in_nb = '/users/jepe/scripting/cellpy/dev_utils/papermill/pm_input_test.ipynb'
out_nb = '/users/jepe/scripting/cellpy/dev_utils/papermill/pm_output_test.ipynb'
import papermill as pm
from pathlib import Path
def create_template(template_name, autorun=False):
    datestamp = '20190810'
    name = 'test'
    out_dir = '/users/jepe/scripting/cellpy/dev_utils/papermill'
    in_nb = input_notebook(template_name)
    out_nb = output_notebook(template_name, out_dir=out_dir, datestamp=datestamp, name=name)
    print(in_nb)
    print(out_nb)
    nb_node = pm.execute_notebook(
        str(in_nb.resolve()),
        str(out_nb.resolve()),
        prepare_only=not autorun,
        parameters=dict(a=3, b='anna')
    )
    
    return nb_node
def output_notebook(template, out_dir=None, datestamp='20190810', name='test'):
    if out_dir is None:
        out_dir = Path()
    fname = f'{datestamp}_{name}_{template}.ipynb'
    path = Path(out_dir) / fname
    return path
def input_notebook(template='pm_input_test'):
    in_dir = '/users/jepe/scripting/cellpy/dev_utils/papermill'
    fname = f'{template}.ipynb'
    path = Path(in_dir) / fname
    return path
nb_node = create_template('pm_input_test', autorun=False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11085556577736891, 'fraction_numerical': 0.011959521619135235, 'mean_word_length': 4.357142857142857, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '216314', 'license': 'MIT', 'n_tokens_mistral': 802, 'n_tokens_neox': 764, 'n_words': 135, 'path': 'dev_utils/template_creation.ipynb', 'repo_name': 'lithium-ion/cellpy', 'size': '2174'}