set_env_demo.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. """
  2. `pywebio.session.set_env()` demo
  3. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  4. """
  5. from pywebio import start_server
  6. from pywebio.input import *
  7. from pywebio.output import *
  8. from pywebio.session import *
  9. import datetime
  10. import asyncio
  11. async def main():
  12. """`pywebio.session.set_env()` demo"""
  13. set_scope('time')
  14. put_markdown('> 可用于观察 `output_animation` 项的动画效果')
  15. put_markdown('---')
  16. async def bg_task():
  17. while 1:
  18. with use_scope('time', clear=True):
  19. put_text('当前时间:', datetime.datetime.now())
  20. await asyncio.sleep(1)
  21. run_async(bg_task())
  22. put_buttons(['输出文本'], [lambda: put_text(datetime.datetime.now())])
  23. put_markdown('> 可用于观察 `auto_scroll_bottom` 项的自动滚动效果')
  24. put_markdown('---')
  25. put_text('Some text.\n' * 10)
  26. state = {
  27. 'title': 'PyWebIO set_env() Demo',
  28. 'output_animation': True,
  29. 'auto_scroll_bottom': False,
  30. }
  31. set_env(**state)
  32. while 1:
  33. curr_state_info = ', '.join('%s=%r' % (k, v) for k, v in state.items())
  34. key = await actions('选择要更改的会话环境设置项', list(state.keys()), help_text='当前状态:' + curr_state_info)
  35. if key == 'title':
  36. state['title'] = await input('请输入标题', value=state['title'])
  37. set_env(title=state['title'])
  38. toast('已将标题设置为%r' % state['title'])
  39. elif key in state:
  40. state[key] = not (state[key])
  41. set_env(**{key: state[key]})
  42. toast('已将`%s`设置为%r' % (key, state[key]))
  43. if __name__ == '__main__':
  44. start_server(main, debug=True, port=8080, cdn=False)