forked from jupyterlab/jupyterlab
/
labextensions.py
143 lines (103 loc) · 4.35 KB
/
labextensions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# coding: utf-8
"""Jupyter LabExtension Entry Points."""
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.
from __future__ import print_function
import os
import sys
from jupyter_core.application import JupyterApp, base_flags, base_aliases
from traitlets import Bool, Unicode
from ._version import __version__
from .commands import (
install_extension, uninstall_extension, list_extensions,
link_package, unlink_package, build, _get_linked_packages
)
flags = dict(base_flags)
flags['no-build'] = (
{'BaseExtensionApp': {'should_build': False}},
"Defer building the app after the action."
)
aliases = dict(base_aliases)
aliases['app-dir'] = 'BaseExtensionApp.app_dir'
class BaseExtensionApp(JupyterApp):
version = __version__
flags = flags
aliases = aliases
app_dir = Unicode('', config=True,
help="The app directory to target")
should_build = Bool(True, config=True,
help="Whether to build the app after the action")
class InstallLabExtensionApp(BaseExtensionApp):
description = "Install labextension(s)"
def start(self):
self.extra_args = self.extra_args or [os.getcwd()]
[install_extension(arg, self.app_dir) for arg in self.extra_args]
if self.should_build:
build(self.app_dir)
class LinkLabExtensionApp(BaseExtensionApp):
description = """
Link labextension(s) or packages.
Links a package to the JupyterLab build process. If the package is
an extension, it will also be installed as an extension. A linked
package is manually re-installed from its source location when
`jupyter lab build` is run.
"""
def start(self):
self.extra_args = self.extra_args or [os.getcwd()]
[link_package(arg, self.app_dir) for arg in self.extra_args]
if self.should_build:
build(self.app_dir)
class UnlinkLabExtensionApp(BaseExtensionApp):
description = "Unlink labextension(s) or packages by name or path"
def start(self):
self.extra_args = self.extra_args or [os.getcwd()]
ans = any([unlink_package(arg, self.app_dir)
for arg in self.extra_args])
if ans and self.should_build:
build(self.app_dir)
class UninstallLabExtensionApp(BaseExtensionApp):
description = "Uninstall labextension(s) by name"
def start(self):
self.extra_args = self.extra_args or [os.getcwd()]
ans = any([uninstall_extension(arg, self.app_dir)
for arg in self.extra_args])
if ans and self.should_build:
build(self.app_dir)
class ListLabExtensionsApp(BaseExtensionApp):
description = "List the installed labextensions"
should_build = False
def start(self):
[print(ext) for ext in list_extensions(self.app_dir)]
class ListLinkedLabExtensionsApp(BaseExtensionApp):
description = "List the linked packages"
should_build = False
def start(self):
for path in _get_linked_packages(self.app_dir).values():
print(path)
_examples = """
jupyter labextension list # list all configured labextensions
jupyter labextension install <extension name> # install a labextension
jupyter labextension uninstall <extension name> # uninstall a labextension
"""
class LabExtensionApp(JupyterApp):
"""Base jupyter labextension command entry point"""
name = "jupyter labextension"
version = __version__
description = "Work with JupyterLab extensions"
examples = _examples
subcommands = dict(
install=(InstallLabExtensionApp, "Install labextension(s)"),
uninstall=(UninstallLabExtensionApp, "Uninstall labextension(s)"),
list=(ListLabExtensionsApp, "List labextensions"),
link=(LinkLabExtensionApp, "Link labextension(s)"),
unlink=(UnlinkLabExtensionApp, "Unlink labextension(s)"),
listlinked=(ListLinkedLabExtensionsApp, "List linked extensions")
)
def start(self):
"""Perform the App's functions as configured"""
super(LabExtensionApp, self).start()
# The above should have called a subcommand and raised NoStart; if we
# get here, it didn't, so we should self.log.info a message.
subcmds = ", ".join(sorted(self.subcommands))
sys.exit("Please supply at least one subcommand: %s" % subcmds)
main = LabExtensionApp.launch_instance