Răsfoiți Sursa

experimenting with template body manipulation instead of own template dir

Rodja Trappe 3 ani în urmă
părinte
comite
b83269434f
2 a modificat fișierele cu 14 adăugiri și 1 ștergeri
  1. 1 1
      nicegui/config.py
  2. 13 0
      nicegui/nicegui.py

+ 1 - 1
nicegui/config.py

@@ -59,6 +59,6 @@ else:
 os.environ['HOST'] = config.host
 os.environ['HOST'] = config.host
 os.environ['PORT'] = str(config.port)
 os.environ['PORT'] = str(config.port)
 os.environ["STATIC_DIRECTORY"] = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'static')
 os.environ["STATIC_DIRECTORY"] = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'static')
-os.environ["TEMPLATES_DIRECTORY"] = os.path.join(os.environ["STATIC_DIRECTORY"], 'templates')
+#os.environ["TEMPLATES_DIRECTORY"] = os.path.join(os.environ["STATIC_DIRECTORY"], 'templates')
 
 
 globals.config = config
 globals.config = config

+ 13 - 0
nicegui/nicegui.py

@@ -2,16 +2,19 @@
 from typing import Awaitable, Callable
 from typing import Awaitable, Callable
 import asyncio
 import asyncio
 
 
+
 from .ui import Ui  # NOTE: before justpy
 from .ui import Ui  # NOTE: before justpy
 import justpy as jp
 import justpy as jp
 from .timer import Timer
 from .timer import Timer
 from . import globals
 from . import globals
 from . import binding
 from . import binding
 from .task_logger import create_task
 from .task_logger import create_task
+import re
 
 
 
 
 @jp.app.on_event('startup')
 @jp.app.on_event('startup')
 def startup():
 def startup():
+    jp.templates.TemplateResponse = proxyPathAwareTemplateResponse
     globals.tasks.extend(create_task(t.coro, name=t.name) for t in Timer.prepared_coroutines)
     globals.tasks.extend(create_task(t.coro, name=t.name) for t in Timer.prepared_coroutines)
     Timer.prepared_coroutines.clear()
     Timer.prepared_coroutines.clear()
     globals.tasks.extend(create_task(t, name='startup task') for t in Ui.startup_tasks if isinstance(t, Awaitable))
     globals.tasks.extend(create_task(t, name='startup task') for t in Ui.startup_tasks if isinstance(t, Awaitable))
@@ -24,6 +27,16 @@ def shutdown():
     [safe_invoke(t) for t in Ui.shutdown_tasks if isinstance(t, Callable)]
     [safe_invoke(t) for t in Ui.shutdown_tasks if isinstance(t, Callable)]
     [t.cancel() for t in globals.tasks]
     [t.cancel() for t in globals.tasks]
 
 
+templateResponse = jp.templates.TemplateResponse
+def proxyPathAwareTemplateResponse(*args):
+    response = templateResponse(*args)
+    body = response.body.decode()
+    body = body.replace('/templates', '/wasserbauer/4/templates')
+    body = body.replace('/static', '/wasserbauer/4/static')
+    body = re.sub('src=.*/wasserbauer/4/static/(.*\.js)', '\g<1>', body)
+    response.body = body.encode(response.charset)
+    return response
+
 def safe_invoke(func: Callable):
 def safe_invoke(func: Callable):
     try:
     try:
         func()
         func()