2023年3月3日金曜日

Useful jupyter lab extension: jupyterlab_templates

 I daily use the jupyter lab for data analysis workflow. Recently I updated the jupyter lab and I found that the jupyterlab extension manager can be available. Since then I frequently check the jupyterlab extension to make my jupyter environment more useful and productive. 

Jupyterlab Extension Manager -- T.Y. Blog

 There are some stuffs I always use in the notebook such as description markdown cells and import-modules cells for notebooks. Today I found an extension for jupyterlab that seems useful for setting up templates.

Jupyterlab_template

Support for jupyter notebook templates in jupyterlab

The readme can be available from the github page.



Installation

Installation guide is available in github README.md. Since I use conda for package management, I install jupyterlab_templates using  conda install 



(base) tk$ conda install -c conda-forge jupyterlab_templates

Retrieving notices: ...working... done

Collecting package metadata (current_repodata.json): done

Solving environment: done


## Package Plan ##


  environment location: /Path/to/miniconda3


  added / updated specs:

    - jupyterlab_templates



The following packages will be downloaded:


    package                    |            build

    ---------------------------|-----------------

    jupyterlab_templates-0.3.2 |     pyh1d7be83_0          72 KB  conda-forge

    ------------------------------------------------------------

                                           Total:          72 KB


The following NEW packages will be INSTALLED:


  jupyterlab_templa~ conda-forge/noarch::jupyterlab_templates-0.3.2-pyh1d7be83_0 



Proceed ([y]/n)? y



Then I install jupyterlab_templates on Extension Manager,




Configuration file

Next, adding parameters on  jupyter_notebook_config.py  is necessary to let jupyterlab find the template files in  template_dirs . The jupyter_notebook_config.py is a configration file of jupyterlab. If there is no file on your system yet, it can be generated by


(base) tk$ jupyter lab --generate-config

Writing default config to: /Path/to/home/.jupyter/jupyter_lab_config.py



I edited this file on the terminal to add three lines below,


c.JupyterLabTemplates.template_dirs = ['/Path/to/template/directory']

c.JupyterLabTemplates.include_default = True

c.JupyterLabTemplates.include_core_paths = True

^G Get Help       ^O WriteOut       ^R Read File      ^Y Prev Pg        ^K Cut Text       ^C Cur Pos        

^X Exit           ^J Justify        ^W Where is       ^V Next Pg        ^U UnCut Text     ^T To Spell       


Now the setup of this extension was completed.



Using template from Launcher

I restart the jupyter lab and found the template button was added on Launcher.




Then The preparation of jupyterlab_extension was completed. When clicking the "Template" on Notebook, I can choose the ipynb template on template directory.



Note that the updates of template directories and notebooks are not reflected until the system is once restarted. 



References