Browse Source

Merge branch 'navigating_with_proxy_prefix'

Falko Schindler 2 years ago
parent
commit
6b88ac7e77

+ 4 - 4
examples/nginx_subpath/app/main.py

@@ -3,14 +3,14 @@ from nicegui import ui
 
 @ui.page('/subpage')
 def subpage():
-    ui.label('This is a subpage').classes('text-h5 mx-auto mt-12')
-    # TODO: this is not working properly yet
-    # ui.button('back', on_click=lambda: ui.open('/')).classes('mx-auto')
+    ui.label('This is a subpage').classes('text-h5 mx-auto mt-24')
+    ui.link('Navigate to the index page.', '/').classes('text-lg mx-auto')
+    ui.button('back', on_click=lambda: ui.open('/')).classes('mx-auto')
 
 
 @ui.page('/')
 def index():
-    with ui.card().classes('mx-auto px-24 pt-12 pb-24 items-center text-center'):
+    with ui.card().classes('mx-auto p-24 items-center text-center'):
         ui.label('This demonstrates hosting of a NiceGUI app on a subpath.').classes('text-h5')
         ui.label('As you can see the entire app is available below "/nicegui".').classes('text-lg')
         ui.label('But the code here does not need to know that.').classes('text-lg')

+ 5 - 1
examples/nginx_subpath/docker-compose.yml

@@ -1,7 +1,11 @@
 version: "3.9"
 services:
   app:
-    image: zauberzeug/nicegui:latest
+    # TODO replace build with image once the image is published
+    #image: zauberzeug/nicegui:1.2.?
+    build:
+      context: ./../../
+      dockerfile: fly.dockerfile
     volumes:
       - ./app:/app # mount local app directory
   proxy:

+ 17 - 0
nicegui/elements/link.js

@@ -0,0 +1,17 @@
+export default {
+  template: `<a :href="computed_href" :target="target"><slot></slot></a>`,
+  mounted() {
+    setTimeout(() => {
+      this.computed_href = (this.href.startsWith("/") ? window.path_prefix : "") + this.href;
+    }, 0); // NOTE: wait for window.path_prefix to be set in app.mounted()
+  },
+  props: {
+    href: String,
+    target: String,
+  },
+  data: function () {
+    return {
+      computed_href: this.href,
+    };
+  },
+};

+ 4 - 1
nicegui/elements/link.py

@@ -1,9 +1,12 @@
 from typing import Callable, Union
 
 from .. import globals
+from ..dependencies import register_component
 from ..element import Element
 from .mixins.text_element import TextElement
 
+register_component('link', __file__, 'link.js')
+
 
 class Link(TextElement):
 
@@ -19,7 +22,7 @@ class Link(TextElement):
         :param target: page function or string that is a an absolute URL or relative path from base URL
         :param new_tab: open link in new tab (default: False)
         """
-        super().__init__(tag='a', text=text)
+        super().__init__(tag='link', text=text)
         self._props['href'] = target if isinstance(target, str) else globals.page_routes[target]
         self._props['target'] = '_blank' if new_tab else '_self'
         self._classes = ['nicegui-link']

+ 1 - 1
nicegui/templates/index.html

@@ -171,7 +171,7 @@
           window.socket.on("update", (msg) => Object.entries(msg).forEach(([id, el]) => this.elements[el.id] = el));
           window.socket.on("run_method", (msg) => getElement(msg.id)?.[msg.name](...msg.args));
           window.socket.on("run_javascript", (msg) => runJavascript(msg['code'], msg['request_id']));
-          window.socket.on("open", (msg) => (location.href = msg));
+          window.socket.on("open", (msg) => (location.href = msg.startsWith('/') ? "{{ prefix | safe }}" + msg : msg));
           window.socket.on("download", (msg) => download(msg.url, msg.filename));
           window.socket.on("notify", (msg) => Quasar.Notify.create(msg));
         },

+ 1 - 1
tests/screen.py

@@ -124,7 +124,7 @@ class Screen:
 
     def find(self, text: str) -> WebElement:
         try:
-            query = f'//*[not(self::script) and not(self::style) and contains(text(), "{text}")]'
+            query = f'//*[not(self::script) and not(self::style) and text()[contains(., "{text}")]]'
             element = self.selenium.find_element(By.XPATH, query)
             if not element.is_displayed():
                 self.wait(0.1)  # HACK: repeat check after a short delay to avoid timing issue on fast machines