1
0

__main__.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import tornado.ioloop
  2. import tornado.web
  3. from demos.bmi import main as bmi
  4. from demos.chat_room import main as chat_room
  5. from demos.input_usage import main as input_usage
  6. from demos.output_usage import main as output_usage
  7. from demos.config import charts_demo_host
  8. from demos.doc_demo import get_app as get_doc_demo_app
  9. from demos.set_env_demo import main as set_env_demo
  10. from demos.markdown_previewer import main as markdown_previewer
  11. from demos.gomoku_game import main as gomoku_game
  12. from pywebio import STATIC_PATH
  13. from pywebio.output import put_markdown, put_row, put_html
  14. from pywebio.platform.tornado import webio_handler
  15. from pywebio.session import info as session_info
  16. from tornado.options import define, options
  17. index_md = r"""### Basic demo
  18. - [BMI calculation](./bmi): Calculating Body Mass Index based on height and weight
  19. - [Online chat room](./chat_room): Chat with everyone currently online (using less than 90 lines of code)
  20. - [Markdown live preview](./markdown_previewer): The online markdown editor with live preview (using less than 40 lines of code)
  21. - [Online Gomoku game](./gomoku_game): An online shared Gomoku game (using less than 100 lines of code)
  22. - [Input demo](./input_usage): Demonstrate the usage of PyWebIO input module
  23. - [Output demo](./output_usage): Demonstrate the usage of PyWebIO output module
  24. ### Data visualization demo
  25. PyWebIO supports for data visualization with the third-party libraries.
  26. - Use `bokeh` for data visualization [**demos**]({charts_demo_host}/?app=bokeh)
  27. - Use `plotly` for data visualization [**demos**]({charts_demo_host}/?app=plotly)
  28. - Use `pyecharts` to create Echarts-based charts in Python [**demos**]({charts_demo_host}/?app=pyecharts)
  29. - Use `pyg2plot` to create G2Plot-based charts in Python [**demos**]({charts_demo_host}/?app=pyg2plot)
  30. - Use `cutecharts.py` to create hand drawing style charts [**demos**]({charts_demo_host}/?app=cutecharts)
  31. **Screenshots**
  32. <a href="{charts_demo_host}/?app=bokeh">
  33. <img src="https://cdn.jsdelivr.net/gh/wang0618/pywebio-chart-gallery/assets/bokeh.png" alt="bokeh demo">
  34. </a>
  35. <a href="{charts_demo_host}/?app=plotly">
  36. <img src="https://cdn.jsdelivr.net/gh/wang0618/pywebio-chart-gallery/assets/plotly.png" alt="plotly demo">
  37. </a>
  38. <a href="{charts_demo_host}/?app=pyecharts">
  39. <img src="https://cdn.jsdelivr.net/gh/wang0618/pywebio-chart-gallery/assets/pyecharts.gif" alt="pyecharts demo">
  40. </a>
  41. <a href="{charts_demo_host}/?app=cutecharts">
  42. <img src="https://cdn.jsdelivr.net/gh/wang0618/pywebio-chart-gallery/assets/cutecharts.png" alt="cutecharts demo">
  43. </a>
  44. ### Links
  45. * PyWebIO Github [github.com/wang0618/PyWebIO](https://github.com/wang0618/PyWebIO)
  46. * Document [pywebio.readthedocs.io](https://pywebio.readthedocs.io)
  47. """.format(charts_demo_host=charts_demo_host)
  48. index_md_zh = r"""### 基本demo
  49. - [BMI计算](./bmi): 根据身高体重计算BMI指数
  50. - [聊天室](./chat_room): 和当前所有在线的人聊天 (不到90行代码实现)
  51. - [Markdown实时预览](./markdown_previewer): 可以实时预览的在线Markdown编辑器 (不到40行代码实现)
  52. - [在线五子棋游戏](./gomoku_game): 多人协作对战的五子棋游戏 (不到100行代码实现)
  53. - [输入演示](./input_usage): 演示PyWebIO输入模块的用法
  54. - [输出演示](./output_usage): 演示PyWebIO输出模块的用法
  55. - 更多Demo请见[文档](https://pywebio.readthedocs.io)中示例代码的在线Demo
  56. ### 数据可视化demo
  57. PyWebIO还支持使用第三方库进行数据可视化
  58. - 使用`bokeh`进行数据可视化 [**demos**]({charts_demo_host}/?app=bokeh)
  59. - 使用`plotly`进行数据可视化 [**demos**]({charts_demo_host}/?app=plotly)
  60. - 使用`pyecharts`创建基于Echarts的图表 [**demos**]({charts_demo_host}/?app=pyecharts)
  61. - 使用`pyg2plot`创建基于G2Plot的图表 [**demos**]({charts_demo_host}/?app=pyg2plot)
  62. - 使用`cutecharts.py`创建卡通风格图表 [**demos**]({charts_demo_host}/?app=cutecharts)
  63. **数据可视化demo截图**
  64. <a href="{charts_demo_host}/?app=bokeh">
  65. <img src="https://cdn.jsdelivr.net/gh/wang0618/pywebio-chart-gallery/assets/bokeh.png" alt="bokeh demo">
  66. </a>
  67. <a href="{charts_demo_host}/?app=plotly">
  68. <img src="https://cdn.jsdelivr.net/gh/wang0618/pywebio-chart-gallery/assets/plotly.png" alt="plotly demo">
  69. </a>
  70. <a href="{charts_demo_host}/?app=pyecharts">
  71. <img src="https://cdn.jsdelivr.net/gh/wang0618/pywebio-chart-gallery/assets/pyecharts.gif" alt="pyecharts demo">
  72. </a>
  73. <a href="{charts_demo_host}/?app=cutecharts">
  74. <img src="https://cdn.jsdelivr.net/gh/wang0618/pywebio-chart-gallery/assets/cutecharts.png" alt="cutecharts demo">
  75. </a>
  76. ### Links
  77. * PyWebIO Github [github.com/wang0618/PyWebIO](https://github.com/wang0618/PyWebIO)
  78. * 使用手册和实现文档见 [pywebio.readthedocs.io](https://pywebio.readthedocs.io/zh_CN/latest/)
  79. """.format(charts_demo_host=charts_demo_host)
  80. def index():
  81. """PyWebIO demos
  82. Basic demo and data visualization demo of PyWebIO.
  83. PyWebIO的基本demo和数据可视化demo
  84. """
  85. put_row([
  86. put_markdown('# PyWebIO demos'),
  87. put_html('<a class="github-button" data-size="large" href="https://github.com/wang0618/PyWebIO" data-show-count="true" aria-label="Star wang0618/PyWebIO on GitHub">Star</a>')
  88. ], size='1fr auto').style('align-items:center')
  89. put_html('<script async defer src="https://buttons.github.io/buttons.js"></script>')
  90. if 'zh' in session_info.user_language:
  91. put_markdown(index_md_zh)
  92. else:
  93. put_markdown(index_md)
  94. if __name__ == "__main__":
  95. define("port", default=8080, help="run on the given port", type=int)
  96. tornado.options.parse_command_line()
  97. application = tornado.web.Application([
  98. (r"/", webio_handler(index, cdn=False)),
  99. (r"/bmi", webio_handler(bmi, cdn=False)),
  100. (r"/chat_room", webio_handler(chat_room, cdn=False)),
  101. (r"/input_usage", webio_handler(input_usage, cdn=False)),
  102. (r"/output_usage", webio_handler(output_usage, cdn=False)),
  103. (r"/doc_demo", webio_handler(get_doc_demo_app(), cdn=False)),
  104. (r"/set_env_demo", webio_handler(set_env_demo, cdn=False)),
  105. (r"/markdown_previewer", webio_handler(markdown_previewer, cdn=False)),
  106. (r"/gomoku_game", webio_handler(gomoku_game, cdn=False)),
  107. (r"/(.*)", tornado.web.StaticFileHandler, {"path": STATIC_PATH, 'default_filename': 'index.html'})
  108. ])
  109. application.listen(port=options.port)
  110. tornado.ioloop.IOLoop.current().start()