Explorar o código

change some function position

wangweimin %!s(int64=5) %!d(string=hai) anos
pai
achega
d31baf1cc0
Modificáronse 6 ficheiros con 57 adicións e 60 borrados
  1. 4 3
      pywebio/demos/overview-zh.py
  2. 1 1
      pywebio/input.py
  3. 44 5
      pywebio/io_ctrl.py
  4. 5 3
      pywebio/ioloop.py
  5. 3 4
      pywebio/output.py
  6. 0 44
      pywebio/output_ctl.py

+ 4 - 3
pywebio/demos/overview-zh.py

@@ -2,12 +2,13 @@
 使用PyWebIO来介绍PyWebIO的各个特性
 使用PyWebIO来介绍PyWebIO的各个特性
 """
 """
 
 
-from pywebio.input import *
-from pywebio.ioloop import start_ioloop
-from pywebio.output import *
 import asyncio
 import asyncio
 from datetime import datetime
 from datetime import datetime
 
 
+from pywebio.input import *
+from pywebio.ioloop import start_ioloop, run_async
+from pywebio.output import *
+
 
 
 async def other(data, save):
 async def other(data, save):
     put_text("You click %s button" % data)
     put_text("You click %s button" % data)

+ 1 - 1
pywebio/input.py

@@ -28,7 +28,7 @@ from base64 import b64decode
 from collections.abc import Mapping
 from collections.abc import Mapping
 from typing import Coroutine
 from typing import Coroutine
 
 
-from .input_ctrl import single_input, input_control
+from .io_ctrl import single_input, input_control
 
 
 logger = logging.getLogger(__name__)
 logger = logging.getLogger(__name__)
 
 

+ 44 - 5
pywebio/input_ctrl.py → pywebio/io_ctrl.py

@@ -1,14 +1,14 @@
+import asyncio
+import inspect
 import logging
 import logging
 
 
-from .framework import WebIOFuture, Global
+from .framework import Global, Task
+from .framework import WebIOFuture
+from .ioloop import run_async
 
 
 logger = logging.getLogger(__name__)
 logger = logging.getLogger(__name__)
 
 
 
 
-def run_async(coro_obj):
-    Global.active_ws.inactive_coro_instances.append(coro_obj)
-
-
 def send_msg(cmd, spec=None):
 def send_msg(cmd, spec=None):
     msg = dict(command=cmd, spec=spec, coro_id=Global.active_coro_id)
     msg = dict(command=cmd, spec=spec, coro_id=Global.active_coro_id)
     Global.active_ws.add_server_msg(msg)
     Global.active_ws.add_server_msg(msg)
