8.flask_multiple_session_impliment.py 2.1 KB

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