|
@@ -1,15 +1,13 @@
|
|
|
import justpy as jp
|
|
|
import os.path
|
|
|
+from typing import List
|
|
|
from starlette.routing import Route
|
|
|
from starlette.responses import FileResponse
|
|
|
|
|
|
class CustomView(jp.JustpyBaseComponent):
|
|
|
- vue_dependencies = []
|
|
|
|
|
|
- def __init__(self, vue_type, filepath, dependencies=[], **options):
|
|
|
+ def __init__(self, vue_type, **options):
|
|
|
self.vue_type = vue_type
|
|
|
- self.vue_filepath = os.path.realpath(filepath).replace('.py', '.js')
|
|
|
- self.vue_dependencies = dependencies
|
|
|
|
|
|
self.pages = {}
|
|
|
self.classes = ''
|
|
@@ -18,24 +16,6 @@ class CustomView(jp.JustpyBaseComponent):
|
|
|
|
|
|
super().__init__(temp=False)
|
|
|
|
|
|
- def add_page(self, wp: jp.WebPage):
|
|
|
- for dependency in self.vue_dependencies:
|
|
|
- is_remote = dependency.startswith('http://') or dependency.startswith('https://')
|
|
|
- src = dependency if is_remote else f'lib/{dependency}'
|
|
|
- if src not in jp.component_file_list:
|
|
|
- jp.component_file_list += [src]
|
|
|
- if not is_remote:
|
|
|
- filepath = f'{os.path.dirname(self.vue_filepath)}/{src}'
|
|
|
- route = Route(f'/{src}', lambda _, filepath=filepath: FileResponse(filepath))
|
|
|
- jp.app.routes.insert(0, route)
|
|
|
-
|
|
|
- if self.vue_filepath not in jp.component_file_list:
|
|
|
- filename = os.path.basename(self.vue_filepath)
|
|
|
- jp.app.routes.insert(0, Route(f'/{filename}', lambda _: FileResponse(self.vue_filepath)))
|
|
|
- jp.component_file_list += [filename]
|
|
|
-
|
|
|
- super().add_page(wp)
|
|
|
-
|
|
|
def react(self, _):
|
|
|
pass
|
|
|
|
|
@@ -48,3 +28,22 @@ class CustomView(jp.JustpyBaseComponent):
|
|
|
'style': self.style,
|
|
|
'options': self.options,
|
|
|
}
|
|
|
+
|
|
|
+ @staticmethod
|
|
|
+ def use(py_filepath: str, dependencies: List[str] = []):
|
|
|
+ vue_filepath = os.path.realpath(py_filepath).replace('.py', '.js')
|
|
|
+
|
|
|
+ for dependency in dependencies:
|
|
|
+ is_remote = dependency.startswith('http://') or dependency.startswith('https://')
|
|
|
+ src = dependency if is_remote else f'lib/{dependency}'
|
|
|
+ if src not in jp.component_file_list:
|
|
|
+ jp.component_file_list += [src]
|
|
|
+ if not is_remote:
|
|
|
+ filepath = f'{os.path.dirname(vue_filepath)}/{src}'
|
|
|
+ route = Route(f'/{src}', lambda _, filepath=filepath: FileResponse(filepath))
|
|
|
+ jp.app.routes.insert(0, route)
|
|
|
+
|
|
|
+ if vue_filepath not in jp.component_file_list:
|
|
|
+ filename = os.path.basename(vue_filepath)
|
|
|
+ jp.app.routes.insert(0, Route(f'/{filename}', lambda _: FileResponse(vue_filepath)))
|
|
|
+ jp.component_file_list += [filename]
|