doc_demo.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. """
  2. 文档中示例代码在线运行
  3. Run the example code in the documentation online
  4. """
  5. from functools import partial
  6. from os import path, listdir
  7. from pywebio import start_server
  8. from pywebio.output import *
  9. from pywebio.input import *
  10. from pywebio.platform import seo
  11. from pywebio.session import *
  12. from pywebio.session import local as session_local
  13. def t(eng, chinese):
  14. """return English or Chinese text according to the user's browser language"""
  15. return chinese if 'zh' in get_info().user_language else eng
  16. here_dir = path.dirname(path.abspath(__file__))
  17. def gen_snippets(code):
  18. code = code.replace('# ..demo-only', '')
  19. code = '\n'.join(i for i in code.splitlines() if '# ..doc-only' not in i)
  20. parts = code.split('\n## ----\n')
  21. for p in parts:
  22. yield p.strip('\n')
  23. def run_code(code, scope):
  24. with use_scope(scope):
  25. try:
  26. """
  27. Remember that at module level, globals and locals are the same dictionary.
  28. If exec gets two separate objects as globals and locals,
  29. the code will be executed as if it were embedded in a class definition.
  30. https://docs.python.org/3/library/functions.html#exec
  31. """
  32. exec(code, session_local.globals)
  33. except Exception as e:
  34. toast('Exception occurred: "%s:%s"' % (type(e).__name__, e), color='error')
  35. IMPORT_CODE = """from pywebio.input import *
  36. from pywebio.output import *
  37. from pywebio.session import *
  38. """
  39. def copytoclipboard(code):
  40. code = IMPORT_CODE + code
  41. if 'put_buttons(' in code or 'put_file(' in code:
  42. code += '\n\nhold() # keep session alive'
  43. run_js("writeText(text)", text=code)
  44. toast('The code has been copied to the clipboard')
  45. def handle_code(code, title):
  46. run_js("""
  47. window.writeText = function(text) {
  48. const input = document.createElement('textarea');
  49. input.style.opacity = 0;
  50. input.style.position = 'absolute';
  51. input.style.left = '-100000px';
  52. document.body.appendChild(input);
  53. input.value = text;
  54. input.select();
  55. input.setSelectionRange(0, text.length);
  56. document.execCommand('copy');
  57. document.body.removeChild(input);
  58. return true;
  59. }
  60. """)
  61. session_local.globals = dict(globals())
  62. if title:
  63. put_markdown('## %s' % title)
  64. for p in gen_snippets(code):
  65. with use_scope() as scope:
  66. put_code(p, 'python')
  67. put_buttons([t('Run', '运行'), t("Copy to clipboard", '复制代码')], onclick=[
  68. partial(run_code, code=p, scope=scope),
  69. partial(copytoclipboard, code=p)
  70. ])
  71. put_markdown('----')
  72. hold()
  73. def get_app():
  74. """PyWebIO demos from document
  75. Run the demos from the document online.
  76. """
  77. app = {}
  78. try:
  79. demos = listdir(path.join(here_dir, 'doc_demos'))
  80. except Exception:
  81. demos = []
  82. for name in demos:
  83. code = open(path.join(here_dir, 'doc_demos', name)).read()
  84. title, code = code.split('\n\n', 1)
  85. app[name] = partial(handle_code, code=code, title=title)
  86. app[name] = seo('', title, app[name])
  87. return app
  88. if __name__ == '__main__':
  89. a = get_app()
  90. start_server(get_app(), debug=True, port=8080, cdn=False)