Skip to content

Commit

Permalink
demos: migrate demos to new PyWebIO version
Browse files Browse the repository at this point in the history
  • Loading branch information
wang0618 committed Mar 18, 2021
1 parent 886e92e commit cbf943d
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 17 deletions.
5 changes: 3 additions & 2 deletions demos/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
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
Expand Down Expand Up @@ -70,6 +70,7 @@
- 使用`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截图**
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions demos/bmi.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
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():
Expand Down
4 changes: 2 additions & 2 deletions demos/bokeh_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

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):
Expand Down Expand Up @@ -35,7 +35,7 @@ def callback(attr, old, new):
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代码交互的可视化图表。
Expand Down
10 changes: 5 additions & 5 deletions demos/chat_room.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@
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

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"""
Expand Down
13 changes: 10 additions & 3 deletions demos/input_usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@
: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():
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions demos/output_usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
"""
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):
Expand Down
2 changes: 1 addition & 1 deletion demos/set_env_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

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():
Expand Down

0 comments on commit cbf943d

Please sign in to comment.