13.misc.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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_env(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. try:
  33. yield put_buttons([{'label': 'must not be shown'}], onclick=[lambda: None])
  34. except Exception:
  35. pass
  36. # test deprecated api
  37. put_table([
  38. ['Idx', 'Actions'],
  39. ['1', table_cell_buttons(['edit', 'delete'], onclick=lambda _: None)],
  40. ])
  41. popup('title', 'text content')
  42. @popup('Popup title')
  43. def show_popup():
  44. put_html('<h3>Popup Content</h3>')
  45. put_text('html: <br/>')
  46. with popup('Popup title') as s:
  47. put_html('<h3>Popup Content</h3>')
  48. clear(s)
  49. put_buttons(['clear()'], onclick=lambda _: clear(s))
  50. popup('title2', 'text content')
  51. close_popup()
  52. with use_scope() as name:
  53. put_text('no show')
  54. remove(name)
  55. with use_scope('test') as name:
  56. put_text('current scope name:%s' % name)
  57. with use_scope('test', clear=True):
  58. put_text('clear previous scope content')
  59. @use_scope('test')
  60. def scoped_func(text):
  61. put_text(text)
  62. scoped_func('text1 from `scoped_func`')
  63. scoped_func('text2 from `scoped_func`')
  64. try:
  65. put_column([put_text('A'), 'error'])
  66. except Exception:
  67. pass
  68. toast('Awesome PyWebIO!!', duration=0)
  69. def show_msg():
  70. put_text("Toast clicked")
  71. toast('You have new messages', duration=0, onclick=show_msg)
  72. run_js("$('.toastify').eq(0).click()")
  73. assert get_scope() == 'ROOT'
  74. with use_scope('go_app'):
  75. put_buttons(['Go thread App'], [lambda: go_app('thread', new_window=False)])
  76. yield hold()
  77. async def corobased():
  78. await wait_host_port(port=8080, host='127.0.0.1')
  79. await to_coroutine(target())
  80. def threadbased():
  81. run_as_function(target())
  82. def test(server_proc: subprocess.Popen, browser: Chrome):
  83. time.sleep(2)
  84. percySnapshot(browser=browser, name='misc output')
  85. coro_out = template.save_output(browser)[-1]
  86. # browser.get('http://localhost:8080/?app=thread')
  87. browser.execute_script("arguments[0].click();", browser.find_element_by_css_selector('#pywebio-scope-go_app button'))
  88. time.sleep(2)
  89. thread_out = template.save_output(browser)[-1]
  90. assert coro_out == thread_out
  91. def start_test_server():
  92. pywebio.enable_debug()
  93. start_server({'coro': corobased, 'thread': threadbased}, port=8080, host='127.0.0.1', debug=True)
  94. if __name__ == '__main__':
  95. util.run_test(start_test_server, test, address='http://localhost:8080/?app=coro')