Skip to content

Commit 55251f7

Browse files
committed
更新文档
1 parent a5c3b07 commit 55251f7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+850
-2212
lines changed

doc/ui/nicegui/events/Async.ipynb

-73
This file was deleted.

doc/ui/nicegui/events/Async.md

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# NiceGUI 异步事件处理器
2+
3+
大多数元素也支持异步事件处理器。
4+
5+
注意:您还可以将 `functools.partial` 传递给 `on_click` 属性,以包装带参数的异步函数。
6+
7+
```python
8+
import asyncio
9+
from nicegui import ui
10+
11+
async def async_task():
12+
ui.notify('Asynchronous task started')
13+
await asyncio.sleep(5)
14+
ui.notify('Asynchronous task finished')
15+
16+
ui.button('start async task', on_click=async_task)
17+
18+
# ui.run()
19+
```

doc/ui/nicegui/events/Events.ipynb

-82
This file was deleted.

doc/ui/nicegui/events/Events.md

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# NiceGUI 事件
2+
3+
您可以注册协程或函数,以便在以下事件发生时被调用:
4+
5+
- `app.on_startup`:在 NiceGUI 启动或重启时调用
6+
- `app.on_shutdown`:在 NiceGUI 关闭或重启时调用
7+
- `app.on_connect`:每个连接的客户端都会调用(可选参数:`nicegui.Client`
8+
- `app.on_disconnect`:每个断开连接的客户端都会调用(可选参数:`nicegui.Client`
9+
- `app.on_exception`:发生异常时调用(可选参数:`exception`
10+
11+
当 NiceGUI 关闭或重启时,所有仍在执行的任务将自动取消。
12+
13+
```python
14+
from datetime import datetime
15+
from nicegui import app, ui
16+
17+
dt = datetime.now()
18+
19+
def handle_connection():
20+
global dt
21+
dt = datetime.now()
22+
app.on_connect(handle_connection)
23+
24+
label = ui.label()
25+
ui.timer(1, lambda: label.set_text(f'Last new connection: {dt:%H:%M:%S}'))
26+
27+
# ui.run()
28+
```

doc/ui/nicegui/events/Storage.ipynb

-120
This file was deleted.

doc/ui/nicegui/events/Storage.md

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
2+
# NiceGUI 存储
3+
4+
NiceGUI 为应用程序内的数据持久性提供了一种简便的方法。它具有三种内置存储类型:
5+
6+
- `app.storage.user`:存储在服务器端,每个字典都与浏览器会话cookie中的唯一标识符相关联。对于每个用户都是唯一的,这种存储可以在他们所有的浏览器标签页中访问。`app.storage.browser['id']` 用于识别用户。
7+
- `app.storage.general`:也存储在服务器端,这个字典提供了一个所有用户都可以访问的共享存储空间。
8+
- `app.storage.browser`:与前两种类型不同,这个字典直接作为浏览器会话cookie存储,在同一用户的所有浏览器标签页之间共享。然而,由于 `app.storage.user` 在减少数据负载、增强安全性和提供更大的存储容量方面的优势,通常更倾向于使用它。默认情况下,NiceGUI 在 `app.storage.browser['id']` 中保存浏览器会话的唯一标识符。
9+
10+
用户存储和浏览器存储仅在页面构建器函数 [pages](../pages/index) 中可用,因为它们正在访问 FastAPI 的底层请求对象。此外,这两种类型需要在 `ui.run()` 中的 `storage_secret` 参数来加密浏览器会话 cookie。
11+
12+
```python
13+
from nicegui import app, ui
14+
15+
@ui.page('/')
16+
def index():
17+
app.storage.user['count'] = app.storage.user.get('count', 0) + 1
18+
with ui.row():
19+
ui.label('your own page visits:')
20+
ui.label().bind_text_from(app.storage.user, 'count')
21+
22+
# ui.run(storage_secret='private key to secure the browser session cookie')
23+
```
24+
25+
## NiceGUI 统计页面访问次数
26+
27+
在这里,我们使用自动可用的浏览器存储的会话 ID 来计算唯一页面访问次数。
28+
29+
```python
30+
from collections import Counter
31+
from datetime import datetime
32+
from nicegui import app, ui
33+
34+
counter = Counter()
35+
start = datetime.now().strftime('%H:%M, %d %B %Y')
36+
37+
@ui.page('/')
38+
def index():
39+
counter[app.storage.browser['id']] += 1
40+
ui.label(f'{len(counter)} unique views ({sum(counter.values())} overall) since {start}')
41+
42+
# ui.run(storage_secret='private key to secure the browser session cookie')
43+
```
44+
45+
## NiceGUI 存储 UI 状态
46+
47+
存储也可以与[绑定](https://nicegui.io/documentation/bindings)结合使用。在这里,我们正在保存访问之间的文本区域值。同一用户的所有标签页之间也共享该便签。
48+
49+
```python
50+
from nicegui import app, ui
51+
52+
@ui.page('/')
53+
def index():
54+
ui.textarea('This note is kept between visits') \
55+
.classes('w-full').bind_value(app.storage.user, 'note')
56+
57+
# ui.run()
58+
```

0 commit comments

Comments
 (0)