How to setup CodeWhisperer with JupyterLab?

· 7 min read
How to setup CodeWhisperer with JupyterLab?

Amazon CodeWhisperer (AI coding companion) is now available as an extension for JupyterLab. If you outline the coding task as a comment, this generative AI extension will auto-suggest single-line, multiline or full-function code right within the JupyterLab.  

Here's a guide on how to set up the CodeWhisperer extension with JupyterLab.

If you haven't installed JupyterLab yet, please refer to the article below.

Install JupyterLab or Jupyter Notebook in Windows Subsystem for Linux (WSL2)
Web-based interactive development environment JupyterLab (next generation of Jupyter Notebook ) is very popular in the field of data science and machine learning. Windows Subsystem for Linux (WSL2) is well known for super fast boot time, less resource consumption and seamless integration of Linux gu…
💡
CodeWhisperer extension does not support the newer version JupyterLab 4. 

Install and Enable the CodeWhisperer extension

Install the Amazon CodeWhisperer extension.

abhijit@AwsJunkie:~$ pip install amazon-codewhisperer-jupyterlab-ext
Collecting amazon-codewhisperer-jupyterlab-ext
  Downloading amazon-codewhisperer-jupyterlab-ext-1.0.4.tar.gz (174 kB)
     |████████████████████████████████| 174 kB 4.3 MB/s
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
    Preparing wheel metadata ... done
Collecting botocore
  Downloading botocore-1.31.22-py3-none-any.whl (11.1 MB)
     |████████████████████████████████| 11.1 MB 9.3 MB/s
Collecting boto3
  Downloading boto3-1.28.22-py3-none-any.whl (135 kB)
     |████████████████████████████████| 135 kB 34.5 MB/s
Collecting jmespath<2.0.0,>=0.7.1
  Downloading jmespath-1.0.1-py3-none-any.whl (20 kB)
Requirement already satisfied: urllib3<1.27,>=1.25.4 in /usr/lib/python3/dist-packages (from botocore->amazon-codewhisperer-jupyterlab-ext) (1.25.8)
Requirement already satisfied: python-dateutil<3.0.0,>=2.1 in ./.local/lib/python3.8/site-packages (from botocore->amazon-codewhisperer-jupyterlab-ext) (2.8.2)
Collecting s3transfer<0.7.0,>=0.6.0
  Downloading s3transfer-0.6.1-py3-none-any.whl (79 kB)
     |████████████████████████████████| 79 kB 3.4 MB/s
Requirement already satisfied: six>=1.5 in /usr/lib/python3/dist-packages (from python-dateutil<3.0.0,>=2.1->botocore->amazon-codewhisperer-jupyterlab-ext) (1.14.0)
Building wheels for collected packages: amazon-codewhisperer-jupyterlab-ext
  Building wheel for amazon-codewhisperer-jupyterlab-ext (PEP 517) ... done
  Created wheel for amazon-codewhisperer-jupyterlab-ext: filename=amazon_codewhisperer_jupyterlab_ext-1.0.4-py3-none-any.whl size=106477 sha256=f34460b38c02e63db893831610c5dd5c8cbf4e7e849f22b4a75f798420855095
  Stored in directory: /home/abhijit/.cache/pip/wheels/95/f8/f8/6e7a1b5d0f5dc321a05ba94746514ac40574073008c401c442
Successfully built amazon-codewhisperer-jupyterlab-ext
Installing collected packages: jmespath, botocore, s3transfer, boto3, amazon-codewhisperer-jupyterlab-ext
Successfully installed amazon-codewhisperer-jupyterlab-ext-1.0.4 boto3-1.28.22 botocore-1.31.22 jmespath-1.0.1 s3transfer-0.6.1

Enable the CodeWhisperer extension in JupyterLab.

abhijit@AwsJunkie:~$ jupyter server extension enable amazon_codewhisperer_jupyterlab_ext --user
Enabling: amazon_codewhisperer_jupyterlab_ext
- Writing config: /home/abhijit/.jupyter
    - Validating amazon_codewhisperer_jupyterlab_ext...
      amazon_codewhisperer_jupyterlab_ext  OK
    - Extension successfully enabled.

