Skip to content

Commit 90f64da

Browse files
committed
add WPA support
1 parent 9eff2a4 commit 90f64da

File tree

4 files changed

+57
-9
lines changed

4 files changed

+57
-9
lines changed
11.6 KB
Loading

pywebio/platform/page.py

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
import urllib.parse
23
from collections import namedtuple
34
from collections.abc import Mapping, Sequence
@@ -21,7 +22,7 @@
2122
DEFAULT_CDN = "https://cdn.jsdelivr.net/gh/wang0618/PyWebIO-assets@v{version}/"
2223

2324
_global_config = {'title': 'PyWebIO Application'}
24-
config_keys = ['title', 'description', 'js_file', 'js_code', 'css_style', 'css_file', 'theme']
25+
config_keys = ['title', 'description', 'js_file', 'js_code', 'css_style', 'css_file', 'theme', 'manifest']
2526
AppMeta = namedtuple('App', config_keys)
2627

2728
_here_dir = path.dirname(path.abspath(__file__))
@@ -45,13 +46,15 @@ def render_page(app, protocol, cdn):
4546
else: # user custom cdn
4647
base_url = cdn.rstrip('/') + '/'
4748

49+
manifest = manifest_tag(base_url, meta)
50+
4851
theme = environ.get('PYWEBIO_THEME', meta.theme) or 'default'
4952
check_theme(theme)
5053

5154
return _index_page_tpl.generate(title=meta.title, description=meta.description, protocol=protocol,
5255
script=True, content='', base_url=base_url, version=version,
5356
js_file=meta.js_file or [], js_code=meta.js_code, css_style=meta.css_style,
54-
css_file=meta.css_file or [], theme=theme)
57+
css_file=meta.css_file or [], theme=theme, manifest=manifest)
5558

5659

5760
@lru_cache(maxsize=64)
@@ -65,7 +68,7 @@ def check_theme(theme):
6568
raise RuntimeError("Can't find css file for theme `%s`" % theme)
6669

6770

68-
def parse_app_metadata(func):
71+
def parse_app_metadata(func) -> AppMeta:
6972
"""Get metadata form pywebio task function, fallback to global config in empty meta field."""
7073
prefix = '_pywebio_'
7174
attrs = get_function_attr(func, [prefix + k for k in config_keys])
@@ -225,7 +228,42 @@ def hello():
225228
return config(title=title, description=description)
226229

227230

228-
def config(*, title=None, description=None, theme=None, js_code=None, js_file=[], css_style=None, css_file=[]):
231+
def manifest_tag(base_url, meta: AppMeta):
232+
"""Generate inline web app manifest
233+
https://stackoverflow.com/questions/46221528/inline-the-web-app-manifest
234+
"""
235+
if meta.manifest is False:
236+
return ""
237+
238+
manifest_ = meta.manifest or {}
239+
if manifest_ is True:
240+
manifest_ = {}
241+
242+
manifest = {
243+
"name": meta.title,
244+
"description": meta.description,
245+
"start_url": ".",
246+
"display": "standalone",
247+
"theme_color": "white",
248+
"background_color": "white",
249+
"icons": [
250+
{"src": f"{base_url}image/apple-touch-icon.png", "type": "image/png", "sizes": "180x180"},
251+
]
252+
}
253+
manifest.update(manifest_)
254+
255+
icon = manifest.pop("icon", None)
256+
if not icon:
257+
icon = base_url + 'image/apple-touch-icon.png'
258+
259+
manifest_encode = urllib.parse.quote(json.dumps(manifest))
260+
tag = f"""<link rel="apple-touch-icon" href="{icon}">
261+
<link rel="manifest" href='data:application/manifest+json,{manifest_encode}' />"""
262+
return tag
263+
264+
265+
def config(*, title=None, description=None, theme=None, js_code=None, js_file=[], css_style=None, css_file=[],
266+
manifest=True):
229267
"""PyWebIO application configuration
230268
231269
:param str title: Application title
@@ -244,6 +282,15 @@ def config(*, title=None, description=None, theme=None, js_code=None, js_file=[]
244282
:param str/list js_file: The javascript files that inject to page, can be a URL in str or a list of it.
245283
:param str css_style: The CSS style that you want to inject to page.
246284
:param str/list css_file: The CSS files that inject to page, can be a URL in str or a list of it.
285+
:param bool/dict manifest: `Web application manifest <https://developer.mozilla.org/en-US/docs/Web/Manifest>`_ configuration.
286+
This feature allows you to add a shortcut to the home screen of your mobile device, and launch the app like a native app.
287+
If set to ``True``, the default manifest will be used. You can also specify the manifest content in dict.
288+
If ``False``, the manifest will be disabled.
289+
290+
.. collapse:: Note for icon configuration
291+
292+
Currently, the `icons <https://developer.mozilla.org/en-US/docs/Web/Manifest/icons>`_ field of the manifest
293+
is not supported. Instead, you can use the ``icon`` field to specify the icon url.
247294
248295
``config()`` can be used in 2 ways: direct call and decorator.
249296
If you call ``config()`` directly, the configuration will be global.

pywebio/platform/tpl/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<meta name="description" content="{{ description }}">
88
<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">
99
<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">
10+
{% raw manifest %}
1011
<link rel="stylesheet" href="{{ base_url }}css/markdown.min.css">
1112
<link rel="stylesheet" href="{{ base_url }}css/codemirror.min.css">
1213
<link rel="stylesheet" href="{{ base_url }}css/toastify.min.css">

setup.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@
4040
# installed) and in MANIFEST.in (which determines what gets included
4141
# in the sdist tarball)
4242
"pywebio": [
43-
"html/codemirror/**",
44-
"html/css/**",
45-
"html/css/bs-theme/**",
46-
"html/image/**",
47-
"html/js/**",
43+
"html/codemirror/*",
44+
"html/image/*",
45+
"html/js/*",
46+
"html/css/*",
47+
"html/css/bs-theme/*",
4848
"platform/tpl/index.html"
4949
],
5050
},

0 commit comments

Comments
 (0)