|
72 | 72 | | | `popup`:sup:`*†` | Show popup | |
73 | 73 | | +---------------------------+------------------------------------------------------------+ |
74 | 74 | | | `close_popup` | Close the current popup window. | |
| 75 | +| +---------------------------+------------------------------------------------------------+ |
| 76 | +| | `page` | Open a new page. | |
75 | 77 | +--------------------+---------------------------+------------------------------------------------------------+ |
76 | 78 | | Layout and Style | `put_row`:sup:`*†` | Use row layout to output content | |
77 | 79 | | +---------------------------+------------------------------------------------------------+ |
|
231 | 233 | 'put_table', 'put_buttons', 'put_image', 'put_file', 'PopupSize', 'popup', 'put_button', |
232 | 234 | 'close_popup', 'put_widget', 'put_collapse', 'put_link', 'put_scrollable', 'style', 'put_column', |
233 | 235 | 'put_row', 'put_grid', 'span', 'put_processbar', 'set_processbar', 'put_loading', |
234 | | - 'output', 'toast', 'get_scope', 'put_info', 'put_error', 'put_warning', 'put_success'] |
| 236 | + 'output', 'toast', 'get_scope', 'put_info', 'put_error', 'put_warning', 'put_success', 'page'] |
235 | 237 |
|
236 | 238 |
|
237 | 239 | # popup size |
@@ -1809,3 +1811,70 @@ async def coro_wrapper(*args, **kwargs): |
1809 | 1811 | return coro_wrapper |
1810 | 1812 | else: |
1811 | 1813 | return wrapper |
| 1814 | + |
| 1815 | + |
| 1816 | +def page(func=None): |
| 1817 | + """ |
| 1818 | + Open a page. Can be used as context manager and decorator. |
| 1819 | +
|
| 1820 | + :Usage: |
| 1821 | +
|
| 1822 | + :: |
| 1823 | +
|
| 1824 | + with page() as scope_name: |
| 1825 | + input() |
| 1826 | + put_xxx() |
| 1827 | +
|
| 1828 | + @page() # or @page |
| 1829 | + def content(): |
| 1830 | + input() |
| 1831 | + put_xxx() |
| 1832 | + """ |
| 1833 | + |
| 1834 | + if func is None: |
| 1835 | + return page_() |
| 1836 | + return page_()(func) |
| 1837 | + |
| 1838 | + |
| 1839 | +class page_: |
| 1840 | + page_id: str |
| 1841 | + |
| 1842 | + def __enter__(self): |
| 1843 | + self.page_id = random_str(10) |
| 1844 | + send_msg('open_page', dict(page_id=self.page_id)) |
| 1845 | + get_current_session().push_page(self.page_id) |
| 1846 | + |
| 1847 | + def __exit__(self, exc_type, exc_val, exc_tb): |
| 1848 | + """ |
| 1849 | + If this method returns True, it means that the context manager can handle the exception, |
| 1850 | + so that the with statement terminates the propagation of the exception |
| 1851 | + """ |
| 1852 | + get_current_session().pop_page() |
| 1853 | + send_msg('close_page', dict(page_id=self.page_id)) |
| 1854 | + |
| 1855 | + # todo: catch Page Close Exception |
| 1856 | + return False # Propagate Exception |
| 1857 | + |
| 1858 | + def __call__(self, func): |
| 1859 | + """decorator implement""" |
| 1860 | + |
| 1861 | + @wraps(func) |
| 1862 | + def wrapper(*args, **kwargs): |
| 1863 | + self.__enter__() |
| 1864 | + try: |
| 1865 | + return func(*args, **kwargs) |
| 1866 | + finally: |
| 1867 | + self.__exit__(None, None, None) |
| 1868 | + |
| 1869 | + @wraps(func) |
| 1870 | + async def coro_wrapper(*args, **kwargs): |
| 1871 | + self.__enter__() |
| 1872 | + try: |
| 1873 | + return await func(*args, **kwargs) |
| 1874 | + finally: |
| 1875 | + self.__exit__(None, None, None) |
| 1876 | + |
| 1877 | + if iscoroutinefunction(func): |
| 1878 | + return coro_wrapper |
| 1879 | + else: |
| 1880 | + return wrapper |
0 commit comments