浏览代码

serve local js libraries from lib folder next to python file instead of global static directory

Falko Schindler 3 年之前
父节点
当前提交
2d359d84ec
共有 3 个文件被更改,包括 9 次插入4 次删除
  1. 8 3
      nicegui/elements/custom_view.py
  2. 1 1
      nicegui/elements/joystick.py
  3. 0 0
      nicegui/elements/lib/nipplejs.min.js

+ 8 - 3
nicegui/elements/custom_view.py

@@ -7,10 +7,10 @@ class CustomView(jp.JustpyBaseComponent):
 
     vue_dependencies = []
 
-    def __init__(self, vue_type, filename, dependencies=[], **options):
+    def __init__(self, vue_type, filepath, dependencies=[], **options):
 
         self.vue_type = vue_type
-        self.vue_filepath = os.path.realpath(filename).replace('.py', '.js')
+        self.vue_filepath = os.path.realpath(filepath).replace('.py', '.js')
         self.vue_dependencies = dependencies
 
         self.pages = {}
@@ -27,7 +27,12 @@ class CustomView(jp.JustpyBaseComponent):
         if marker not in wp.head_html:
             wp.head_html += marker
             for dependency in self.vue_dependencies:
-                wp.head_html += f'<script src="{dependency}"></script>\n'
+                if dependency.startswith('http://') or dependency.startswith('https://'):
+                    wp.head_html += f'<script src="{dependency}"></script>\n'
+                else:
+                    wp.head_html += f'<script src="lib/{dependency}"></script>\n'
+                    filepath = f'{os.path.dirname(self.vue_filepath)}/lib/{dependency}'
+                    jp.app.routes.insert(0, Route(f'/lib/{dependency}', lambda _: FileResponse(filepath)))
 
         if self.vue_filepath not in jp.component_file_list:
             filename = os.path.basename(self.vue_filepath)

+ 1 - 1
nicegui/elements/joystick.py

@@ -6,7 +6,7 @@ class JoystickView(CustomView):
 
     def __init__(self, on_start, on_move, on_end, **options):
 
-        super().__init__('joystick', __file__, ['static/lib/nipplejs.min.js'], **options)
+        super().__init__('joystick', __file__, ['nipplejs.min.js'], **options)
 
         self.on_start = on_start
         self.on_move = on_move

+ 0 - 0
nicegui/static/lib/nipplejs.min.js → nicegui/elements/lib/nipplejs.min.js