Skip to content

Commit

Permalink
make extension start automatically and only if has visualization fiel…
Browse files Browse the repository at this point in the history
…d in metadata
  • Loading branch information
mchami02 committed Aug 1, 2024
1 parent 3e3301e commit b851917
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 42 deletions.
75 changes: 33 additions & 42 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {

import { FlowchartWidget } from './graphWidget';
import { NotebookManager } from './notebookManager';
import { ICommandPalette } from '@jupyterlab/apputils';


import { OpenNotebookButton } from './openNotebookButton';
import { NotebookSelector } from './notebookSelector';
Expand All @@ -27,59 +27,50 @@ import { NotebookSelector } from './notebookSelector';
const plugin: JupyterFrontEndPlugin<void> = {
id: 'sidebar-cluster:plugin',
autoStart: true,
requires: [ICommandPalette, INotebookTracker, IFileBrowserFactory],
activate: (app: JupyterFrontEnd, palette: ICommandPalette, tracker: INotebookTracker, factory: IFileBrowserFactory) => {
requires: [INotebookTracker, IFileBrowserFactory],
activate: (app: JupyterFrontEnd, tracker: INotebookTracker, factory: IFileBrowserFactory) => {

console.log('JupyterLab extension sidebar-cluster is activated!');
const notebookManager = new NotebookManager();
const graphWidget = new FlowchartWidget(notebookManager);
const notebookSelector = new NotebookSelector(notebookManager, graphWidget);

// When a notebook is opened, add the custom widget to the top
tracker.widgetAdded.connect((sender, notebook) => {
console.log('Notebook added');
const toolbar = notebook.toolbar.node;
const customWidget = notebookSelector.createSelector();

// Insert the custom widget at the beginning of the toolbar
toolbar.parentNode?.insertBefore(customWidget.node, toolbar.parentNode?.nextSibling as Node);
});
tracker.widgetAdded.connect((sender, panel) => {
panel.context.ready.then(() => {

panel.content.model?.sharedModel.getMetadata('visualization');
if (!panel.content.model?.sharedModel.getMetadata('visualization')){
return;
}

// Initialize the classes
const notebookManager = new NotebookManager();
const graphWidget = new FlowchartWidget(notebookManager);
const notebookSelector = new NotebookSelector(notebookManager, graphWidget);

app.commands.addCommand('sidebar-cluster:open', {
label: 'Generate sidebar graphs',
caption: 'Generate sidebar graphs',
execute: () => {
const panel = tracker.currentWidget;
if (panel) {
// Instantiating the notebook manager
notebookManager.populateCells(panel.content.widgets);
notebookManager.showAllNotebooks();
// Create a new button extension
notebookManager.showNotebooks([]);

// Adding the toolbar selector
const toolbar = panel.toolbar.node;
const customWidget = notebookSelector.createSelector();
toolbar.parentNode?.insertBefore(customWidget.node, toolbar.parentNode?.nextSibling as Node);
notebookSelector.addOptions();
console.log('Selector added to toolbar');

// Create the Open Notebooks button
const buttonExtension = new OpenNotebookButton(notebookManager, factory);
panel.toolbar.insertItem(11, 'showNotebook', buttonExtension.createButton());
console.log('Button extension added to notebook');
}
else {
console.log('No notebook is open');
}

graphWidget.id = 'my-sidebar-graph-widget';
graphWidget.title.iconClass = 'jp-SideBar-tabIcon';
graphWidget.title.caption = 'My Sidebar Graph Widget';

// Add the sidebar to the left area
app.shell.add(graphWidget, 'left');
app.shell.activateById(graphWidget.id);
// Add the graph widget to the sidebar
graphWidget.id = 'my-sidebar-graph-widget';
graphWidget.title.iconClass = 'jp-SideBar-tabIcon';
graphWidget.title.caption = 'My Sidebar Graph Widget';
app.shell.add(graphWidget, 'left');
app.shell.activateById(graphWidget.id);
console.log('Graph widget added to sidebar');

// console.log('Changing height');
// const mainArea = document.querySelector('.jp-Notebook') as HTMLElement;
// mainArea.style.marginTop = '50px'; // Adjust this value to push the main area down

notebookSelector.addOptions();
}
});
});

palette.addItem({ command: 'sidebar-cluster:open', category: 'Extension' });
}
};

Expand Down
1 change: 1 addition & 0 deletions test_files/new_format.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
}
],
"metadata": {
"visualization": true,
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
Expand Down

0 comments on commit b851917

Please sign in to comment.