pywebio_battery
— PyWebIO battery¶
Utilities that help write PyWebIO apps quickly and easily.
注解
pywebio_battery
is an extension package of PyWebIO, you must install it before using it.
To install this package, run pip3 install -U pywebio-battery
Functions list¶
Interaction related¶
Function name |
Description |
Confirmation modal |
|
Show a form in popup window |
|
redirecting stdout to pywebio |
|
Run command in shell |
|
Logbox widget |
|
Output video |
|
Output audio |
Web application related¶
Function name |
Description |
Get URL parameter |
|
User browser storage |
|
Web Cookie |
|
Authentication |
-
pywebio_battery.
confirm
(title: str, content: Optional[Union[str, pywebio.io_ctrl.Output, Sequence[Union[str, pywebio.io_ctrl.Output]]]] = None, *, timeout: Optional[int] = None) → Optional[bool][源代码]¶ Show a confirmation modal.
- 参数
title (str) – Model title.
content (list/put_xxx()/str) – The content of the confirmation modal. Can be a string, the put_xxx() calls , or a list of them.
timeout (None/float) – Seconds for operation time out.
- 返回
Return
True
when the “CONFIRM” button is clicked, returnFalse
when the “CANCEL” button is clicked, returnNone
when a timeout is given and the operation times out.
choice = confirm("Delete File", "Are you sure to delete this file?") put_text("Your choice", choice)
-
pywebio_battery.
popup_input
(pins: Sequence[pywebio.io_ctrl.Output], title='Please fill out the form below') → Optional[dict][源代码]¶ Show a form in popup window.
- 参数
pins (list) – pin widget list. It can also contain ordinary output widgets.
title (str) – model title.
- 返回
return the form value as dict, return
None
when user cancel the form.
form = popup_input([ put_input("username", label="User name"), put_input("password", type=PASSWORD, label="Password"), put_info("If you forget your password, please contact the administrator."), ], "Login") put_text("Login info:", form)
-
pywebio_battery.
redirect_stdout
(output_func=functools.partial(<function put_text>, inline=True))[源代码]¶ Context manager for temporarily redirecting stdout to pywebio.
with redirect_stdout(): print("Hello world.")
-
pywebio_battery.
run_shell
(cmd: str, output_func=functools.partial(<function put_text>, inline=True), encoding='utf8') → int[源代码]¶ Run command in shell and output the result to pywebio
- 参数
cmd (str) – command to run
output_func (callable) – output function, default to
put_text()
. the function should accept one argument, the output text of command.encoding (str) – command output encoding
- 返回
shell command return code
在 0.4 版更改: add
encoding
parameter and return code
-
pywebio_battery.
put_logbox
(name: str, height=400, keep_bottom=True) → pywebio.io_ctrl.Output[源代码]¶ Output a logbox widget
import time put_logbox("log") while True: logbox_append("log", f"{time.time()}\n") time.sleep(0.2)
- 参数
name (str) – the name of the widget, must unique in session-wide.
height (int) – the height of the widget in pixel
keep_bottom (bool) – Whether to scroll to bottom when new content is appended (via
logbox_append()
).
在 0.3 版更改: add
keep_bottom
parameter
-
pywebio_battery.
put_video
(src: Union[str, bytes], autoplay: bool = False, loop: bool = False, height: Optional[int] = None, width: Optional[int] = None, muted: bool = False, poster: Optional[str] = None, scope: Optional[str] = None, position: int = - 1) → pywebio.io_ctrl.Output[源代码]¶ Output video
- 参数
src (str/bytes) – Source of video. It can be a string specifying video URL, a bytes-like object specifying the binary content of the video.
autoplay (bool) – Whether to autoplay the video. In some browsers (e.g. Chrome 70.0) autoplay doesn’t work if not enable
muted
.loop (bool) – If True, the browser will automatically seek back to the start upon reaching the end of the video.
width (int) – The width of the video’s display area, in CSS pixels. If not specified, the intrinsic width of the video is used.
height (int) – The height of the video’s display area, in CSS pixels. If not specified, the intrinsic height of the video is used.
muted (bool) – If set, the audio will be initially silenced.
poster (str) – A URL for an image to be shown while the video is downloading. If this attribute isn’t specified, nothing is displayed until the first frame is available, then the first frame is shown as the poster frame.
scope, position (int) – Those arguments have the same meaning as for
put_text()
Example:
url = "https://interactive-examples.mdn.mozilla.net/media/cc0-videos/flower.mp4" put_video(url)
0.4 新版功能.
-
pywebio_battery.
put_audio
(src: Union[str, bytes], autoplay: bool = False, loop: bool = False, muted: bool = False, scope: Optional[str] = None, position: int = - 1) → pywebio.io_ctrl.Output[源代码]¶ Output audio
- 参数
src (str/bytes) – Source of audio. It can be a string specifying video URL, a bytes-like object specifying the binary content of the audio.
autoplay (bool) – Whether to autoplay the audio.
loop (bool) – If True, the browser will automatically seek back to the start upon reaching the end of the audio.
muted (bool) – If set, the audio will be initially silenced.
scope – The scope of the video. It can be
"session"
or"page"
. If not specified, the video will be automatically removed when the session is closed.scope, position (int) – Those arguments have the same meaning as for
put_text()
Example:
url = "https://interactive-examples.mdn.mozilla.net/media/cc0-audio/t-rex-roar.mp3" put_audio(url)
0.4 新版功能.
-
pywebio_battery.
get_all_query
()[源代码]¶ Get URL parameter (also known as “query strings” or “URL query parameters”) as a dict
-
pywebio_battery.
set_localstorage
(key: str, value: str)[源代码]¶ Save data to user’s web browser
The data is specific to the origin (protocol+domain+port) of the app. Different origins use different web browser local storage.
- 参数
key – the key you want to create/update.
value – the value you want to give the key you are creating/updating.
You can read the value by using
get_localstorage(key)
-
pywebio_battery.
get_localstorage
(key: str) → str[源代码]¶ Get the key’s value in user’s web browser local storage
-
pywebio_battery.
basic_auth
(verify_func: Callable[[str, str], bool], secret: Union[str, bytes], expire_days=7, token_name='pywebio_auth_token') → str[源代码]¶ Persistence authentication with username and password.
You need to provide a function to verify the current user based on username and password. The
basic_auth()
function will save the authentication state in the user’s web browser, so that the authed user does not need to log in again.- 参数
verify_func (callable) – User authentication function. It should receive two arguments: username and password. If the authentication is successful, it should return
True
, otherwise returnFalse
.secret (str) – HMAC secret for the signature. It should be a long, random str.
expire_days (int) – how many days the auth state can keep valid. After this time, authed users need to log in again.
token_name (str) – the name of the token to store the auth state in user browser.
- Return str
username of the current authed user
Example:
user_name = basic_auth(lambda username, password: username == 'admin' and password == '123', secret="__TODO:_GENERATE_YOUR_OWN_RANDOM_VALUE_HERE__") put_text("Hello, %s. You can refresh this page and see what happen" % user_name)
0.4 新版功能.
-
pywebio_battery.
custom_auth
(login_func: Callable[], str], secret=typing.Union[str, bytes], expire_days=7, token_name='pywebio_auth_token') → str[源代码]¶ Persistence authentication with custom logic.
You need to provide a function to determine the current user and return the username. The
custom_auth()
function will save the authentication state in the user’s web browser, so that the authed user does not need to log in again.- 参数
login_func (callable) – User login function. It should receive no arguments and return the username of the current user. If fail to verify the current user, it should return
None
.secret (str) – HMAC secret for the signature. It should be a long, random str.
expire_days (int) – how many days the auth state can keep valid. After this time,authed users need to log in again.
token_name (str) – the name of the token to store the auth state in user browser.
- Return str
username of the current authed user.
0.4 新版功能.