main.py 2.6 KB

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