Skip to content

Commit

Permalink
add stackoverflow button
Browse files Browse the repository at this point in the history
  • Loading branch information
teticio committed Aug 28, 2023
1 parent 0bd8eff commit d6b4eb1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import '../style/index.css';

import { CodeCell } from '@jupyterlab/cells';
import { IDocumentManager } from '@jupyterlab/docmanager';
import { IEditorTracker, FileEditor } from '@jupyterlab/fileeditor';
import { IOutput } from '@jupyterlab/nbformat';
import { NotebookActions } from '@jupyterlab/notebook';

import {
JupyterFrontEnd,
Expand Down Expand Up @@ -72,6 +75,27 @@ const plugin: JupyterFrontEndPlugin<void> = {
}
}
});

NotebookActions.executed.connect((_, args) => {
const cell: CodeCell = args.cell as CodeCell;
const outputs = cell.model.sharedModel.outputs;

outputs.forEach((output: IOutput) => {
if (output.output_type === 'error') {
const stackTrace: string[] = output.traceback as string[] ?? [];
const searchText = escape(stackTrace[stackTrace.length - 1].replace(/\x1b\[(.*?)([@-~])/g, ''));
const url = 'https://google.com/search?q=' + searchText + '+site:stackoverflow.com';

cell.model.outputs.add({
output_type: 'display_data',
data: {
'text/html': '<br><button class="stack-trace-stackoverflow-btn" onclick="window.open(\'' + url + '\', \'_blank\');">Search Stack Overflow</button>'
}
});
}
});
});

}
}

Expand Down
15 changes: 15 additions & 0 deletions style/base.css
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,18 @@
span.ansi-green-fg {
cursor: pointer;
}

.stack-trace-stackoverflow-btn {
border: 0;
font-size: 11px;
margin: 0px 4px;
padding: 6px;
text-transform: uppercase;
background-color: #f7f7f7;
box-shadow: 0px 3px 4px #aaa;
user-select: none;
}

.stack-trace-stackoverflow-btn:hover {
background-color: #e0e0e0;
}

0 comments on commit d6b4eb1

Please sign in to comment.