PyWebIO之输出域scope用法(use_scope、put_scope、get_scope、clear、remove、scroll_to)

该文详细介绍了PyWebIO库中关于scope的使用,包括创建、清除、作为装饰器以及嵌套。同时展示了如何指定输出内容到特定scope,调整输出位置,以及使用scroll_to进行页面滚动。通过案例分析了不同场景下的scope管理和交互方式。
摘要由CSDN通过智能技术生成

函数清单

在这里插入图片描述

use_scope

case1:自定义scope和Root scope

使用start_server()启动一个applications,示例如下。
后续例子只展示application,需要老铁们start_server时手动更换要启动的applications名字。

  • app:use_scope_case1()
  • port:19003(此端口需要没有被占用才行)。使用端口号 start_server的好处是url固定,share给别人使用很方便。如果不指定port每次运行都会随机一个。
  • auto_open_webbrowser:为True时,执行代码后会自动帮你打开浏览器。
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)

效果:

  • get_scope()获取当前运行时scope栈中的scope名。
  • stack_idx (int) – 当前运行时的scope栈索引,默认值为-1。-1表示当前scope,-2表示进入当前scope前的scope,依次类推;0表示 ROOT scope
    在这里插入图片描述

case2:scope的clear参数

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')

效果:
scope1输入了两次内容,但最终只显示了text in scope1,因为在第2次输入内容时,将scope1中的内容清空了。
在这里插入图片描述

case3:use_scope作为装饰器

# use_scope() 还可以作为装饰器来使用
@use_scope('time', clear=True)
def use_scope_case3():
    # 每次刷新网页就会更新时间
    put_text(datetime.now())

效果:
在这里插入图片描述

case4:scope嵌套

# 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')

效果:
在这里插入图片描述

case5:remove scope

注:不能remove ‘ROOT’ scope,否则会报AssertionError
在这里插入图片描述remove函数,scope参数默认为当前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')

效果:
remove的scope如果有嵌套关系,那么此scope嵌套内的scope也将remove掉。例如,当remove scope1时,也将remove掉scope2。当只remove scope2时,scope1将不会受影响。
注:当在某个scope下执行remove函数,那么不管scope name填写的是哪个,都会默认remove当前所在的scope。
在这里插入图片描述

get_scope

见use_scope中的case1

输出域scope

case1:输出内容指定scope

  • 输出函数默认将内容输出到“当前scope”。
  • 所有输入函数都支持使用 scope 参数来指定输出的目的scope
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:输出内容指定position

  • 一个scope可以包含多个输出项,输出函数的默认行为是将内容追加到目标scope中
  • 可以使用输出函数的 position 参数来指定输出内容在目标scope中的插入位置
  • position 参数类型为整形, position>=0 时表示输出内容到目标Scope的第position号元素的前面; position<0 时表示输出内容到目标Scope第position号元素之后
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

效果:
在这里插入图片描述

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)

效果:
在这里插入图片描述

scroll_to

将页面滚动到 scope处,并且将scope置于屏幕可视区域的位置。

  • ‘top’ : 滚动页面,让Scope位于屏幕可视区域顶部
  • ‘middle’ : 滚动页面,让Scope位于屏幕可视区域中间
  • ‘bottom’ : 滚动页面,让Scope位于屏幕可视区域底部
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')

效果:
以下效果是代码自己滚动的。
在这里插入图片描述

所有代码

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)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Summer@123

不积跬步无以至千里,感谢支持!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值