You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@superset.apache.org by GitBox <gi...@apache.org> on 2021/10/20 12:36:39 UTC

[GitHub] [superset] ypankovych commented on pull request #12315: feat: provide support for an overridable template to be included in every view

ypankovych commented on pull request #12315:
URL: https://github.com/apache/superset/pull/12315#issuecomment-947622466


   that's what we did:
   
   ```python
   from superset import manifest_processor
   from superset.extensions import UIManifestProcessor
   
   def extend_ui_manifest():
       """
       Makes our custom JS files to be executed on every HTML page.
   
       In order to add JS file, go to `/static/assets/dist/manifest.json`
        and add path to JS file within the `/static` folder, e.g:
       >>> {
       >>>   "js": [
       >>>     "welcome/walkme.js",
       >>>     "hello.js",
       >>>   ]
       >>> }
       """
       with open(os.path.join(SIMPLESET_STATIC_DIR, "assets", "dist", "manifest.json")) as fp:
           manifest = json.load(fp)
       parse_manifest_json = UIManifestProcessor.parse_manifest_json
   
       def custom_parse_manifest_json(self):
           parse_manifest_json(self)
           entrypoints = self.manifest.setdefault("theme", {"js": []})["js"]
           for js_path in manifest["js"]:
               # /static handled by our custom static view
               entrypoints.append(os.path.join("/static", js_path))
   
       UIManifestProcessor.parse_manifest_json = custom_parse_manifest_json
       manifest_processor.parse_manifest_json()
   ```
   
   But in order for this to work, you also need to patch superset static, e.g:
   
   ```python
   def flask_app_mutator(app):
   
       def superset_static(self, filename):  # vendored from flask and modified to support our static files
           if not self.has_static_folder and not self.config.get("SUPERSET_STATIC"):
               raise RuntimeError("No static folder for this object")
           # Ensure get_send_file_max_age is called in all cases.
           # Here, we ensure get_send_file_max_age is called for Blueprints.
           cache_timeout = self.get_send_file_max_age(filename)
           for static_folder in filter(None, (self.config.get("SUPERSET_STATIC"), self.static_folder)):
               with contextlib.suppress(NotFound):
                   return send_from_directory(
                       static_folder, filename, cache_timeout=cache_timeout
                   )
           raise NotFound
   
       extend_ui_manifest()
       app.view_functions["static"] = MethodType(superset_static, app)  # hack
   ```
   
   And then you simply edit that manifest from our static folder:
   
   ```json
   {
     "js": [
       "walkme.js",
       "a/b/c/hello.js"
     ]
   }
   ```
   
   Above JS files will be loaded along with `base.html`


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org