浏览代码

maint: add js library existing check

wangweimin 4 年之前
父节点
当前提交
9d47fe8bb4
共有 4 个文件被更改,包括 29 次插入9 次删除
  1. 6 7
      pywebio/__init__.py
  2. 3 1
      pywebio/platform/httpbased.py
  3. 2 1
      pywebio/platform/tornado.py
  4. 18 0
      pywebio/utils.py

+ 6 - 7
pywebio/__init__.py

@@ -1,20 +1,19 @@
-from .platform import start_server
 from . import input
 from . import output
-from .session import *
-from .exceptions import SessionException, SessionClosedException, SessionNotFoundException
-from .utils import STATIC_PATH
-
-from .__version__ import __description__, __url__, __version__
 from .__version__ import __author__, __author_email__, __license__
-
+from .__version__ import __description__, __url__, __version__
+from .exceptions import SessionException, SessionClosedException, SessionNotFoundException
+from .platform import start_server
 from .platform.bokeh import try_install_bokeh_hook
+from .session import *
+from .utils import STATIC_PATH
 
 try_install_bokeh_hook()
 del try_install_bokeh_hook
 
 # Set default logging handler to avoid "No handler found" warnings.
 import logging
+
 logging.getLogger(__name__).addHandler(logging.NullHandler())
 
 

+ 3 - 1
pywebio/platform/httpbased.py

@@ -24,7 +24,7 @@ import time
 from .utils import make_applications, render_page
 from ..session import CoroutineBasedSession, Session, ThreadBasedSession, register_session_implement_for_target
 from ..session.base import get_session_info_from_headers
-from ..utils import random_str, LRUDict, isgeneratorfunction, iscoroutinefunction
+from ..utils import random_str, LRUDict, isgeneratorfunction, iscoroutinefunction, check_webio_js
 
 
 class HttpContext:
@@ -251,6 +251,8 @@ class HttpHandler:
         :param callable check_origin: 请求来源检查函数。接收请求来源(包含协议和域名和端口部分)字符串,
             返回 ``True/False`` 。若设置了 ``check_origin`` , ``allowed_origins`` 参数将被忽略
         """
+        check_webio_js()
+
         cls = type(self)
 
         self.applications = make_applications(applications)

+ 2 - 1
pywebio/platform/tornado.py

@@ -17,7 +17,7 @@ from tornado.websocket import WebSocketHandler
 from ..session import CoroutineBasedSession, ThreadBasedSession, ScriptModeSession, \
     register_session_implement_for_target, Session
 from ..session.base import get_session_info_from_headers
-from ..utils import get_free_port, wait_host_port, STATIC_PATH, iscoroutinefunction, isgeneratorfunction
+from ..utils import get_free_port, wait_host_port, STATIC_PATH, iscoroutinefunction, isgeneratorfunction, check_webio_js
 from .utils import make_applications, render_page
 
 logger = logging.getLogger(__name__)
@@ -59,6 +59,7 @@ def _webio_handler(applications, check_origin_func=_is_same_site):
     :param callable check_origin_func: check_origin_func(origin, handler) -> bool
     :return: Tornado RequestHandler类
     """
+    check_webio_js()
 
     class WSHandler(WebSocketHandler):
 

+ 18 - 0
pywebio/utils.py

@@ -1,6 +1,7 @@
 import asyncio
 import functools
 import inspect
+import os
 import queue
 import random
 import socket
@@ -314,3 +315,20 @@ _html_value_chars = set(string.ascii_letters + string.digits + '_-')
 def is_html_safe_value(val):
     """检查是字符串是否可以作为html属性值"""
     return all(i in _html_value_chars for i in val)
+
+
+def check_webio_js():
+    js_files = [os.path.join(STATIC_PATH, 'js', i) for i in ('pywebio.js', 'pywebio.min.js')]
+    if any(os.path.isfile(f) for f in js_files):
+        return
+    error_msg = """
+Error: Missing pywebio.js library for frontend page.
+This may be because you cloned or downloaded the project directly from the Git repository.
+
+You Can:
+  * Manually build the pywebio.js file. See `webiojs/README.md` for more info.
+OR
+  * Use the following command to install the latest development version of PyWebIO:
+    pip3 install -U https://code.aliyun.com/wang0618/pywebio/repository/archive.zip
+""".strip()
+    raise RuntimeError(error_msg)