github.com/apache/beam/sdks/v2@v2.48.2/python/apache_beam/runners/interactive/extensions/apache-beam-jupyterlab-sidepanel/src/SidePanel.ts (about)

     1  // Licensed under the Apache License, Version 2.0 (the 'License'); you may not
     2  // use this file except in compliance with the License. You may obtain a copy of
     3  // the License at
     4  //
     5  //   http://www.apache.org/licenses/LICENSE-2.0
     6  //
     7  // Unless required by applicable law or agreed to in writing, software
     8  // distributed under the License is distributed on an 'AS IS' BASIS, WITHOUT
     9  // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    10  // License for the specific language governing permissions and limitations under
    11  // the License.
    12  
    13  import {
    14    ReactWidget,
    15    SessionContext,
    16    ISessionContext,
    17    sessionContextDialogs
    18  } from '@jupyterlab/apputils';
    19  import { IRenderMimeRegistry } from '@jupyterlab/rendermime';
    20  import { ServiceManager } from '@jupyterlab/services';
    21  import { Message } from '@lumino/messaging';
    22  import { BoxPanel } from '@lumino/widgets';
    23  
    24  /**
    25   * The side panel: main user interface of the extension.
    26   *
    27   * Multiple instances of the side panel can be opened at the same time. They
    28   * can be operated independently but sharing the same kernel state if connected
    29   * to the same notebook session model or running kernel instance.
    30   */
    31  export class SidePanel extends BoxPanel {
    32    constructor(
    33      manager: ServiceManager.IManager,
    34      rendermime: IRenderMimeRegistry,
    35      sessionContext: SessionContext,
    36      title: string,
    37      widget: ReactWidget
    38    ) {
    39      super({
    40        direction: 'top-to-bottom',
    41        alignment: 'end'
    42      });
    43      this.id = 'apache-beam-jupyterlab-sidepanel';
    44      this.title.label = title;
    45      this.title.closable = true;
    46      this._sessionContext = sessionContext;
    47      this._widget = widget;
    48      this.addWidget(this._widget);
    49      this.initializeSession(manager);
    50    }
    51  
    52    async initializeSession(manager: ServiceManager.IManager): Promise<void> {
    53      const sessionContext = await this._sessionContext.initialize();
    54      if (!sessionContext) {
    55        console.error('Cannot initialize the session in SidePanel.');
    56        return;
    57      }
    58      const sessionModelItr = manager.sessions.running();
    59      const firstModel = sessionModelItr.next();
    60      let onlyOneUniqueKernelExists = true;
    61      if (firstModel === undefined) {
    62        // There is zero unique running kernel.
    63        onlyOneUniqueKernelExists = false;
    64      } else {
    65        let sessionModel = sessionModelItr.next();
    66        while (sessionModel !== undefined) {
    67          if (sessionModel.kernel.id !== firstModel.kernel.id) {
    68            // There is more than one unique running kernel.
    69            onlyOneUniqueKernelExists = false;
    70            break;
    71          }
    72          sessionModel = sessionModelItr.next();
    73        }
    74      }
    75      try {
    76        // Create a new notebook session with the same model of the first
    77        // session (any session would suffice) when there is only one running
    78        // kernel.
    79        if (onlyOneUniqueKernelExists) {
    80          this._sessionContext.sessionManager.connectTo({
    81            model: firstModel,
    82            kernelConnectionOptions: {
    83              // Only one connection can handleComms. Leave it to the connection
    84              // established by the opened notebook.
    85              handleComms: false
    86            }
    87          });
    88          // Connect to the unique kernel.
    89          this._sessionContext.changeKernel(firstModel.kernel);
    90        } else {
    91          // Let the user choose among sessions and kernels when there is no
    92          // or more than 1 running kernels.
    93          await sessionContextDialogs.selectKernel(this._sessionContext);
    94        }
    95      } catch (err) {
    96        console.error(`Failed to initialize the session in SidePanel.\n${err}`);
    97      }
    98    }
    99  
   100    get session(): ISessionContext {
   101      return this._sessionContext;
   102    }
   103  
   104    dispose(): void {
   105      this._sessionContext.dispose();
   106      super.dispose();
   107    }
   108  
   109    protected onCloseRequest(msg: Message): void {
   110      super.onCloseRequest(msg);
   111      this.dispose();
   112    }
   113  
   114    private _widget: ReactWidget;
   115    private _sessionContext: SessionContext;
   116  }