@@ -119,3 +119,42 @@ async def input_event_handle(item_valid_funcs, form_valid_funcs, preprocess_func
             logger.warning("Unhandled Event: %s", event)
             logger.warning("Unhandled Event: %s", event)
 
 
     return data
     return data
+
+
+def output_register_callback(callback, save, mutex_mode):
+    """
+    为输出区显示的控件注册回调函数
+
+    原理:
+        向框架注册一个新协程,在协程内对回调函数进行调用 callback(widget_data, save)
+        协程会在用户与控件交互时触发
+
+    :return: 协程id
+    """
+
+    async def callback_coro():
+        while True:
+            event = await next_event()
+            assert event['event'] == 'callback'
+            coro = None
+            if asyncio.iscoroutinefunction(callback):
+                coro = callback(event['data'], save)
+            elif inspect.isgeneratorfunction(callback):
+                coro = asyncio.coroutine(callback)(save, event['data'])
+            else:
+                try:
+                    callback(event['data'], save)
+                except:
+                    Global.active_ws.on_coro_error()
+
+            if coro is not None:
+                if mutex_mode:
+                    await coro
+                else:
+                    run_async(coro)
+
+    callback_task = Task(callback_coro(), Global.active_ws)
+    callback_task.coro.send(None)  # 激活,Non't callback.step() ,导致嵌套调用step  todo 与inactive_coro_instances整合
+    Global.active_ws.coros[callback_task.coro_id] = callback_task
+
+    return callback_task.coro_id

+ 5 - 3
pywebio/ioloop.py

@@ -1,11 +1,13 @@
-import logging
-
 import tornado.websocket
 import tornado.websocket
-from tornado.log import gen_log, access_log
 from tornado.web import StaticFileHandler
 from tornado.web import StaticFileHandler
+from .framework import Global
 from .platform.tornado import ws_handler, STATIC_PATH
 from .platform.tornado import ws_handler, STATIC_PATH
 
 
 
 
+def run_async(coro_obj):
+    Global.active_ws.inactive_coro_instances.append(coro_obj)
+
+
 def start_ioloop(coro_func, port=8080, debug=True, tornado_app_args=None):
 def start_ioloop(coro_func, port=8080, debug=True, tornado_app_args=None):
     handlers = [(r"/ws", ws_handler(coro_func)),
     handlers = [(r"/ws", ws_handler(coro_func)),
                 (r"/(.*)", StaticFileHandler, {"path": STATIC_PATH,
                 (r"/(.*)", StaticFileHandler, {"path": STATIC_PATH,

+ 3 - 4
pywebio/output.py

@@ -35,8 +35,7 @@ from base64 import b64encode
 from collections.abc import Mapping
 from collections.abc import Mapping
 
 
 from .framework import Global
 from .framework import Global
-from .input_ctrl import send_msg
-from .output_ctl import register_callback
+from .io_ctrl import output_register_callback, send_msg
 
 
 
 
 def set_title(title):
 def set_title(title):
@@ -260,7 +259,7 @@ def td_buttons(buttons, onclick, save=None, mutex_mode=False):
     :param str buttons, onclick, save: 与 `put_buttons` 函数的同名参数含义一致
     :param str buttons, onclick, save: 与 `put_buttons` 函数的同名参数含义一致
     """
     """
     btns = _format_button(buttons)
     btns = _format_button(buttons)
-    callback_id = register_callback(onclick, save, mutex_mode)
+    callback_id = output_register_callback(onclick, save, mutex_mode)
     tpl = '<button type="button" value="{value}" class="btn btn-primary btn-sm" ' \
     tpl = '<button type="button" value="{value}" class="btn btn-primary btn-sm" ' \
           'onclick="WebIO.DisplayAreaButtonOnClick(this, \'%s\')">{label}</button>' % callback_id
           'onclick="WebIO.DisplayAreaButtonOnClick(this, \'%s\')">{label}</button>' % callback_id
     btns_html = [tpl.format(**b) for b in btns]
     btns_html = [tpl.format(**b) for b in btns]
@@ -288,7 +287,7 @@ def put_buttons(buttons, onclick, small=False, save=None, mutex_mode=False, anch
     """
     """
     assert not (before and after), "Parameter 'before' and 'after' cannot be specified at the same time"
     assert not (before and after), "Parameter 'before' and 'after' cannot be specified at the same time"
     btns = _format_button(buttons)
     btns = _format_button(buttons)
-    callback_id = register_callback(onclick, save, mutex_mode)
+    callback_id = output_register_callback(onclick, save, mutex_mode)
     _put_content('buttons', callback_id=callback_id, buttons=btns, small=small, anchor=anchor, before=before,
     _put_content('buttons', callback_id=callback_id, buttons=btns, small=small, anchor=anchor, before=before,
                  after=after)
                  after=after)
 
 

+ 0 - 44
pywebio/output_ctl.py

@@ -1,44 +0,0 @@
-import asyncio
-import inspect
-
-from .framework import Global, Task
-from .input_ctrl import next_event, run_async
-
-
-def register_callback(callback, save, mutex_mode):
-    """
-    为输出区显示的控件注册回调函数
-
-    原理:
-        向框架注册一个新协程,在协程内对回调函数进行调用 callback(widget_data, save)
-        协程会在用户与控件交互时触发
-
-    :return: 协程id
-    """
-
-    async def callback_coro():
-        while True:
-            event = await next_event()
-            assert event['event'] == 'callback'
-            coro = None
-            if asyncio.iscoroutinefunction(callback):
-                coro = callback(event['data'], save)
-            elif inspect.isgeneratorfunction(callback):
-                coro = asyncio.coroutine(callback)(save, event['data'])
-            else:
-                try:
-                    callback(event['data'], save)
-                except:
-                    Global.active_ws.on_coro_error()
-
-            if coro is not None:
-                if mutex_mode:
-                    await coro
-                else:
-                    run_async(coro)
-
-    callback_task = Task(callback_coro(), Global.active_ws)
-    callback_task.coro.send(None)  # 激活,Non't callback.step() ,导致嵌套调用step  todo 与inactive_coro_instances整合
-    Global.active_ws.coros[callback_task.coro_id] = callback_task
-
-    return callback_task.coro_id