--user
Apply the operation only for the given user
Equivalent to: [--BaseExtensionApp.user=True]
--system
Apply the operation system-wide
Equivalent to: [--BaseExtensionApp.user=False --BaseExtensionApp.sys_prefix=False]

Launch JupyterLab

Launch JupyterLab jupyter-lab. By default, the Notebook server will be launched at http://localhost:8888/. I have launched JupyterLab on WSL (Windows Subsystem for Linux). So, I have used --no-browser flag and will access remotely from the host Windows machine using the URL with a token (e.g. http://localhost:8888/lab?token=e5743ec65c95a6f474ccdb4dbfff2882edd513ee8a31cead).

abhijit@AwsJunkie:~$ jupyter-lab --no-browser
[I 2023-08-09 13:51:55.522 ServerApp] Package jupyterlab took 0.0000s to import
[I 2023-08-09 13:51:59.935 ServerApp] Package amazon_codewhisperer_jupyterlab_ext took 4.4121s to import
[I 2023-08-09 13:51:59.943 ServerApp] Package jupyter_lsp took 0.0077s to import
[W 2023-08-09 13:51:59.943 ServerApp] A `_jupyter_server_extension_points` function was not found in jupyter_lsp. Instead, a `_jupyter_server_extension_paths` function was found and will be used for now. This function name will be deprecated in future releases of Jupyter Server.
[I 2023-08-09 13:51:59.945 ServerApp] Package jupyter_server_fileid took 0.0018s to import
[I 2023-08-09 13:51:59.948 ServerApp] Package jupyter_server_terminals took 0.0028s to import
[I 2023-08-09 13:51:59.966 ServerApp] Package jupyter_server_ydoc took 0.0175s to import
[I 2023-08-09 13:51:59.967 ServerApp] Package nbclassic took 0.0000s to import
[W 2023-08-09 13:51:59.968 ServerApp] A `_jupyter_server_extension_points` function was not found in nbclassic. Instead, a `_jupyter_server_extension_paths` function was found and will be used for now. This function name will be deprecated in future releases of Jupyter Server.
[I 2023-08-09 13:51:59.969 ServerApp] Package notebook_shim took 0.0000s to import
[W 2023-08-09 13:51:59.969 ServerApp] A `_jupyter_server_extension_points` function was not found in notebook_shim. Instead, a `_jupyter_server_extension_paths` function was found and will be used for now. This function name will be deprecated in future releases of Jupyter Server.
[I 2023-08-09 13:51:59.969 ServerApp] amazon_codewhisperer_jupyterlab_ext | extension was successfully linked.
[I 2023-08-09 13:51:59.969 ServerApp] jupyter_lsp | extension was successfully linked.
[I 2023-08-09 13:51:59.974 ServerApp] jupyter_server_fileid | extension was successfully linked.
[I 2023-08-09 13:51:59.977 ServerApp] jupyter_server_terminals | extension was successfully linked.
[I 2023-08-09 13:51:59.980 ServerApp] jupyter_server_ydoc | extension was successfully linked.
[I 2023-08-09 13:51:59.984 ServerApp] jupyterlab | extension was successfully linked.
[I 2023-08-09 13:51:59.987 ServerApp] nbclassic | extension was successfully linked.
[I 2023-08-09 13:52:00.139 ServerApp] notebook_shim | extension was successfully linked.
[I 2023-08-09 13:52:00.162 ServerApp] notebook_shim | extension was successfully loaded.
[I 2023-08-09 13:52:00.163 ServerApp] Registered amazon_codewhisperer_jupyterlab_ext extension at URL path /amazon_codewhisperer_jupyterlab_ext
[I 2023-08-09 13:52:00.163 ServerApp] amazon_codewhisperer_jupyterlab_ext | extension was successfully loaded.
[I 2023-08-09 13:52:00.165 ServerApp] jupyter_lsp | extension was successfully loaded.
[I 2023-08-09 13:52:00.165 FileIdExtension] Configured File ID manager: ArbitraryFileIdManager
[I 2023-08-09 13:52:00.165 FileIdExtension] ArbitraryFileIdManager : Configured root dir: /home/abhijit
[I 2023-08-09 13:52:00.165 FileIdExtension] ArbitraryFileIdManager : Configured database path: /home/abhijit/.local/share/jupyter/file_id_manager.db
[I 2023-08-09 13:52:00.165 FileIdExtension] ArbitraryFileIdManager : Successfully connected to database file.
[I 2023-08-09 13:52:00.166 FileIdExtension] ArbitraryFileIdManager : Creating File ID tables and indices with journal_mode = DELETE
[I 2023-08-09 13:52:00.166 FileIdExtension] Attached event listeners.
[I 2023-08-09 13:52:00.166 ServerApp] jupyter_server_fileid | extension was successfully loaded.
[I 2023-08-09 13:52:00.167 ServerApp] jupyter_server_terminals | extension was successfully loaded.
[I 2023-08-09 13:52:00.167 ServerApp] jupyter_server_ydoc | extension was successfully loaded.
[I 2023-08-09 13:52:00.168 LabApp] JupyterLab extension loaded from /home/abhijit/.local/lib/python3.8/site-packages/jupyterlab
[I 2023-08-09 13:52:00.168 LabApp] JupyterLab application directory is /home/abhijit/.local/share/jupyter/lab
[I 2023-08-09 13:52:00.170 ServerApp] jupyterlab | extension was successfully loaded.

  _   _          _      _
 | | | |_ __  __| |__ _| |_ ___
 | |_| | '_ \/ _` / _` |  _/ -_)
  \___/| .__/\__,_\__,_|\__\___|
       |_|

