8.flask_multiple_session_impliment.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import subprocess
  2. from selenium.webdriver import Chrome
  3. import pywebio
  4. import template
  5. import time
  6. import util
  7. from pywebio.input import *
  8. from pywebio.output import *
  9. from pywebio.utils import to_coroutine, run_as_function
  10. def target():
  11. template.basic_output()
  12. template.background_output()
  13. run_as_function(template.basic_input())
  14. actions(buttons=['Continue'])
  15. template.background_input()
  16. async def async_target():
  17. template.basic_output()
  18. await template.coro_background_output()
  19. await to_coroutine(template.basic_input())
  20. await actions(buttons=['Continue'])
  21. await template.coro_background_input()
  22. def test(server_proc: subprocess.Popen, browser: Chrome):
  23. template.test_output(browser)
  24. time.sleep(1)
  25. template.test_input(browser)
  26. time.sleep(1)
  27. template.save_output(browser, '8.flask_multiple_session_impliment_p1.html')
  28. browser.get('http://localhost:8080/io2?_pywebio_debug=1&_pywebio_http_pull_interval=400')
  29. template.test_output(browser)
  30. time.sleep(1)
  31. template.test_input(browser)
  32. time.sleep(1)
  33. template.save_output(browser, '8.flask_multiple_session_impliment_p2.html')
  34. def start_test_server():
  35. pywebio.enable_debug()
  36. from flask import Flask, send_from_directory
  37. from pywebio.platform.flask import webio_view, run_event_loop
  38. from pywebio import STATIC_PATH
  39. import threading
  40. import logging
  41. app = Flask(__name__)
  42. app.add_url_rule('/io', 'webio_view', webio_view(target, cdn=False), methods=['GET', 'POST', 'OPTIONS'])
  43. app.add_url_rule('/io2', 'webio_view_async_target', webio_view(async_target, cdn=False), methods=['GET', 'POST', 'OPTIONS'])
  44. @app.route('/')
  45. @app.route('/<path:static_file>')
  46. def serve_static_file(static_file='index.html'):
  47. return send_from_directory(STATIC_PATH, static_file)
  48. threading.Thread(target=run_event_loop, daemon=True).start()
  49. logging.getLogger('werkzeug').setLevel(logging.WARNING)
  50. app.run(port=8080, host='127.0.0.1')
  51. if __name__ == '__main__':
  52. util.run_test(start_test_server, test, address='http://localhost:8080/io?_pywebio_debug=1&_pywebio_http_pull_interval=400')