{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# `ui.chat_message`\n", "\n", "## 聊天消息\n", "\n", "基于 [Quasar 的聊天消息](https://quasar.dev/vue-components/chat/)组件。\n", "\n", "- `text`:消息正文(可以是多个消息部分的字符串列表)\n", "- `name`:消息作者的名称\n", "- `label`:仅渲染标签头/部分\n", "- `stamp`:消息的时间戳\n", "- `avatar`:头像的URL\n", "- `sent`:将消息渲染为已发送的消息(即来自当前用户)(默认:`False`)\n", "- `text_html`:将文本渲染为HTML(默认:`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.chat_message('Hello NiceGUI!',\n", " name='Robot',\n", " stamp='now',\n", " avatar='https://robohash.org/ui')\n", "\n", "# ui.run()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## HTML文本\n", "\n", "使用 `text_html` 参数,您可以向聊天发送HTML文本。" ] }, { "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", "ui.chat_message('Without HTML')\n", "ui.chat_message('With HTML', text_html=True)\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.chat_message('This is a\\nlong line!')\n", "\n", "# ui.run()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 多部分消息\n", "\n", "您可以通过传递字符串列表来发送多个消息部分。" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from nicegui import ui\n", "\n", "ui.chat_message(['Hi! 😀', 'How are you?']\n", " )\n", "\n", "# ui.run()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 带有子元素的聊天消息\n", "\n", "您可以向聊天消息添加子元素。" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "from nicegui import ui\n", "\n", "with ui.chat_message():\n", " ui.label('Guess where I am!')\n", " ui.image('https://picsum.photos/id/249/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 }