Read the migration plan to Notebook 7 to learn about the new features and the actions to take if you are using extensions.

https://jupyter-notebook.readthedocs.io/en/latest/migrate_to_notebook7.html

Please note that updating to Notebook 7 might break some of your extensions.

[I 2023-08-09 13:52:00.173 ServerApp] nbclassic | extension was successfully loaded.
[I 2023-08-09 13:52:00.173 ServerApp] Serving notebooks from local directory: /home/abhijit
[I 2023-08-09 13:52:00.173 ServerApp] Jupyter Server 2.7.0 is running at:
[I 2023-08-09 13:52:00.173 ServerApp] http://localhost:8888/lab?token=0639d4e70283069033997ca85218ceb00f6e36e7a503de73
[I 2023-08-09 13:52:00.173 ServerApp]     http://127.0.0.1:8888/lab?token=0639d4e70283069033997ca85218ceb00f6e36e7a503de73
[I 2023-08-09 13:52:00.173 ServerApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 2023-08-09 13:52:00.176 ServerApp]

    To access the server, open this file in a browser:
        file:///home/abhijit/.local/share/jupyter/runtime/jpserver-20734-open.html
    Or copy and paste one of these URLs:
        http://localhost:8888/lab?token=0639d4e70283069033997ca85218ceb00f6e36e7a503de73
        http://127.0.0.1:8888/lab?token=0639d4e70283069033997ca85218ceb00f6e36e7a503de73
[I 2023-08-09 13:52:00.192 ServerApp] Skipped non-installed server(s): bash-language-server, dockerfile-language-server-nodejs, javascript-typescript-langserver, jedi-language-server, julia-language-server, pyright, python-language-server, python-lsp-server, r-languageserver, sql-language-server, texlab, typescript-language-server, unified-language-server, vscode-css-languageserver-bin, vscode-html-languageserver-bin, vscode-json-languageserver-bin, yaml-language-server

Setup CodeWhisperer using AWS Builder ID

Click "Start CodeWhisperer".

Click "Copy Code and Proceed".

"To use CodeWhisperer, individual developers must sign in using AWS Builder ID". If you don't have one, then create one. It is free.

Now, allow CodeWhisperer for JupyterLab to access your data and generate suggestions in code.

You'll see the following message "CodeWhisperer for JupyterLab can now access your data."

Now, on clicking "CodeWhisperer", you'll see the below menu.

Start building awesome apps faster and more securely with Amazon CodeWhisperer, your AI coding companion within JupyterLab.

References