{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# `ui.link`\n", "\n", "## 链接\n", "\n", "创建超链接。\n", "\n", "要在页面内跳转到特定位置,您可以使用 `ui.link_target(\"name\")` 放置可链接锚点,并使用 `ui.link(target=\"#name\")` 进行链接。\n", "\n", "- `text`:显示文本\n", "- `target`:页面函数、同一页面上的 NiceGUI 元素或从基本 URL 开始的绝对 URL 或相对路径的字符串\n", "- `new_tab`:在新标签页中打开链接(默认:`False`)" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from nicegui import ui\n", "\n", "ui.link('NiceGUI on GitHub', 'https://github.com/zauberzeug/nicegui')\n", "\n", "# ui.run()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 在大型页面上导航\n", "\n", "要在页面内跳转到特定位置,您可以使用 `ui.link_target('target_name')` 放置可链接锚点,或者直接将 NiceGUI 元素作为链接目标传递。" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "from nicegui import ui\n", "\n", "navigation = ui.row()\n", "ui.link_target('target_A')\n", "ui.label(\n", " 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, '\n", " 'sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.'\n", ")\n", "label_B = ui.label(\n", " 'Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. '\n", " 'Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. '\n", " 'Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'\n", ")\n", "with navigation:\n", " ui.link('Goto A', '#target_A')\n", " ui.link('Goto B', label_B)\n", "\n", "# ui.run()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 链接到其他页面\n", "\n", "您可以通过将链接目标提供为路径或函数引用来链接到其他页面。" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from nicegui import ui\n", "\n", "@ui.page('/some_other_page')\n", "def my_page():\n", " ui.label('This is another page')\n", "\n", "ui.label('Go to other page')\n", "ui.link('... with path', '/some_other_page')\n", "ui.link('... with function reference', my_page)\n", "\n", "# ui.run()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 从图像和其他元素链接\n", "\n", "通过将元素嵌套在链接内部,您可以使整个元素可点击。这适用于所有元素,但对于非交互式元素(如 [`ui.image`](https://nicegui.io/documentation/image)、[`ui.avatar`](https://nicegui.io/documentation/image) 等)最为有用。" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "from nicegui import ui\n", "\n", "with ui.link(target='https://github.com/zauberzeug/nicegui'):\n", " ui.image('https://picsum.photos/id/41/640/360').classes('w-64')\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 }