{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# UI 更新\n", "\n", "NiceGUI 尝试自动将 UI 元素的状态与客户端同步,例如当标签文本、输入值或元素的样式/类/属性发生更改时。在其他情况下,您可以显式调用` element.update()` 或 `ui.update(*elements)` 进行更新。演示代码展示了针对 `ui.echart` 的两种方法,其中很难自动检测选项字典中的变化。" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from nicegui import ui\n", "from random import random\n", "\n", "chart = ui.echart({\n", " 'xAxis': {'type': 'value'},\n", " 'yAxis': {'type': 'value'},\n", " 'series': [{'type': 'line', 'data': [[0, 0], [1, 1]]}],\n", "})\n", "\n", "def add():\n", " chart.options['series'][0]['data'].append([random(), random()])\n", " chart.update()\n", "\n", "def clear():\n", " chart.options['series'][0]['data'].clear()\n", " ui.update(chart)\n", "\n", "with ui.row():\n", " ui.button('Add', on_click=add)\n", " ui.button('Clear', on_click=clear)\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 }