{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 绑定属性\n", "\n", "NiceGUI 能够直接将 UI 元素绑定到模型。可以对 UI 元素的 `text`、`value` 或 `visibility` 属性以及(嵌套的)类属性进行绑定。每个元素都提供了如 `bind_value` 和 `bind_visibility` 之类的方法来创建与相应属性的双向绑定。要定义单向绑定,请使用这些方法的 `_from` 和 `_to` 变体。只需将这些方法的参数设置为模型的属性即可创建绑定。值将在立即更新,并在其中一个值更改时更新。" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from nicegui import ui\n", "\n", "class Demo:\n", " def __init__(self):\n", " self.number = 1\n", "\n", "demo = Demo()\n", "v = ui.checkbox('visible', value=True)\n", "with ui.column().bind_visibility_from(v, 'value'):\n", " ui.slider(min=1, max=3).bind_value(demo, 'number')\n", " ui.toggle({1: 'A', 2: 'B', 3: 'C'}).bind_value(demo, 'number')\n", " ui.number().bind_value(demo, 'number')\n", "\n", "# ui.run()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 绑定到字典\n", "\n", "在这里,我们将标签的文本绑定到一个字典。" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from nicegui import ui\n", "\n", "data = {'name': 'Bob', 'age': 17}\n", "\n", "ui.label().bind_text_from(data, 'name', backward=lambda n: f'Name: {n}')\n", "ui.label().bind_text_from(data, 'age', backward=lambda a: f'Age: {a}')\n", "\n", "ui.button('Turn 18', on_click=lambda: data.update(age=18))\n", "\n", "# ui.run()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 绑定到变量\n", "\n", "在这里,我们将 `datepicker` 的值绑定到普通变量。因此,我们使用了包含所有全局变量的字典 `globals()`。这个示例基于[官方的 `datepicker` 示例](https://nicegui.io/documentation/date#input_element_with_date_picker)。" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "from nicegui import ui\n", "\n", "date = '2023-01-01'\n", "\n", "with ui.input('Date').bind_value(globals(), 'date') as date_input:\n", " with ui.menu() as menu:\n", " ui.date(on_change=lambda: ui.notify(f'Date: {date}')).bind_value(date_input)\n", " with date_input.add_slot('append'):\n", " ui.icon('edit_calendar').on('click', menu.open).classes('cursor-pointer')\n", "\n", "# ui.run()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 绑定到存储\n", "\n", "绑定也可以与 `app.storage` 一起使用。在这里,我们在访问之间存储 `textarea` 的值。同一个用户的所有标签页之间也会共享这个笔记。" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "from nicegui import app, ui\n", "\n", "@ui.page('/')\n", "def index():\n", " ui.textarea('This note is kept between visits').classes('w-full').bind_value(app.storage.user, 'note')\n", "\n", "# ui.run()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "py311", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.7" } }, "nbformat": 4, "nbformat_minor": 2 }