labextensions.py 5.7 KB

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