function list
use_scope
case1: Custom scope and Root scope
Use start_server() to start an applications, the example is as follows.
Subsequent examples only show the application, and veterans need to manually change the name of the applications to be started when they start_server.
- app:use_scope_case1()
- port: 19003 (this port needs to be unoccupied). The advantage of using the port number start_server is that the url is fixed, and it is very convenient to share it for others to use. If you do not specify a port, one will be randomly selected every time you run it.
- auto_open_webbrowser: When it is True, it will automatically open the browser for you after executing the code.
def use_scope_case1():
with use_scope('scope1'): # 创建并进入scope 'scope1'
put_text(f'text1 in scope1') # 输出内容到 scope1
put_text(f"text1:{
get_scope()}")
put_text('text3 in parent scope of scope1') # 输出内容到 ROOT scope
put_text(f"text3:{
get_scope()}")
with use_scope('scope1'): # 进入之前创建的scope 'scope1'
put_text('text2 in scope1') # 输出内容到 scope1
put_text(f"text2:{
get_scope()}")
if __name__ == '__main__':
start_server(applications=use_scope_case1, port=19003, auto_open_webbrowser=False)
Effect:
- get_scope() gets the scope name in the current runtime scope stack.
- stack_idx (int) – The scope stack index of the current runtime, the default value is -1. -1 indicates the current scope, -2 indicates the scope before entering the current scope, and so on; 0 indicates the ROOT scope
case2: clear parameter of scope
def use_scope_case2():
with use_scope('scope1'):
put_text('create scope1')
put_text('text in parent scope of scope1')
# use_scope() 还可以使用 clear 参数将scope中原有的内容清空:
with use_scope('scope1', clear=True): # enter the existing scope and clear the previous content
put_text('text in scope1')
Effect:
scope1 enters content twice, but only text in scope1 is displayed in the end, because the content in scope1 is cleared when the content is entered for the second time.
case3: use_scope as a decorator
# use_scope() 还可以作为装饰器来使用
@use_scope('time', clear=True)
def use_scope_case3():
# 每次刷新网页就会更新时间
put_text(datetime.now())
Effect:
case4: scope nesting
# scope嵌套
def use_scope_case4():
# 会话开始时,PyWebIO应用只有一个 ROOT scope。
# 在root scope中创建scope A
with use_scope('A'):
put_text('Text in scope A')
# 在scope A中创建scope B
with use_scope('B'):
put_text('Text in scope B')
# 在root scope中创建scope C,和A同级且显示在A下方
with use_scope('C'):
put_text('Text in scope C')
Effect:
case5:remove scope
Note: The 'ROOT' scope cannot be removed, otherwise an AssertionError will be reported for
the remove function, and the scope parameter defaults to the current scope name.
def use_scope_case5():
with use_scope('scope1'):
put_text('A')
with use_scope('scope2'):
put_text('B')
remove('scope2')
put_text('C')
Effect:
If the scope of remove has a nested relationship, then the scopes within the nested scope will also be removed. For example, when scope1 is removed, scope2 will also be removed. When only scope2 is removed, scope1 will not be affected.
Note: When the remove function is executed under a certain scope, no matter which scope name is filled in, the current scope of remove will be removed by default.
get_scope
See case1 in use_scope
output domain scope
case1: the output content specifies the scope
- The output function outputs the content to the "current scope" by default.
- All input functions support the use of scope parameters to specify the destination scope of the output
def put_scope_case1():
# 所有输入函数都支持使用scope参数来指定输出的目的scope
with use_scope('scope3'):
put_text('text1 in scope3') # output to current scope: scope3
put_text('text in ROOT scope', scope='ROOT') # output to ROOT Scope
put_text('text2 in scope3', scope='scope3') # output to scope3
case2: output content specified position
- A scope can contain multiple output items, the default behavior of the output function is to append the content to the target scope
- You can use the position parameter of the output function to specify the insertion position of the output content in the target scope
- The type of the position parameter is integer. When position>=0, it means that the output content is in front of the position No. element of the target Scope; when position<0, it means that the output content is after the position No. element of the target Scope.
def use_scope_case6():
with use_scope('scope1'):
put_text('A')
put_text('B', position=0) # insert B before A -> B A
put_text('C', position=-2) # insert C after B -> B C A
put_text('D', position=1) # insert D before C B -> B D C A
Effect:
case3
def put_scope_case3():
# 表头是:Name和Hobbies
# 第一行数据,name列展示Tom,Hobbies列使用put_scope显示创建了scope 'hobby',且输出内容是Coding
put_table([
['Name', 'Hobbies'],
['Tom', put_scope('hobby', content=put_text('Coding'))] # hobby is initialized to coding
])
# 进入hobby容器之前,将内容清空,然后输出Movie
with use_scope('hobby', clear=True):
put_text('Movie') # hobby is reset to Movie
# 没有加clear=True, 因此是在hobby容器的Movie后追加Music和Drama
with use_scope('hobby'):
put_text('Music')
put_text('Drama')
# 容器中的内容有对应的position, 可以通过指定position进行insert
# insert the Coding into the top of the hobby
put_markdown('**Coding**', scope='hobby', position=0)
Effect:
scroll_to
Scroll the page to the scope, and place the scope in the visible area of the screen.
- 'top' : Scroll the page so that the Scope is at the top of the visible area of the screen
- 'middle' : Scroll the page so that the Scope is in the middle of the visible area of the screen
- 'bottom' : Scroll the page so that the Scope is at the bottom of the visible area of the screen
def scroll_to_case():
with use_scope("scope1"):
put_markdown(r""" # scope1
scope1:第一行
scope1
scope1
scope1
scope1
scope1
scope1
scope1
scope1
scope1:最后一行
""")
with use_scope("scope2"):
put_markdown(r""" # scope2
scope2:第一行
scope2
scope2
scope2
scope2
scope2
scope2
scope2
scope2
scope2:最后一行
""")
with use_scope("scope3"):
put_markdown(r""" # scope3
scope3: 第一行
scope3
scope3
scope3
scope3
scope3
scope3
scope3
scope3
scope3:最后一行
""")
scroll_to(scope='scope3', position='bottom')
sleep(2)
scroll_to(scope='scope2', position='middle')
sleep(2)
scroll_to(scope='scope1', position='top')
Effect:
The following effect is the code rolling by itself.
all codes
from time import sleep
from pywebio import start_server
from pywebio.output import *
from datetime import datetime
def use_scope_case1():
with use_scope('scope1'): # 创建并进入scope 'scope1'
put_text(f'text1 in scope1') # 输出内容到 scope1
put_text(f"text1:{
get_scope()}")
put_text('text3 in parent scope of scope1') # 输出内容到 ROOT scope
put_text(f"text3:{
get_scope()}")
with use_scope('scope1'): # 进入之前创建的scope 'scope1'
put_text('text2 in scope1') # 输出内容到 scope1
put_text(f"text2:{
get_scope()}")
def use_scope_case2():
with use_scope('scope1'):
put_text('create scope1')
put_text('text in parent scope of scope1')
# use_scope() 还可以使用 clear 参数将scope中原有的内容清空:
with use_scope('scope1', clear=True): # enter the existing scope and clear the previous content
put_text('text in scope1')
# use_scope() 还可以作为装饰器来使用
@use_scope('time', clear=True)
def use_scope_case3():
# 每次刷新网页就会更新时间
put_text(datetime.now())
# scope嵌套
def use_scope_case4():
# 会话开始时,PyWebIO应用只有一个 ROOT scope。
# 在root scope中创建scope A
with use_scope('A'):
put_text('Text in scope A')
# 在scope A中创建scope B
with use_scope('B'):
put_text('Text in scope B')
# 在root scope中创建scope C,和A同级且显示在A下方
with use_scope('C'):
put_text('Text in scope C')
def use_scope_case5():
with use_scope('scope1'):
put_text('A')
with use_scope('scope2'):
put_text('B')
remove('scope2')
put_text('C')
def put_scope_case1():
# 所有输入函数都支持使用scope参数来指定输出的目的scope
with use_scope('scope3'):
put_text('text1 in scope3') # output to current scope: scope3
put_text('text in ROOT scope', scope='ROOT') # output to ROOT Scope
put_text('text2 in scope3', scope='scope3') # output to scope3
def put_scope_case2():
with use_scope('scope1'):
put_text('A')
put_text('B', position=0) # insert B before A -> B A
put_text('C', position=-2) # insert C after B -> B C A
put_text('D', position=1) # insert D before C B -> B D C A
def put_scope_case3():
# 表头是:Name和Hobbies
# 第一行数据,name列展示Tom,Hobbies列使用put_scope显示创建了scope 'hobby',且输出内容是Coding
put_table([
['Name', 'Hobbies'],
['Tom', put_scope('hobby', content=put_text('Coding'))] # hobby is initialized to coding
])
# 进入hobby容器之前,将内容清空,然后输出Movie
with use_scope('hobby', clear=True):
put_text('Movie') # hobby is reset to Movie
# 没有加clear=True, 因此是在hobby容器的Movie后追加Music和Drama
with use_scope('hobby'):
put_text('Music')
put_text('Drama')
# 容器中的内容有对应的position, 可以通过指定position进行insert
# insert the Coding into the top of the hobby
put_markdown('**Coding**', scope='hobby', position=0)
def scroll_to_case():
with use_scope("scope1"):
put_markdown(r""" # scope1
scope1:第一行
scope1
scope1
scope1
scope1
scope1
scope1
scope1
scope1
scope1:最后一行
""")
with use_scope("scope2"):
put_markdown(r""" # scope2
scope2:第一行
scope2
scope2
scope2
scope2
scope2
scope2
scope2
scope2
scope2:最后一行
""")
with use_scope("scope3"):
put_markdown(r""" # scope3
scope3: 第一行
scope3
scope3
scope3
scope3
scope3
scope3
scope3
scope3
scope3:最后一行
""")
scroll_to(scope='scope3', position='bottom')
sleep(2)
scroll_to(scope='scope2', position='middle')
sleep(2)
scroll_to(scope='scope1', position='top')
if __name__ == '__main__':
start_server(scroll_to_case, port=19003, auto_open_webbrowser=False)