# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.

import os
import json

from glob import glob
from jupyterlab_server import LabServerApp

HERE = os.path.abspath(os.path.dirname(__file__))

# Turn off the Jupyter configuration system so configuration files on disk do
# not affect this app. This helps this app to truly be standalone.
os.environ["JUPYTER_NO_CONFIG"]="1"

with open(os.path.join(HERE, 'package.json')) as fid:
    version = json.load(fid)['version']

def _jupyter_server_extension_points():
    return [
        {
            'module': __name__,
            'app': ExampleApp
        }
    ]

class ExampleApp(LabServerApp):
    name = 'lab'
    load_other_extensions = False
    app_name = 'JupyterLab Example Federated App'
    app_settings_dir = os.path.join(HERE, 'build', 'application_settings')
    app_version = version
    schemas_dir = os.path.join(HERE, 'core_package', 'build', 'schemas')
    static_dir = os.path.join(HERE, 'core_package', 'build')
    templates_dir = os.path.join(HERE, 'templates')
    themes_dir = os.path.join(HERE, 'core_package', 'build', 'themes')
    user_settings_dir = os.path.join(HERE, 'core_package', 'build', 'user_settings')
    workspaces_dir = os.path.join(HERE, 'core_package', 'build', 'workspaces')
    labextensions_path =  [os.path.join(HERE, "labextensions")]

    def initialize_handlers(self):
        # Handle labextension assets
        web_app = self.serverapp.web_app
        page_config = web_app.settings.get('page_config_data', {})
        web_app.settings['page_config_data'] = page_config

        # By default, make terminals available.
        web_app.settings.setdefault('terminals_available', True)

        # Extract the federated extension data from lab_extensions
        federated_exts = []
        for ext_path in [path for path in glob('./labextensions/**/package.json', recursive=True)]:
            with open(ext_path) as fid:
                data = json.load(fid)
            extbuild = data['jupyterlab']['_build']
            ext = {
                'name': data['name'],
                'load': extbuild['load'],
            }
            if 'extension' in extbuild:
                ext['extension'] = extbuild['extension']
            if 'mimeExtension' in extbuild:
                ext['mimeExtension'] = extbuild['mimeExtension']
            if 'style' in extbuild:
                ext['style'] = extbuild['style']
            federated_exts.append(ext)

        page_config['federated_extensions'] = federated_exts

        super().initialize_handlers()

if __name__ == '__main__':
    ExampleApp.launch_instance()

@@ -667,16 +667,24 @@
  Example
+Prebuilt
 App(LabS
@@ -784,21 +784,36 @@
 ple 
-Federated App
+App with Prebuilt Extensions
 '%0A  
@@ -1327,24 +1327,148 @@
 workspaces')
+%0A%0A    # Set the location for prebuilt extensions, overriding the default%0A    # of looking in each of the Jupyter data paths.
 %0A    labexte
@@ -1480,17 +1480,16 @@
 s_path =
- 
  %5Bos.pat
@@ -1523,16 +1523,18 @@
 %22)%5D%0A%0A   
+ #
  def ini
@@ -1561,20 +1561,22 @@
 f):%0A    
+#
     
+ 
 # Handle
@@ -1595,24 +1595,26 @@
 n assets%0A   
+ #
      web_app
@@ -1638,24 +1638,26 @@
 .web_app%0A   
+ #
      page_co
@@ -1707,24 +1707,26 @@
 ta', %7B%7D)%0A   
+ #
      web_app
@@ -1769,24 +1769,26 @@
 _config%0A%0A   
+ #
      # By de
@@ -1819,24 +1819,26 @@
 ailable.%0A   
+ #
      web_app
@@ -1887,24 +1887,26 @@
 , True)%0A%0A   
+ #
      # Extra
@@ -1956,24 +1956,26 @@
 tensions%0A   
+ #
      federat
@@ -1991,20 +1991,22 @@
  %5B%5D%0A    
+#
     
+ 
 for ext_
@@ -2088,24 +2088,26 @@
 =True)%5D:%0A   
+ #
          wit
@@ -2131,24 +2131,25 @@
 as fid:%0A    
+#
             
@@ -2148,16 +2148,17 @@
         
+ 
 data = j
@@ -2175,24 +2175,25 @@
 id)%0A    
+#
         
 extbuild
@@ -2184,16 +2184,17 @@
         
+ 
 extbuild
@@ -2224,24 +2224,26 @@
 _build'%5D%0A   
+ #
          ext
@@ -2246,24 +2246,26 @@
  ext = %7B%0A   
+ #
             
@@ -2286,24 +2286,26 @@
 'name'%5D,%0A   
+ #
             
@@ -2330,24 +2330,26 @@
 'load'%5D,%0A   
+ #
          %7D%0A 
@@ -2346,24 +2346,26 @@
        %7D%0A   
+ #
          if 
@@ -2384,32 +2384,34 @@
 in extbuild:%0A   
+ #
              ext
@@ -2443,32 +2443,34 @@
 'extension'%5D%0A   
+ #
          if 'mim
@@ -2490,32 +2490,33 @@
 n extbuild:%0A    
+#
             ext%5B
@@ -2507,24 +2507,25 @@
             
+ 
 ext%5B'mimeExt
@@ -2560,24 +2560,26 @@
 ension'%5D%0A   
+ #
          if 
@@ -2598,24 +2598,26 @@
 xtbuild:%0A   
+ #
             
@@ -2654,24 +2654,25 @@
 e'%5D%0A    
+#
         
 federate
@@ -2659,24 +2659,25 @@
    #        
+ 
 federated_ex
@@ -2691,24 +2691,26 @@
 nd(ext)%0A%0A   
+ #
      page_co
@@ -2755,24 +2755,26 @@
 ed_exts%0A%0A   
+ #
      super()

Simplify prebuilt extension app example