@@ -331,51 +331,51 @@ your extension needs to be defined as a proper Python package with some hook fun
331
331
# jlab_ext_example / __init__ .py
332
332
333
333
import json
334
- import os . path as osp
334
+ from pathlib import Path
335
335
336
336
from .handlers import setup_handlers
337
337
from ._version import __version__
338
338
339
- HERE = osp . abspath ( osp . dirname ( __file__ ))
339
+ HERE = Path ( __file__ ). parent . resolve ( )
340
340
341
- with open ( osp . join ( HERE , ' labextension' , ' package.json' ) ) as fid :
341
+ with ( HERE / " labextension" / " package.json" ). open ( ) as fid :
342
342
data = json .load (fid )
343
343
344
+
344
345
def _jupyter_labextension_paths ():
345
- return [{
346
- ' src' : ' labextension' ,
347
- ' dest' : data [' name' ]
348
- }]
346
+ return [{" src" : " labextension" , " dest" : data [" name" ]}]
349
347
350
348
351
- def _jupyter_server_extension_paths ():
349
+ def _jupyter_server_extension_points ():
352
350
return [{" module" : " jlab_ext_example" }]
353
351
354
352
355
- def load_jupyter_server_extension ( lab_app ):
353
+ def _load_jupyter_server_extension ( server_app ):
356
354
" " " Registers the API handler to receive HTTP requests from the frontend extension.
357
355
Parameters
358
356
----------
359
- lab_app : jupyterlab .labapp .LabApp
357
+ server_app : jupyterlab .labapp .LabApp
360
358
JupyterLab application instance
361
359
" " "
362
360
url_path = " jlab - ext - example "
363
- setup_handlers(lab_app .web_app, url_path)
364
- lab_app .log .info (
365
- " Registered jlab_ext_example extension at URL path /{} " . format ( url_path )
361
+ setup_handlers(server_app .web_app, url_path)
362
+ server_app .log .info (
363
+ f " Registered jlab_ext_example extension at URL path /{url_path} "
366
364
)
367
365
366
+ # For backward compatibility with the classical notebook
367
+ load_jupyter_server_extension = _load_jupyter_server_extension
368
368
369
369
` ` `
370
370
371
- The ` _jupyter_server_extension_paths ` provides the Python package name
372
- to the server. But the most important one is ` load_jupyter_server_extension `
371
+ The ` _jupyter_server_extension_points ` provides the Python package name
372
+ to the server. But the most important one is ` _load_jupyter_server_extension `
373
373
that register new handlers.
374
374
375
375
` ` ` py
376
- # jlab_ext_example / __init__ .py #L31 - L31
376
+ # jlab_ext_example / __init__ .py #L29 - L29
377
377
378
- setup_handlers (lab_app .web_app , url_path )
378
+ setup_handlers (server_app .web_app , url_path )
379
379
` ` `
380
380
381
381
A handler is registered in the web application by linking an url to a class. In this
@@ -496,81 +496,77 @@ The `setup.py` file is the entry point to describe package metadata:
496
496
jlab_ext_example setup
497
497
" " "
498
498
import json
499
- import os
499
+ from pathlib import Path
500
500
501
501
from jupyter_packaging import (
502
- create_cmdclass , install_npm , ensure_targets ,
503
- combine_commands , skip_if_exists
502
+ create_cmdclass ,
503
+ install_npm ,
504
+ ensure_targets ,
505
+ combine_commands ,
506
+ skip_if_exists ,
504
507
)
505
508
import setuptools
506
509
507
- HERE = os . path . abspath ( os . path . dirname ( __file__ ) )
510
+ HERE = Path ( __file__ ). parent . resolve ( )
508
511
509
512
# The name of the project
510
- name =" jlab_ext_example"
511
-
512
- # Get our version
513
- with open (os .path .join (HERE , ' package.json' )) as f :
514
- version = json .load (f )[' version' ]
513
+ name = " jlab_ext_example"
515
514
516
- lab_path = os . path . join ( HERE , name , " labextension" )
515
+ lab_path = HERE / name / " labextension"
517
516
518
517
# Representative files that should exist after a successful build
519
518
jstargets = [
520
- os . path . join (lab_path , " package.json" ),
519
+ str (lab_path / " package.json" ),
521
520
]
522
521
523
- package_data_spec = {
524
- name : [
525
- "*"
526
- ]
527
- }
522
+ package_data_spec = {name : ["*" ]}
528
523
529
524
labext_name = " @jupyterlab-examples/server-extension"
530
525
531
526
data_files_spec = [
532
- (" share/jupyter/labextensions/%s" % labext_name , lab_path , " **" ),
533
- (" share/jupyter/labextensions/%s" % labext_name , HERE , " install.json" ),( " etc/jupyter/jupyter_server_config.d " ,
534
- " jupyter-config" , " jlab_ext_example.json" ),
535
-
527
+ (" share/jupyter/labextensions/%s" % labext_name , str ( lab_path ) , " **" ),
528
+ (" share/jupyter/labextensions/%s" % labext_name , str ( HERE ) , " install.json" ),
529
+ ( " etc/jupyter/jupyter_notebook_config.d " , " jupyter-config/jupyter_notebook_config.d " , " jlab_ext_example.json" ),
530
+ ( " etc/jupyter/jupyter_server_config.d " , " jupyter-config/jupyter_server_config.d " , " jlab_ext_example.json " ),
536
531
]
537
532
538
- cmdclass = create_cmdclass (" jsdeps" ,
539
- package_data_spec =package_data_spec ,
540
- data_files_spec =data_files_spec
533
+ cmdclass = create_cmdclass (
534
+ " jsdeps" , package_data_spec =package_data_spec , data_files_spec =data_files_spec
541
535
)
542
536
543
537
js_command = combine_commands (
544
538
install_npm (HERE , build_cmd =" build:prod" , npm =[" jlpm" ]),
545
539
ensure_targets (jstargets ),
546
540
)
547
541
548
- is_repo = os . path . exists ( os . path . join ( HERE , " .git" ))
542
+ is_repo = ( HERE / " .git" ). exists ( )
549
543
if is_repo :
550
544
cmdclass [" jsdeps" ] = js_command
551
545
else :
552
546
cmdclass [" jsdeps" ] = skip_if_exists (jstargets , js_command )
553
547
554
- with open (" README.md" , " r" ) as fh :
555
- long_description = fh .read ()
548
+ long_description = (HERE / " README.md" ).read_text ()
549
+
550
+ # Get the package info from package .json
551
+ pkg_json = json .loads ((HERE / " package.json" ).read_bytes ())
556
552
557
553
setup_args = dict (
558
554
name = name ,
559
- version =version ,
560
- url =" https://github.com/jupyterlab/extension-examples.git" ,
561
- author =" Project Jupyter Contributors" ,
562
- description =" A minimal JupyterLab extension with backend and frontend parts." ,
563
- long_description = long_description ,
555
+ version = pkg_json [" version" ],
556
+ url = pkg_json [" homepage" ],
557
+ author = pkg_json [" author" ],
558
+ description = pkg_json [" description" ],
559
+ license = pkg_json [" license" ],
560
+ long_description = long_description ,
564
561
long_description_content_type = " text/markdown" ,
565
- cmdclass = cmdclass ,
562
+ cmdclass = cmdclass ,
566
563
packages = setuptools .find_packages (),
567
564
install_requires = [
568
- " jupyterlab> =3.0.0rc15,==3.* " ,
565
+ " jupyterlab~ =3.0" ,
569
566
],
570
567
zip_safe = False ,
571
568
include_package_data = True ,
572
569
python_requires = " >=3.6" ,
573
- license =" BSD-3-Clause" ,
574
570
platforms = " Linux, Mac OS X, Windows" ,
575
571
keywords = [" Jupyter" , " JupyterLab" , " JupyterLab3" ],
576
572
classifiers = [
@@ -580,6 +576,7 @@ setup_args = dict(
580
576
" Programming Language :: Python :: 3.6" ,
581
577
" Programming Language :: Python :: 3.7" ,
582
578
" Programming Language :: Python :: 3.8" ,
579
+ " Programming Language :: Python :: 3.9" ,
583
580
" Framework :: Jupyter" ,
584
581
],
585
582
)
@@ -596,11 +593,10 @@ the frontend NPM package needs to be built and inserted in the Python package. T
596
593
done using a special ` cmdclass ` :
597
594
598
595
` ` ` py
599
- # setup .py #L44 - L52
596
+ # setup .py #L39 - L46
600
597
601
- cmdclass = create_cmdclass (" jsdeps" ,
602
- package_data_spec =package_data_spec ,
603
- data_files_spec =data_files_spec
598
+ cmdclass = create_cmdclass (
599
+ " jsdeps" , package_data_spec = package_data_spec , data_files_spec = data_files_spec
604
600
)
605
601
606
602
js_command = combine_commands (
@@ -612,28 +608,29 @@ js_command = combine_commands(
612
608
Basically it will build the frontend NPM package:
613
609
614
610
` ` ` py
615
- # setup .py #L50 - L50
611
+ # setup .py #L44 - L44
616
612
617
613
install_npm (HERE , build_cmd = " build:prod" , npm = [" jlpm" ]),
618
614
` ` `
619
615
620
616
It will ensure one of the generated files is ` jlab_ext_example / labextension / package .json ` :
621
617
622
618
` ` ` py
623
- # setup .py #L24 - L27
619
+ # setup .py #L23 - L26
624
620
625
621
# Representative files that should exist after a successful build
626
622
jstargets = [
627
- os . path . join (lab_path , " package.json" ),
623
+ str (lab_path / " package.json" ),
628
624
]
629
625
` ` `
630
626
631
627
It will copy the NPM package in the Python package and force it to be copied in a place
632
628
JupyterLab is looking for frontend extensions when the Python package is installed:
633
629
634
630
` ` ` py
631
+ # setup .py #L33 - L33
635
632
636
- (" share/jupyter/labextensions/%s" % labext_name , lab_path , " *. *" )
633
+ (" share/jupyter/labextensions/%s" % labext_name , str ( lab_path ) , " **" ),
637
634
` ` `
638
635
639
636
The last piece of configuration needed is the enabling of the server extension. This is
@@ -655,9 +652,18 @@ done by copying the following JSON file:
655
652
in the appropriate jupyter folder ( ` etc / jupyter / jupyter_server_config .d ` ):
656
653
657
654
` ` ` py
655
+ # setup .py #L36 - L36
656
+
657
+ (" etc/jupyter/jupyter_server_config.d" , " jupyter-config/jupyter_server_config.d" , " jlab_ext_example.json" ),
658
+ ` ` `
659
+
660
+ For backward compatibility with the classical notebook, the old version of that file is copied in
661
+ ( ` etc / jupyter / jupyter_notebook_config .d ` ):
662
+
663
+ ` ` ` py
664
+ # setup .py #L35 - L35
658
665
659
- (" etc/jupyter/jupyter_server_config.d" ,
660
- " jupyter-config" , " jlab_ext_example.json" ),
666
+ (" etc/jupyter/jupyter_notebook_config.d" , " jupyter-config/jupyter_notebook_config.d" , " jlab_ext_example.json" ),
661
667
` ` `
662
668
663
669
### JupyterLab Extension Manager
0 commit comments