doc_demo.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. """
  2. Run the example code in the documentation online
  3. """
  4. import base64
  5. from functools import partial
  6. from os import path, listdir
  7. from pywebio import start_server
  8. from pywebio.platform import config
  9. from pywebio.session import local as session_local, info as session_info
  10. ##########################################
  11. # Pre-import modules for demo
  12. import time # lgtm [py/unused-import]
  13. from pywebio.input import *
  14. from pywebio.output import *
  15. from pywebio.session import *
  16. from pywebio.pin import *
  17. from pywebio_battery import *
  18. ##########################################
  19. here_dir = path.dirname(path.abspath(__file__))
  20. playground_host = "https://play.pywebio.online"
  21. def t(eng, chinese):
  22. """return English or Chinese text according to the user's browser language"""
  23. return chinese if 'zh' in session_info.user_language else eng
  24. def playground(code):
  25. code = f"{PRE_IMPORT}\n{code}"
  26. encode = base64.b64encode(code.encode('utf8')).decode('utf8')
  27. url = f"{playground_host}/#{encode}"
  28. run_js('window.open(url)', url=url)
  29. def gen_snippets(code):
  30. code = code.replace('# ..demo-only', '')
  31. code = '\n'.join(i for i in code.splitlines() if '# ..doc-only' not in i)
  32. parts = code.split('\n## ----\n')
  33. for p in parts:
  34. yield p.strip('\n')
  35. def run_code(code, scope):
  36. with use_scope(scope):
  37. try:
  38. """
  39. Remember that at module level, globals and locals are the same dictionary.
  40. If exec gets two separate objects as globals and locals,
  41. the code will be executed as if it were embedded in a class definition.
  42. https://docs.python.org/3/library/functions.html#exec
  43. """
  44. exec(code, session_local.globals)
  45. except Exception as e:
  46. toast('Exception occurred: "%s:%s"' % (type(e).__name__, e), color='error')
  47. PRE_IMPORT = """from pywebio.input import *
  48. from pywebio.output import *
  49. from pywebio.session import *
  50. from pywebio.pin import *
  51. from pywebio import start_server
  52. """
  53. APP_TPL = f"""{PRE_IMPORT}
  54. def main():
  55. %s
  56. start_server(main, port=8080, debug=True)
  57. """
  58. CLIPBOARD_SETUP = """
  59. window.writeText = function(text) {
  60. const input = document.createElement('textarea');
  61. input.style.opacity = 0;
  62. input.style.position = 'absolute';
  63. input.style.left = '-100000px';
  64. document.body.appendChild(input);
  65. input.value = text;
  66. input.select();
  67. input.setSelectionRange(0, text.length);
  68. document.execCommand('copy');
  69. document.body.removeChild(input);
  70. return true;
  71. }
  72. """
  73. def copytoclipboard(code):
  74. code = APP_TPL % code.replace('\n', '\n ')
  75. run_js("writeText(text)", text=code)
  76. toast('The code has been copied to the clipboard')
  77. def handle_code(code, title):
  78. run_js(CLIPBOARD_SETUP)
  79. session_local.globals = dict(globals())
  80. if title:
  81. put_markdown('## %s' % title)
  82. for p in gen_snippets(code):
  83. with use_scope() as scope:
  84. put_code(p, 'python')
  85. put_buttons(
  86. [t('Run', '运行'),
  87. t("Edit", '编辑'),
  88. t("Copy to clipboard", '复制代码')],
  89. onclick=[
  90. partial(run_code, code=p, scope=scope),
  91. partial(playground, code=p),
  92. partial(copytoclipboard, code=p)
  93. ]
  94. )
  95. put_markdown('----')
  96. def get_app():
  97. """PyWebIO demos from document
  98. Run the demos from the document online.
  99. """
  100. app = {}
  101. try:
  102. demos = listdir(path.join(here_dir, 'doc_demos'))
  103. except Exception:
  104. demos = []
  105. for name in demos:
  106. code = open(path.join(here_dir, 'doc_demos', name)).read()
  107. title, code = code.split('\n\n', 1)
  108. app[name] = partial(handle_code, code=code, title=title)
  109. app[name] = config(title=name, description=title)(app[name])
  110. return app
  111. main = get_app()
  112. if __name__ == '__main__':
  113. start_server(main, debug=True, port=8080)