8.flask_multiple_session_impliment.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. set_auto_scroll_bottom(False)
  12. template.basic_output()
  13. template.background_output()
  14. run_as_function(template.basic_input())
  15. actions(buttons=['Continue'])
  16. template.background_input()
  17. async def async_target():
  18. set_auto_scroll_bottom(False)
  19. template.basic_output()
  20. await template.coro_background_output()
  21. await to_coroutine(template.basic_input())
  22. await actions(buttons=['Continue'])
  23. await template.coro_background_input()
  24. def test(server_proc: subprocess.Popen, browser: Chrome):
  25. template.test_output(browser, percy_prefix='[multi flask coro]')
  26. time.sleep(1)
  27. template.test_input(browser, percy_prefix='[multi flask coro]')
  28. time.sleep(3)
  29. browser.get('http://localhost:8080?_pywebio_debug=1&pywebio_api=io2')
  30. template.test_output(browser, percy_prefix='[multi flask thread]')
  31. time.sleep(1)
  32. template.test_input(browser, percy_prefix='[multi flask thread]')
  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), methods=['GET', 'POST', 'OPTIONS'])
  42. app.add_url_rule('/io2', 'webio_view_async_target', webio_view(async_target), 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)
  50. if __name__ == '__main__':
  51. util.run_test(start_test_server, test)