Jelajahi Sumber

add WPA support

wangweimin 2 tahun lalu
induk
melakukan
90f64dad07
4 mengubah file dengan 57 tambahan dan 9 penghapusan
  1. TEMPAT SAMPAH
      pywebio/html/image/apple-touch-icon.png
  2. 51 4
      pywebio/platform/page.py
  3. 1 0
      pywebio/platform/tpl/index.html
  4. 5 5
      setup.py

TEMPAT SAMPAH
pywebio/html/image/apple-touch-icon.png


+ 51 - 4
pywebio/platform/page.py

@@ -1,3 +1,4 @@
+import json
 import urllib.parse
 from collections import namedtuple
 from collections.abc import Mapping, Sequence
@@ -21,7 +22,7 @@ MAX_PAYLOAD_SIZE = 0
 DEFAULT_CDN = "https://cdn.jsdelivr.net/gh/wang0618/PyWebIO-assets@v{version}/"
 
 _global_config = {'title': 'PyWebIO Application'}
-config_keys = ['title', 'description', 'js_file', 'js_code', 'css_style', 'css_file', 'theme']
+config_keys = ['title', 'description', 'js_file', 'js_code', 'css_style', 'css_file', 'theme', 'manifest']
 AppMeta = namedtuple('App', config_keys)
 
 _here_dir = path.dirname(path.abspath(__file__))
@@ -45,13 +46,15 @@ def render_page(app, protocol, cdn):
     else:  # user custom cdn
         base_url = cdn.rstrip('/') + '/'
 
+    manifest = manifest_tag(base_url, meta)
+
     theme = environ.get('PYWEBIO_THEME', meta.theme) or 'default'
     check_theme(theme)
 
     return _index_page_tpl.generate(title=meta.title, description=meta.description, protocol=protocol,
                                     script=True, content='', base_url=base_url, version=version,
                                     js_file=meta.js_file or [], js_code=meta.js_code, css_style=meta.css_style,
-                                    css_file=meta.css_file or [], theme=theme)
+                                    css_file=meta.css_file or [], theme=theme, manifest=manifest)
 
 
 @lru_cache(maxsize=64)
@@ -65,7 +68,7 @@ def check_theme(theme):
         raise RuntimeError("Can't find css file for theme `%s`" % theme)
 
 
-def parse_app_metadata(func):
+def parse_app_metadata(func) -> AppMeta:
     """Get metadata form pywebio task function, fallback to global config in empty meta field."""
     prefix = '_pywebio_'
     attrs = get_function_attr(func, [prefix + k for k in config_keys])
@@ -225,7 +228,42 @@ def seo(title, description=None, app=None):
     return config(title=title, description=description)
 
 
