doc_demo.py 3.5 KB

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