Skip to content

文本元素

文本 Label

显示一些文本。

参数 Param说明 Description
text文本的内容
python
from nicegui import ui

ui.label('你好世界')

查看更多...

创建超链接。

要跳转到页面内的特定位置,您可以使用 ui.link_target("name") 方法放置锚点,然后使用 ui.link(target="#name") 方法。

参数 Param说明 Description
text显示的文本
targetpage 函数,同一页面上的 NiceGUI 元素或字符串,该字符串是一个绝对 URL 或相对于根 URL 的相对路径
new_tab在新标签页中打开 (默认值: False)
python
from nicegui import ui

ui.link('Github 上的 NiceGUI', 'https://github.com/zauberzeug/nicegui')

ui.run()

查看更多...

聊天消息 Chat Message

基于 Quasar 的 Chat Message↗ 组件。

参数 Param说明 Description
text消息内容(可以是一个列表的多条消息)
name消息发送者名称
label仅呈现标签标题/部分
stamp消息的时间戳
avatar头像 URL
sent是否为消息发送者(默认值:False)
text_html是否以 HTML 渲染消息(默认值:False)
python
from nicegui import ui

ui.chat_message('Hello NiceGUI!',
                name='Robot',
                stamp='now',
                avatar='https://robohash.org/ui')

ui.run()

查看更多...

通用元素

这个类是所有其他UI元素的基类,但是您可以使用它来创建带有任意HTML标记的元素。

参数 Param说明 Description
tag元素的 HTML 标签
_client此元素的客户端(仅供内部测试)
python
from nicegui import ui

with ui.element('div').classes('p-2 bg-blue-100'):
    ui.label('inside a colored div')

ui.run()

查看更多...

Markdown 元素 Markdown Element

在页面上渲染 Markdown。

参数 Param说明 Description
contentMarkdown 内容
extras参考 Markdown2 extensions (默认值:(default: ['fenced-code-blocks', 'tables']))
python
from nicegui import ui

ui.markdown('这是一段 **Markdown**.')

ui.run()

带缩进的Markdown

查看更多...

reST 文本 ReStructuredText

在页面上渲染 ReStructuredText。

参数 Param说明 Description
contentReStructuredText 内容
python
from nicegui import ui

ui.restructured_text('这是一段 **reStructuredText**.')

ui.run()

这是一段 reStructuredText.

查看更多...

美人鱼图 Mermaid Diagrams

将采用基于 Markdown 语法扩展的 Mermaid 语言编写的图表进行可视化渲染。 通过在 ui.markdown 元素中添加扩展字符串 'mermaid',可将mermaid语法嵌入Markdown 元素中使用。

可选配置字典会在首个图表渲染前直接传递给mermaid。 该参数可用于设置以下选项:

  • 允许在节点被点击时执行JavaScript代码: {'securityLevel': 'loose', ...}
  • 在控制台输出信息级日志: {'logLevel': 'info', ...}

有关完整配置选项列表,请参阅 Mermaid 文档中的 mermaid.initialize() 方法说明。

参数 Param说明 Description
contentMermaid 内容
config传递给 mermaid.initialize() 的配置字典
python
from nicegui import ui

ui.mermaid('''
graph LR;
    A --> B;
    A --> C;
''')

ui.run()

查看更多...

HTML 元素 HTML Element

将任意 HTML 内容渲染到页面,并包裹在指定标签中,并可使用 Tailwind 进行样式设计。

还可以使用 ui.add_head_html 将 HTML 代码添加到文档头部,使用 ui.add_body_html 将其添加到正文部分。

python
from nicegui import ui

ui.html('这是一段 <strong>HTML</strong>.')

ui.run()

查看更多...

其他 HTML 元素 Other HTML Elements ^2.5.0

还存在一个html模块,允许您插入其他HTML元素如<span><div><p>等。 其功能等同于使用带 tag 参数的 ui.element 方法。

与任何其他元素一样,您可以添加类名、样式、属性、工具提示和事件。 其中一项便利之处在于,关键字参数会自动添加到元素的 props 字典中。

python
from nicegui import html, ui

with html.section().style('font-size: 120%'):
    html.strong('一段粗体') \
        .classes('cursor-pointer') \
        .on('click', lambda: ui.notify('粗体!'))
    html.hr()
    html.em('一段斜体').tooltip('真棒!')
    with ui.row():
        html.img().props('src=https://placehold.co/60')
        html.img(src='https://placehold.co/60')

ui.run()
最近更新

更新日期: 2025 年 12 月 17 日