浏览代码

demos: migrate demos to new PyWebIO version

wangweimin 4 年之前
父节点
当前提交
cbf943da1c
共有 7 个文件被更改,包括 25 次插入17 次删除
  1. 3 2
      demos/__main__.py
  2. 2 2
      demos/bmi.py
  3. 2 2
      demos/bokeh_app.py
  4. 5 5
      demos/chat_room.py
  5. 10 3
      demos/input_usage.py
  6. 2 2
      demos/output_usage.py
  7. 1 1
      demos/set_env_demo.py

+ 3 - 2
demos/__main__.py

@@ -12,7 +12,7 @@ from demos.set_env_demo import main as set_env_demo
 from pywebio import STATIC_PATH
 from pywebio.output import put_markdown, put_row, put_html, style
 from pywebio.platform.tornado import webio_handler
-from pywebio.session import get_info
+from pywebio.session import info as session_info
 from tornado.options import define, options
 
 index_md = r"""### Basic demo
@@ -70,6 +70,7 @@ PyWebIO还支持使用第三方库进行数据可视化
  - 使用`bokeh`进行数据可视化 [**demos**]({charts_demo_host}/?app=bokeh)
  - 使用`plotly`进行数据可视化 [**demos**]({charts_demo_host}/?app=plotly)
  - 使用`pyecharts`创建基于Echarts的图表 [**demos**]({charts_demo_host}/?app=pyecharts)
+ - 使用`pyg2plot`创建基于G2Plot的图表 [**demos**]({charts_demo_host}/?app=pyg2plot)
  - 使用`cutecharts.py`创建卡通风格图表 [**demos**]({charts_demo_host}/?app=cutecharts)
 
 **数据可视化demo截图**
@@ -108,7 +109,7 @@ def index():
     ], size='1fr auto'), 'align-items:center')
     put_html('<script async defer src="https://buttons.github.io/buttons.js"></script>')
 
-    if 'zh' in get_info().user_language:
+    if 'zh' in session_info.user_language:
         put_markdown(index_md_zh)
     else:
         put_markdown(index_md)

+ 2 - 2
demos/bmi.py

@@ -9,12 +9,12 @@ Simple application for calculating `Body Mass Index <https://en.wikipedia.org/wi
 from pywebio import start_server
 from pywebio.input import *
 from pywebio.output import *
-from pywebio.session import get_info
+from pywebio.session import info as session_info
 
 
 def t(eng, chinese):
     """return English or Chinese text according to the user's browser language"""
-    return chinese if 'zh' in get_info().user_language else eng
+    return chinese if 'zh' in session_info.user_language else eng
 
 
 def main():

+ 2 - 2
demos/bokeh_app.py

@@ -7,7 +7,7 @@ from bokeh.sampledata.sea_surface_temperature import sea_surface_temperature
 
 from pywebio import start_server
 from pywebio.output import *
-from pywebio.session import get_info
+from pywebio.session import info as session_info
 
 
 def bkapp(doc):
@@ -35,7 +35,7 @@ def bkapp(doc):
 def main():
     output_notebook(verbose=False, notebook_type='pywebio')
 
-    if 'zh' in get_info().user_language:
+    if 'zh' in session_info.user_language:
         put_markdown("""# Bokeh Applications in PyWebIO
         [Bokeh Applications](https://docs.bokeh.org/en/latest/docs/user_guide/server.html) 支持向图表的添加按钮、输入框等交互组件,并向组件添加Python回调,从而创建可以与Python代码交互的可视化图表。
 

+ 5 - 5
demos/chat_room.py

@@ -13,11 +13,7 @@ import asyncio
 from pywebio import start_server
 from pywebio.input import *
 from pywebio.output import *
-from pywebio.session import defer_call, get_info, run_async
-
-def t(eng, chinese):
-    """return English or Chinese text according to the user's browser language"""
-    return chinese if 'zh' in get_info().user_language else eng
+from pywebio.session import defer_call, info as session_info, run_async
 
 # 最大消息记录保存
 MAX_MESSAGES_CNT = 10 ** 4
@@ -25,6 +21,10 @@ MAX_MESSAGES_CNT = 10 ** 4
 chat_msgs = []  # 聊天记录 (name, msg)
 online_users = set()  # 在线用户
 
+def t(eng, chinese):
+    """return English or Chinese text according to the user's browser language"""
+    return chinese if 'zh' in session_info.user_language else eng
+
 
 async def refresh_msg(my_name, msg_box):
     """send new message to current session"""

+ 10 - 3
demos/input_usage.py

@@ -5,15 +5,17 @@ Demonstrate various input usage supported by PyWebIO
 
 :demo_host:`Demo </?pywebio_api=input_usage>`  `Source code <https://github.com/wang0618/PyWebIO/blob/dev/demos/input_usage.py>`_
 """
+import json
+
 from pywebio import start_server
 from pywebio.input import *
 from pywebio.output import *
-from pywebio.session import set_env, get_info
+from pywebio.session import set_env, info as session_info
 
 
 def t(eng, chinese):
     """return English or Chinese text according to the user's browser language"""
-    return chinese if 'zh' in get_info().user_language else eng
+    return chinese if 'zh' in session_info.user_language else eng
 
 
 def main():
@@ -110,7 +112,12 @@ def main():
     text = textarea('Text Area', rows=3, placeholder='Some text')
     put_markdown("`text = %r`" % text)
     img = file_upload("Select a image:", accept="image/*", help_text=t('You can just click "Submit" button', '可以直接选择"提交"'))
-    put_markdown("`img = %r`" % img)
+    if img is None:
+        put_markdown("`img = %r`" % img)
+    else:
+        img['content'] = '...'
+        img.pop('dataurl', None)
+        put_code(json.dumps(img, indent=4, ensure_ascii=False).replace('"..."', '...'), 'json')
 
     # 输入选项
     put_markdown(t("""#### Parameter of input functions

+ 2 - 2
demos/output_usage.py

@@ -7,13 +7,13 @@ Demonstrate various output usage supported by PyWebIO
 """
 from pywebio import start_server
 from pywebio.output import *
-from pywebio.session import hold, get_info
+from pywebio.session import hold, info as session_info
 from functools import partial
 
 
 def t(eng, chinese):
     """return English or Chinese text according to the user's browser language"""
-    return chinese if 'zh' in get_info().user_language else eng
+    return chinese if 'zh' in session_info.user_language else eng
 
 
 def code_block(code, strip_indent=4):

+ 1 - 1
demos/set_env_demo.py

@@ -12,7 +12,7 @@ import asyncio
 
 def t(eng, chinese):
     """return English or Chinese text according to the user's browser language"""
-    return chinese if 'zh' in get_info().user_language else eng
+    return chinese if 'zh' in info.user_language else eng
 
 
 async def main():