main.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # Copyright (c) Jupyter Development Team.
  2. # Distributed under the terms of the Modified BSD License.
  3. from jupyterlab_server import LabServerApp, LabConfig
  4. from jupyterlab_server.server import FileFindHandler
  5. import json
  6. import os
  7. from traitlets import Unicode
  8. from glob import glob
  9. HERE = os.path.abspath(os.path.dirname(__file__))
  10. # Turn off the Jupyter configuration system so configuration files on disk do
  11. # not affect this app. This helps this app to truly be standalone.
  12. os.environ["JUPYTER_NO_CONFIG"]="1"
  13. with open(os.path.join(HERE, 'package.json')) as fid:
  14. version = json.load(fid)['version']
  15. def _jupyter_server_extension_points():
  16. return [
  17. {
  18. 'module': __name__,
  19. 'app': ExampleApp
  20. }
  21. ]
  22. class ExampleApp(LabServerApp):
  23. name = 'lab'
  24. load_other_extensions = False
  25. app_name = 'JupyterLab Example Federated App'
  26. app_settings_dir = os.path.join(HERE, 'build', 'application_settings')
  27. app_version = version
  28. schemas_dir = os.path.join(HERE, 'core_package', 'build', 'schemas')
  29. static_dir = os.path.join(HERE, 'core_package', 'build')
  30. templates_dir = os.path.join(HERE, 'templates')
  31. themes_dir = os.path.join(HERE, 'core_package', 'build', 'themes')
  32. user_settings_dir = os.path.join(HERE, 'core_package', 'build', 'user_settings')
  33. workspaces_dir = os.path.join(HERE, 'core_package', 'build', 'workspaces')
  34. labextensions_path = [os.path.join(HERE, "labextensions")]
  35. def initialize_handlers(self):
  36. # Handle labextension assets
  37. web_app = self.serverapp.web_app
  38. base_url = web_app.settings['base_url']
  39. page_config = web_app.settings.get('page_config_data', {})
  40. web_app.settings['page_config_data'] = page_config
  41. # By default, make terminals available.
  42. web_app.settings.setdefault('terminals_available', True)
  43. # Extract the federated extension data from lab_extensions
  44. federated_exts = []
  45. for ext_path in [path for path in glob('./labextensions/**/package.json', recursive=True)]:
  46. with open(ext_path) as fid:
  47. data = json.load(fid)
  48. extbuild = data['jupyterlab']['_build']
  49. ext = {
  50. 'name': data['name'],
  51. 'load': extbuild['load'],
  52. }
  53. if 'extension' in extbuild:
  54. ext['extension'] = extbuild['extension']
  55. if 'mimeExtension' in extbuild:
  56. ext['mimeExtension'] = extbuild['mimeExtension']
  57. if 'style' in extbuild:
  58. ext['style'] = extbuild['style']
  59. federated_exts.append(ext)
  60. page_config['federated_extensions'] = federated_exts
  61. super().initialize_handlers()
  62. if __name__ == '__main__':
  63. ExampleApp.launch_instance()