-def config(*, title=None, description=None, theme=None, js_code=None, js_file=[], css_style=None, css_file=[]):
+def manifest_tag(base_url, meta: AppMeta):
+    """Generate inline web app manifest
+    https://stackoverflow.com/questions/46221528/inline-the-web-app-manifest
+    """
+    if meta.manifest is False:
+        return ""
+
+    manifest_ = meta.manifest or {}
+    if manifest_ is True:
+        manifest_ = {}
+
+    manifest = {
+        "name": meta.title,
+        "description": meta.description,
+        "start_url": ".",
+        "display": "standalone",
+        "theme_color": "white",
+        "background_color": "white",
+        "icons": [
+            {"src": f"{base_url}image/apple-touch-icon.png", "type": "image/png", "sizes": "180x180"},
+        ]
+    }
+    manifest.update(manifest_)
+
+    icon = manifest.pop("icon", None)
+    if not icon:
+        icon = base_url + 'image/apple-touch-icon.png'
+
+    manifest_encode = urllib.parse.quote(json.dumps(manifest))
+    tag = f"""<link rel="apple-touch-icon" href="{icon}">
+    <link rel="manifest" href='data:application/manifest+json,{manifest_encode}' />"""
+    return tag
+
+
+def config(*, title=None, description=None, theme=None, js_code=None, js_file=[], css_style=None, css_file=[],
+           manifest=True):
     """PyWebIO application configuration
 
     :param str title: Application title
@@ -244,6 +282,15 @@ def config(*, title=None, description=None, theme=None, js_code=None, js_file=[]
     :param str/list js_file: The javascript files that inject to page, can be a URL in str or a list of it.
     :param str css_style: The CSS style that you want to inject to page.
     :param str/list css_file: The CSS files that inject to page, can be a URL in str or a list of it.
+    :param bool/dict manifest: `Web application manifest <https://developer.mozilla.org/en-US/docs/Web/Manifest>`_ configuration.
+        This feature allows you to add a shortcut to the home screen of your mobile device, and launch the app like a native app.
+        If set to ``True``, the default manifest will be used. You can also specify the manifest content in dict.
+        If ``False``, the manifest will be disabled.
+
+        .. collapse:: Note for icon configuration
+
+            Currently, the `icons <https://developer.mozilla.org/en-US/docs/Web/Manifest/icons>`_ field of the manifest
+            is not supported. Instead, you can use the ``icon`` field to specify the icon url.
 
     ``config()`` can be used in 2 ways: direct call and decorator.
     If you call ``config()`` directly, the configuration will be global.

+ 1 - 0
pywebio/platform/tpl/index.html

@@ -7,6 +7,7 @@
     <meta name="description" content="{{ description }}">
     <link rel="icon" type="image/png" sizes="32x32" href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAwklEQVQ4T63TvU5CQRCG4WcwMfEuqOgNtQ2Nd4CxV2LHtVhJ0N7AHdjQUBtrrLwLA4ks2Rx+/Qucw3Y78807M7sz4ft5dq6mI7RQX7o/JCNzfdfetkNifRk6k9wLN9jYdxMkyZPQ1faZXYUwB/OCix8V/W4Y4zJDCsBAX7jdM7iQJY+udELu+cTrP2X/xU2+NMPAg3B3UPaVOOmFoQkapQC8Z8AUpyUBs6MAKrZQ+RErf2PlQTrKKK8gpZdpewgOXOcFTTxEjYwMoIkAAAAASUVORK5CYII=" id="favicon32">
     <link rel="icon" type="image/png" sizes="16x16" href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAABmUlEQVRYR82XK0wDQRCGv21TUUUJGBwGDBggGCSGBIcAWnBAgsNAgkKhSMDgCA8HtEXgSDBIDC9DDRgcpoSiKo52yea49DiutMttsz27M/98N7s7OyNo9tujgxSTwDiCIaAXSH27l4AXJA/AFSUuWOajGWnR0ChLP3HWkWSAZEN716CM4JQKW6R5+sunPkCeJJJNBCtAosnAQTMHyS6CDWYoh2mEAxzTR4JzYOCfgYNuBRymmOc5uPAbIMswMS6BbkPBPZkiVSZIc+/X/Qng/vl1C4LXIBzG/JmoAag9hxuDaa+XwAIw6p2JGkCObQSrhtMeLifZYZY1tegCqKsW4zHCadfldqgyqK6oC3DGIZIFXZVI9oIjplkUqArXyatGkYkU1+dc5p0eQY4MghNTqlo6kjkFsI9gScvRlLHkQJDnFhgxpampc6cAikCXpqMp8zcF8AnETSlq6lTaAsD6Flg+hNavofVCZL0UW3+M2uI5VhBWGxIFYL0lUxBWm1KviFttyz0Iq4OJB2F1NPO/qdaG0+DD3qLx/AuMVJFhmC8dSgAAAABJRU5ErkJggg==" id="favicon16">
+    {% raw manifest %}
     <link rel="stylesheet" href="{{ base_url }}css/markdown.min.css">
     <link rel="stylesheet" href="{{ base_url }}css/codemirror.min.css">
     <link rel="stylesheet" href="{{ base_url }}css/toastify.min.css">

+ 5 - 5
setup.py

@@ -40,11 +40,11 @@ setup(
         # installed) and in MANIFEST.in (which determines what gets included
         # in the sdist tarball)
         "pywebio": [
-            "html/codemirror/**",
-            "html/css/**",
-            "html/css/bs-theme/**",
-            "html/image/**",
-            "html/js/**",
+            "html/codemirror/*",
+            "html/image/*",
+            "html/js/*",
+            "html/css/*",
+            "html/css/bs-theme/*",
             "platform/tpl/index.html"
         ],
     },