Skip to content

Commit 9bb07c0

Browse files
committed
Align more server example with template
1 parent c4902be commit 9bb07c0

File tree

4 files changed

+107
-103
lines changed

4 files changed

+107
-103
lines changed

advanced/server-extension/README.md

Lines changed: 50 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -331,51 +331,48 @@ your extension needs to be defined as a proper Python package with some hook fun
331331
# jlab_ext_example/__init__.py
332332

333333
import json
334-
import os.path as osp
334+
from pathlib import Path
335335

336336
from .handlers import setup_handlers
337337
from ._version import __version__
338338

339-
HERE = osp.abspath(osp.dirname(__file__))
339+
HERE = Path(__file__).parent.resolve()
340340

341-
with open(osp.join(HERE, 'labextension', 'package.json')) as fid:
341+
with (HERE / "labextension" / "package.json").open() as fid:
342342
data = json.load(fid)
343343

344+
344345
def _jupyter_labextension_paths():
345-
return [{
346-
'src': 'labextension',
347-
'dest': data['name']
348-
}]
346+
return [{"src": "labextension", "dest": data["name"]}]
349347

350348

351349
def _jupyter_server_extension_points():
352350
return [{"module": "jlab_ext_example"}]
353351

354352

355-
def _load_jupyter_server_extension(lab_app):
353+
def _load_jupyter_server_extension(server_app):
356354
"""Registers the API handler to receive HTTP requests from the frontend extension.
357355
Parameters
358356
----------
359-
lab_app: jupyterlab.labapp.LabApp
357+
server_app: jupyterlab.labapp.LabApp
360358
JupyterLab application instance
361359
"""
362360
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}"
366364
)
367365

368-
369366
```
370367
371368
The `_jupyter_server_extension_points` provides the Python package name
372369
to the server. But the most important one is `_load_jupyter_server_extension`
373370
that register new handlers.
374371
375372
```py
376-
# jlab_ext_example/__init__.py#L31-L31
373+
# jlab_ext_example/__init__.py#L29-L29
377374

378-
setup_handlers(lab_app.web_app, url_path)
375+
setup_handlers(server_app.web_app, url_path)
379376
```
380377
381378
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:
496493
jlab_ext_example setup
497494
"""
498495
import json
499-
import os
496+
from pathlib import Path
500497

501498
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,
504504
)
505505
import setuptools
506506

507-
HERE = os.path.abspath(os.path.dirname(__file__))
507+
HERE = Path(__file__).parent.resolve()
508508

509509
# 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"
515511

516-
lab_path = os.path.join(HERE, name, "labextension")
512+
lab_path = HERE / name / "labextension"
517513

518514
# Representative files that should exist after a successful build
519515
jstargets = [
520-
os.path.join(lab_path, "package.json"),
516+
str(lab_path / "package.json"),
521517
]
522518

523-
package_data_spec = {
524-
name: [
525-
"*"
526-
]
527-
}
519+
package_data_spec = {name: ["*"]}
528520

529521
labext_name = "@jupyterlab-examples/server-extension"
530522

531523
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"),
534526
("etc/jupyter/jupyter_server_config.d", "jupyter-config", "jlab_ext_example.json"),
535527
]
536528

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
540531
)
541532

542533
js_command = combine_commands(
543534
install_npm(HERE, build_cmd="build:prod", npm=["jlpm"]),
544535
ensure_targets(jstargets),
545536
)
546537

547-
is_repo = os.path.exists(os.path.join(HERE, ".git"))
538+
is_repo = (HERE / ".git").exists()
548539
if is_repo:
549540
cmdclass["jsdeps"] = js_command
550541
else:
551542
cmdclass["jsdeps"] = skip_if_exists(jstargets, js_command)
552543

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())
555548

556549
setup_args = dict(
557550
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,
563557
long_description_content_type="text/markdown",
564-
cmdclass= cmdclass,
558+
cmdclass=cmdclass,
565559
packages=setuptools.find_packages(),
566560
install_requires=[
567-
"jupyterlab>=3.0.0rc15,==3.*",
561+
"jupyterlab~=3.0",
568562
],
569563
zip_safe=False,
570564
include_package_data=True,
571565
python_requires=">=3.6",
572-
license="BSD-3-Clause",
573566
platforms="Linux, Mac OS X, Windows",
574567
keywords=["Jupyter", "JupyterLab", "JupyterLab3"],
575568
classifiers=[
@@ -579,6 +572,7 @@ setup_args = dict(
579572
"Programming Language :: Python :: 3.6",
580573
"Programming Language :: Python :: 3.7",
581574
"Programming Language :: Python :: 3.8",
575+
"Programming Language :: Python :: 3.9",
582576
"Framework :: Jupyter",
583577
],
584578
)
@@ -595,11 +589,10 @@ the frontend NPM package needs to be built and inserted in the Python package. T
595589
done using a special `cmdclass`:
596590
597591
```py
598-
# setup.py#L43-L51
592+
# setup.py#L38-L45
599593

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
603596
)
604597

