13.misc.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import asyncio
  2. import subprocess
  3. from functools import partial
  4. from percy import percySnapshot
  5. from selenium.webdriver import Chrome
  6. import pywebio
  7. import template
  8. import util
  9. from pywebio import start_server
  10. from pywebio.output import *
  11. from pywebio.session import *
  12. from pywebio.utils import *
  13. def target():
  14. set_auto_scroll_bottom(False)
  15. # test session data
  16. g = data()
  17. assert g.none is None
  18. g.one = 1
  19. g.one += 1
  20. assert g.one == 2
  21. # test pywebio.utils
  22. async def corofunc(**kwargs):
  23. pass
  24. def genfunc(**kwargs):
  25. yield
  26. corofunc = partial(corofunc, a=1)
  27. genfunc = partial(genfunc, a=1)
  28. assert isgeneratorfunction(genfunc)
  29. assert iscoroutinefunction(corofunc)
  30. assert get_function_name(corofunc) == 'corofunc'
  31. get_free_port()
  32. # test pywebio.output
  33. set_output_fixed_height(False)
  34. try:
  35. yield put_buttons([{'label': 'must not be shown'}], onclick=[lambda: None])
  36. except Exception:
  37. pass
  38. # test deprecated api
  39. put_table([
  40. ['Idx', 'Actions'],
  41. ['1', table_cell_buttons(['edit', 'delete'], onclick=lambda _: None)],
  42. ])
  43. popup('title', 'text content')
  44. @popup('Popup title')
  45. def show_popup():
  46. put_html('<h3>Popup Content</h3>')
  47. put_text('html: <br/>')
  48. with popup('Popup title') as s:
  49. put_html('<h3>Popup Content</h3>')
  50. clear(s)
  51. put_buttons(['clear()'], onclick=lambda _: clear(s))
  52. popup('title2', 'text content')
  53. close_popup()
  54. with use_scope() as name:
  55. put_text('no show')
  56. remove(name)
  57. with use_scope('test') as name:
  58. put_text('current scope name:%s' % name)
  59. with use_scope('test', clear=True):
  60. put_text('clear previous scope content')
  61. @use_scope('test')
  62. def scoped_func(text):
  63. put_text(text)
  64. scoped_func('text1 from `scoped_func`')
  65. scoped_func('text2 from `scoped_func`')
  66. try:
  67. put_column([put_text('A'), 'error'])
  68. except Exception:
  69. pass
  70. toast('Awesome PyWebIO!!', duration=0)
  71. def show_msg():
  72. put_text("Toast clicked")
  73. toast('You have new messages', duration=0, onclick=show_msg)
  74. run_js("$('.toastify').eq(0).click()")
  75. yield hold()
  76. async def corobased():
  77. await wait_host_port(port=8080, host='127.0.0.1')
  78. await to_coroutine(target())
  79. def threadbased():
  80. run_as_function(target())
  81. def test(server_proc: subprocess.Popen, browser: Chrome):
  82. time.sleep(2)
  83. percySnapshot(browser=browser, name='misc output')
  84. coro_out = template.save_output(browser)[-1]
  85. browser.get('http://localhost:8080/?app=thread')
  86. time.sleep(2)
  87. thread_out = template.save_output(browser)[-1]
  88. assert coro_out == thread_out
  89. def start_test_server():
  90. pywebio.enable_debug()
  91. start_server({'coro': corobased, 'thread': threadbased}, port=8080, host='127.0.0.1', debug=True)
  92. if __name__ == '__main__':
  93. util.run_test(start_test_server, test, address='http://localhost:8080/?app=coro')