123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- """
- `pywebio.session.set_env()` demo
- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- """
- from pywebio import start_server
- from pywebio.input import *
- from pywebio.output import *
- from pywebio.session import *
- import datetime
- import asyncio
- async def main():
- put_buttons(['返回底部'], [lambda: scroll_to('ROOT', Position.BOTTOM)])
- put_markdown('---')
- set_scope('time')
- put_markdown('---')
- async def bg_task():
- count = 5
- while 1:
- with use_scope('time', clear=True):
- put_text('当前时间:', datetime.datetime.now())
- put_text(count)
- count += 1
- await asyncio.sleep(1)
- put_text('\n'.join(str(i) for i in range(5)))
- run_async(bg_task())
- state = {
- 'title': 'PyWebIO set_env() Demo',
- 'output_animation': True,
- 'auto_scroll_bottom': True,
- 'output_fixed_height': True,
- }
- set_env(**state)
- while 1:
- curr_state_info = ', '.join('%s=%r' % (k, v) for k, v in state.items())
- key = await actions('选择要更改的会话环境设置项', list(state.keys()), help_text='当前状态:' + curr_state_info)
- if key == 'title':
- state['title'] = await input('请输入标题')
- set_env(title=state['title'])
- toast('已将标题设置为%r' % state['title'])
- elif key == 'output_fixed_height':
- act = await actions('选择要对output_fixed_height设置项进行的操作', ['设置为False', '自定义高度'])
- if act == '自定义高度':
- state['output_fixed_height'] = await input('请输入输出区高度(像素)', type=NUMBER)
- else:
- state['output_fixed_height'] = False
- set_env(output_fixed_height=state['output_fixed_height'])
- elif key in state:
- state[key] = not (state[key])
- set_env(**{key: state[key]})
- toast('已将`%s`设置为%r' % (key, state[key]))
- if __name__ == '__main__':
- start_server(main, debug=True, port=8080)
|