605598
js_command = combine_commands(
@@ -611,29 +604,29 @@ js_command = combine_commands(
611604
Basically it will build the frontend NPM package:
612605
613606
```py
614-
# setup.py#L49-L49
607+
# setup.py#L43-L43
615608

616609
install_npm(HERE, build_cmd="build:prod", npm=["jlpm"]),
617610
```
618611
619612
It will ensure one of the generated files is `jlab_ext_example/labextension/package.json`:
620613
621614
```py
622-
# setup.py#L24-L27
615+
# setup.py#L23-L26
623616

624617
# Representative files that should exist after a successful build
625618
jstargets = [
626-
os.path.join(lab_path, "package.json"),
619+
str(lab_path / "package.json"),
627620
]
628621
```
629622
630623
It will copy the NPM package in the Python package and force it to be copied in a place
631624
JupyterLab is looking for frontend extensions when the Python package is installed:
632625
633626
```py
634-
# setup.py#L38-L38
627+
# setup.py#L33-L33
635628

636-
("share/jupyter/labextensions/%s" % labext_name, lab_path, "**"),
629+
("share/jupyter/labextensions/%s" % labext_name, str(lab_path), "**"),
637630
```
638631
639632
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:
655648
in the appropriate jupyter folder (`etc/jupyter/jupyter_server_config.d`):
656649
657650
```py
658-
# setup.py#L40-L40
651+
# setup.py#L35-L35
659652

660653
("etc/jupyter/jupyter_server_config.d", "jupyter-config", "jlab_ext_example.json"),
661654
```
Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,32 @@
11
import json
2-
import os.path as osp
2+
from pathlib import Path
33

44
from .handlers import setup_handlers
55
from ._version import __version__
66

7-
HERE = osp.abspath(osp.dirname(__file__))
7+
HERE = Path(__file__).parent.resolve()
88

9-
with open(osp.join(HERE, 'labextension', 'package.json')) as fid:
9+
with (HERE / "labextension" / "package.json").open() as fid:
1010
data = json.load(fid)
1111

12+
1213
def _jupyter_labextension_paths():
13-
return [{
14-
'src': 'labextension',
15-
'dest': data['name']
16-
}]
14+
return [{"src": "labextension", "dest": data["name"]}]
1715

1816

1917
def _jupyter_server_extension_points():
2018
return [{"module": "jlab_ext_example"}]
2119

2220

23-
def _load_jupyter_server_extension(lab_app):
21+
def _load_jupyter_server_extension(server_app):
2422
"""Registers the API handler to receive HTTP requests from the frontend extension.
2523
Parameters
2624
----------
27-
lab_app: jupyterlab.labapp.LabApp
25+
server_app: jupyterlab.labapp.LabApp
2826
JupyterLab application instance
2927
"""
3028
url_path = "jlab-ext-example"
31-
setup_handlers(lab_app.web_app, url_path)
32-
lab_app.log.info(
33-
"Registered jlab_ext_example extension at URL path /{}".format(url_path)
29+
setup_handlers(server_app.web_app, url_path)
30+
server_app.log.info(
31+
f"Registered jlab_ext_example extension at URL path /{url_path}"
3432
)
Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,18 @@
1-
version_info = (0, 1, 0)
2-
__version__ = ".".join(map(str, version_info))
1+
import json
2+
from pathlib import Path
3+
4+
__all__ = ["__version__"]
5+
6+
def _fetchVersion():
7+
HERE = Path(__file__).parent.resolve()
8+
9+
for settings in HERE.rglob("package.json"):
10+
try:
11+
with settings.open() as f:
12+
return json.load(f)["version"]
13+
except FileNotFoundError:
14+
pass
15+
16+
raise FileNotFoundError(f"Could not find package.json under dir {HERE!s}")
17+
18+
__version__ = _fetchVersion()

0 commit comments

Comments
 (0)