def replace_parameters(context, nb, parameters): """Assigned parameters into the appropiate place in the input notebook Args: nb (NotebookNode): Executable notebook object parameters (dict): Arbitrary keyword arguments to pass to the notebook parameters. """ check.dict_param(parameters, "parameters") # Copy the nb object to avoid polluting the input nb = copy.deepcopy(nb) # papermill method chooses translator based on kernel_name and language, but we just call the # DagsterTranslator to generate parameter content based on the kernel_name param_content = DagsterTranslator.codify(parameters) newcell = nbformat.v4.new_code_cell(source=param_content) newcell.metadata["tags"] = ["injected-parameters"] param_cell_index = _find_first_tagged_cell_index(nb, "parameters") injected_cell_index = _find_first_tagged_cell_index(nb, "injected-parameters") if injected_cell_index >= 0: # Replace the injected cell with a new version before = nb.cells[:injected_cell_index] after = nb.cells[injected_cell_index + 1 :] check.int_value_param(param_cell_index, -1, "param_cell_index") # We should have blown away the parameters cell if there is an injected-parameters cell elif param_cell_index >= 0: # Replace the parameter cell with the injected-parameters cell before = nb.cells[:param_cell_index] after = nb.cells[param_cell_index + 1 :] else: # Inject to the top of the notebook, presumably first cell includes dagstermill import context.log.debug( ( "Executing notebook with no tagged parameters cell: injecting boilerplate in first " "cell." ) ) before = [] after = nb.cells nb.cells = before + [newcell] + after nb.metadata.papermill["parameters"] = seven.json.dumps(parameters) return nb
def replace_parameters(context, nb, parameters): '''Assigned parameters into the appropiate place in the input notebook Args: nb (NotebookNode): Executable notebook object parameters (dict): Arbitrary keyword arguments to pass to the notebook parameters. ''' check.dict_param(parameters, 'parameters') # Copy the nb object to avoid polluting the input nb = copy.deepcopy(nb) # papermill method chooses translator based on kernel_name and language, but we just call the # DagsterTranslator to generate parameter content based on the kernel_name param_content = DagsterTranslator.codify(parameters) newcell = nbformat.v4.new_code_cell(source=param_content) newcell.metadata['tags'] = ['injected-parameters'] param_cell_index = _find_first_tagged_cell_index(nb, 'parameters') injected_cell_index = _find_first_tagged_cell_index( nb, 'injected-parameters') if injected_cell_index >= 0: # Replace the injected cell with a new version before = nb.cells[:injected_cell_index] after = nb.cells[injected_cell_index + 1:] check.int_value_param(param_cell_index, -1, 'param_cell_index') # We should have blown away the parameters cell if there is an injected-parameters cell elif param_cell_index >= 0: # Replace the parameter cell with the injected-parameters cell before = nb.cells[:param_cell_index] after = nb.cells[param_cell_index + 1:] else: # Inject to the top of the notebook, presumably first cell includes dagstermill import context.log.debug(( 'Warning notebook has no parameters cell, ' 'so first cell must import dagstermill and call dm.register_repository()' )) before = nb.cells[:1] after = nb.cells[1:] nb.cells = before + [newcell] + after nb.metadata.papermill['parameters'] = parameters return nb