@@ -331,51 +331,48 @@ 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
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
368
-
369
366
` ` `
370
367
371
368
The ` _jupyter_server_extension_points ` provides the Python package name
372
369
to the server. But the most important one is ` _load_jupyter_server_extension `
373
370
that register new handlers.
374
371
375
372
` ` ` py
376
- # jlab_ext_example / __init__ .py #L31 - L31
373
+ # jlab_ext_example / __init__ .py #L29 - L29
377
374
378
- setup_handlers (lab_app .web_app , url_path )
375
+ setup_handlers (server_app .web_app , url_path )
379
376
` ` `
380
377
381
378
A handler is registered in the web application by linking an url to a class. In this
@@ -496,80 +493,76 @@ The `setup.py` file is the entry point to describe package metadata:
496
493
jlab_ext_example setup
497
494
" " "
498
495
import json
499
- import os
496
+ from pathlib import Path
500
497
501
498
from jupyter_packaging import (
502
- create_cmdclass , install_npm , ensure_targets ,
503
- combine_commands , skip_if_exists
499
+ create_cmdclass ,
500
+ install_npm ,
501
+ ensure_targets ,
502
+ combine_commands ,
503
+ skip_if_exists ,
504
504
)
505
505
import setuptools
506
506
507
- HERE = os . path . abspath ( os . path . dirname ( __file__ ) )
507
+ HERE = Path ( __file__ ). parent . resolve ( )
508
508
509
509
# 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' ]
510
+ name = " jlab_ext_example"
515
511
516
- lab_path = os . path . join ( HERE , name , " labextension" )
512
+ lab_path = HERE / name / " labextension"
517
513
518
514
# Representative files that should exist after a successful build
519
515
jstargets = [
520
- os . path . join (lab_path , " package.json" ),
516
+ str (lab_path / " package.json" ),
521
517
]
522
518
523
- package_data_spec = {
524
- name : [
525
- "*"
526
- ]
527
- }
519
+ package_data_spec = {name : ["*" ]}
528
520
529
521
labext_name = " @jupyterlab-examples/server-extension"
530
522
531
523
data_files_spec = [
532
- (" share/jupyter/labextensions/%s" % labext_name , lab_path , " **" ),
533
- (" share/jupyter/labextensions/%s" % labext_name , HERE , " install.json" ),
524
+ (" share/jupyter/labextensions/%s" % labext_name , str ( lab_path ) , " **" ),
525
+ (" share/jupyter/labextensions/%s" % labext_name , str ( HERE ) , " install.json" ),
534
526
(" etc/jupyter/jupyter_server_config.d" , " jupyter-config" , " jlab_ext_example.json" ),
535
527
]
536
528
537
- cmdclass = create_cmdclass (" jsdeps" ,
538
- package_data_spec =package_data_spec ,
539
- data_files_spec =data_files_spec
529
+ cmdclass = create_cmdclass (
530
+ " jsdeps" , package_data_spec =package_data_spec , data_files_spec =data_files_spec
540
531
)
541
532
542
533
js_command = combine_commands (
543
534
install_npm (HERE , build_cmd =" build:prod" , npm =[" jlpm" ]),
544
535
ensure_targets (jstargets ),
545
536
)
546
537
547
- is_repo = os . path . exists ( os . path . join ( HERE , " .git" ))
538
+ is_repo = ( HERE / " .git" ). exists ( )
548
539
if is_repo :
549
540
cmdclass [" jsdeps" ] = js_command
550
541
else :
551
542
cmdclass [" jsdeps" ] = skip_if_exists (jstargets , js_command )
552
543
553
- with open (" README.md" , " r" ) as fh :
554
- long_description = fh .read ()
544
+ long_description = (HERE / " README.md" ).read_text ()
545
+
546
+ # Get the package info from package .json
547
+ pkg_json = json .loads ((HERE / " package.json" ).read_bytes ())
555
548
556
549
setup_args = dict (
557
550
name = name ,
558
- version =version ,
559
- url =" https://github.com/jupyterlab/extension-examples.git" ,
560
- author =" Project Jupyter Contributors" ,
561
- description =" A minimal JupyterLab extension with backend and frontend parts." ,
562
- long_description = long_description ,
551
+ version = pkg_json [" version" ],
552
+ url = pkg_json [" homepage" ],
553
+ author = pkg_json [" author" ],
554
+ description = pkg_json [" description" ],
555
+ license = pkg_json [" license" ],
556
+ long_description = long_description ,
563
557
long_description_content_type = " text/markdown" ,
564
- cmdclass = cmdclass ,
558
+ cmdclass = cmdclass ,
565
559
packages = setuptools .find_packages (),
566
560
install_requires = [
567
- " jupyterlab> =3.0.0rc15,==3.* " ,
561
+ " jupyterlab~ =3.0" ,
568
562
],
569
563
zip_safe = False ,
570
564
include_package_data = True ,
571
565
python_requires = " >=3.6" ,
572
- license =" BSD-3-Clause" ,
573
566
platforms = " Linux, Mac OS X, Windows" ,
574
567
keywords = [" Jupyter" , " JupyterLab" , " JupyterLab3" ],
575
568
classifiers = [
@@ -579,6 +572,7 @@ setup_args = dict(
579
572
" Programming Language :: Python :: 3.6" ,
580
573
" Programming Language :: Python :: 3.7" ,
581
574
" Programming Language :: Python :: 3.8" ,
575
+ " Programming Language :: Python :: 3.9" ,
582
576
" Framework :: Jupyter" ,
583
577
],
584
578
)
@@ -595,11 +589,10 @@ the frontend NPM package needs to be built and inserted in the Python package. T
595
589
done using a special ` cmdclass ` :
596
590
597
591
` ` ` py
598
- # setup .py #L43 - L51
592
+ # setup .py #L38 - L45
599
593
600
- cmdclass = create_cmdclass (" jsdeps" ,
601
- package_data_spec =package_data_spec ,
602
- data_files_spec =data_files_spec
594
+ cmdclass = create_cmdclass (
595
+ " jsdeps" , package_data_spec = package_data_spec , data_files_spec = data_files_spec
603
596
)
604
597
605
598
js_command = combine_commands (
@@ -611,29 +604,29 @@ js_command = combine_commands(
611
604
Basically it will build the frontend NPM package:
612
605
613
606
` ` ` py
614
- # setup .py #L49 - L49
607
+ # setup .py #L43 - L43
615
608
616
609
install_npm (HERE , build_cmd = " build:prod" , npm = [" jlpm" ]),
617
610
` ` `
618
611
619
612
It will ensure one of the generated files is ` jlab_ext_example / labextension / package .json ` :
620
613
621
614
` ` ` py
622
- # setup .py #L24 - L27
615
+ # setup .py #L23 - L26
623
616
624
617
# Representative files that should exist after a successful build
625
618
jstargets = [
626
- os . path . join (lab_path , " package.json" ),
619
+ str (lab_path / " package.json" ),
627
620
]
628
621
` ` `
629
622
630
623
It will copy the NPM package in the Python package and force it to be copied in a place
631
624
JupyterLab is looking for frontend extensions when the Python package is installed:
632
625
633
626
` ` ` py
634
- # setup .py #L38 - L38
627
+ # setup .py #L33 - L33
635
628
636
- (" share/jupyter/labextensions/%s" % labext_name , lab_path , " **" ),
629
+ (" share/jupyter/labextensions/%s" % labext_name , str ( lab_path ) , " **" ),
637
630
` ` `
638
631
639
632
The last piece of configuration needed is the enabling of the server extension. This is
@@ -655,7 +648,7 @@ done by copying the following JSON file:
655
648
in the appropriate jupyter folder ( ` etc / jupyter / jupyter_server_config .d ` ):
656
649
657
650
` ` ` py
658
- # setup .py #L40 - L40
651
+ # setup .py #L35 - L35
659
652
660
653
(" etc/jupyter/jupyter_server_config.d" , " jupyter-config" , " jlab_ext_example.json" ),
661
654
` ` `
0 commit comments