Enable Parameterization via Notebook URI (#13869)

* Enable Parameterization via Notebook URI

* Add Parameterization and Move notebookModel tests

* minor typos

* parameter typo

* Multiple parameters through uri fix

* Address PR comments and tests fix

* add new injected parameters after original injected parameters

* Add new test to verify notebookURi parameter is injected after both parameter and injected cell

* fix tests
This commit is contained in:
Vasu Bhog
2021-01-08 00:38:33 -08:00
committed by GitHub
parent 5d1b328866
commit df51f35be0
3 changed files with 238 additions and 9 deletions

View File

@@ -49,6 +49,7 @@ export class ErrorInfo {
}
const saveConnectionNameConfigName = 'notebook.saveConnectionName';
const injectedParametersMsg = localize('injectedParametersMsg', '# Injected-Parameters\n');
export class NotebookModel extends Disposable implements INotebookModel {
private _contextsChangedEmitter = new Emitter<void>();
@@ -400,9 +401,20 @@ export class NotebookModel extends Disposable implements INotebookModel {
}
});
}
// Modify Notebook URI Params format from URI query to string space delimited format
let notebookUriParams: string = this.notebookUri.query;
notebookUriParams = notebookUriParams.replace(/&/g, '\n').replace(/=/g, ' = ');
// Get parameter cell and index to place new notebookUri parameters accordingly
let parameterCellIndex = 0;
let hasParameterCell = false;
let hasInjectedCell = false;
if (contents.cells && contents.cells.length > 0) {
this._cells = contents.cells.map(c => {
let cellModel = factory.createCell(c, { notebook: this, isTrusted: isTrusted });
if (cellModel.isParameter) {
parameterCellIndex = contents.cells.indexOf(c);
hasParameterCell = true;
}
/*
In a parameterized notebook there will be an injected parameter cell.
Papermill originally inserts the injected parameter with the comment "# Parameters"
@@ -410,13 +422,18 @@ export class NotebookModel extends Disposable implements INotebookModel {
So to make it clear we edit the injected parameters comment to indicate it is the Injected-Parameters cell.
*/
if (cellModel.isInjectedParameter) {
hasInjectedCell = true;
cellModel.source = cellModel.source.slice(1);
cellModel.source = ['# Injected-Parameters\n'].concat(cellModel.source);
cellModel.source = [injectedParametersMsg].concat(cellModel.source);
}
this.trackMarkdownTelemetry(<nb.ICellContents>c, cellModel);
return cellModel;
});
}
// Only add new parameter cell if notebookUri Parameters are found
if (notebookUriParams) {
this.addUriParameterCell(notebookUriParams, hasParameterCell, parameterCellIndex, hasInjectedCell);
}
}
// Trust notebook by default if there are no code cells
@@ -431,6 +448,7 @@ export class NotebookModel extends Disposable implements INotebookModel {
throw error;
}
}
public async requestModelLoad(): Promise<void> {
try {
this.setDefaultKernelAndProviderId();
@@ -487,6 +505,33 @@ export class NotebookModel extends Disposable implements INotebookModel {
return cell;
}
/**
* Adds Parameters cell based on Notebook URI parameters
* @param notebookUriParams contains the parameters from Notebook URI
* @param hasParameterCell notebook contains a parameter cell
* @param parameterCellIndex index of the parameter cell in notebook
* @param hasInjectedCell notebook contains a injected parameter cell
*/
private addUriParameterCell(notebookUriParams: string, hasParameterCell: boolean, parameterCellIndex: number, hasInjectedCell: boolean): void {
let uriParamsIndex = parameterCellIndex;
// Set new uri parameters as a Injected Parameters cell after original parameter cell
if (hasParameterCell) {
uriParamsIndex = parameterCellIndex + 1;
// Set the uri parameters after the injected parameter cell
if (hasInjectedCell) {
uriParamsIndex = uriParamsIndex + 1;
}
this.addCell('code', uriParamsIndex);
this.cells[uriParamsIndex].isInjectedParameter = true;
this.cells[uriParamsIndex].source = [injectedParametersMsg].concat(notebookUriParams);
} else {
// Set new parameters as the parameters cell as the first cell in the notebook
this.addCell('code', uriParamsIndex);
this.cells[uriParamsIndex].isParameter = true;
this.cells[uriParamsIndex].source = [notebookUriParams];
}
}
moveCell(cell: ICellModel, direction: MoveDirection): void {
if (this.inErrorState) {
return;