输出小部件:富文本

输出小部件:富文本#

import ipywidgets as widgets

富文本输出也可以定向到输出区域。任何在 Jupyter 笔记本中显示良好的内容也会在 Output 部件中显示得很好。

from IPython.display import YouTubeVideo
with out:
    display(YouTubeVideo('eWzY2nGfkXk'))

甚至可以在输出部件中显示复杂的 mimetypes,例如嵌套的部件。

out = widgets.Output(layout={'border': '2px solid black'})
with out:
    display(widgets.IntSlider())
# out

可以通过在上下文管理器中使用 IPython.display.clear_output 来清除输出,或者我们可以直接调用部件的 clear_output 方法。

out = widgets.Output(layout={'border': '2px solid black'})
with out:
    display(widgets.IntSlider())
out.clear_output()
# out
import os
os.
environ{'SHELL': '/bin/bash',
        'LC_ADDRESS': 'zh_CN.UTF-8',
        'LC_NAME': 'zh_CN.UTF-8',
        'LC_MONETARY': 'zh_CN.UTF-8',
        'PWD': '/home/ai',
        'LOGNAME': 'ai',
        'XDG_SESSION_TYPE': 'tty',
        'MOTD_SHOWN': 'pam',
        'HOME': '/home/ai',
        'LC_PAPER': 'zh_CN.UTF-8',
        'LANG': 'zh_CN.UTF-8',
        'SSL_CERT_DIR': '/usr/lib/ssl/certs',
        'VSCODE_AGENT_FOLDER': '/home/ai/.vscode-server',
        'SSH_CONNECTION': '10.16.2.109 59882 10.16.11.3 22',
        'XDG_SESSION_CLASS': 'user',
        'LC_IDENTIFICATION': 'zh_CN.UTF-8',
        'USER': 'ai',
        'DISPLAY': 'localhost:12.0',
        'SHLVL': '1',
        'LC_TELEPHONE': 'zh_CN.UTF-8',
        'LC_MEASUREMENT': 'zh_CN.UTF-8',
        'XDG_SESSION_ID': '3042',
        'XDG_RUNTIME_DIR': '/run/user/1006',
        'SSL_CERT_FILE': '/usr/lib/ssl/certs/ca-certificates.crt',
        'SSH_CLIENT': '10.16.2.109 59882 22',
        'LC_TIME': 'zh_CN.UTF-8',
        'VSCODE_CLI_REQUIRE_TOKEN': 'a2d4eece-ad41-436b-9660-812271e7852e',
        'PATH': '/media/pc/data/lxw/envs/anaconda3x/envs/xxx/bin:/media/pc/data/tasks/.vscode-server/cli/servers/Stable-f1a4fb101478ce6ec82fe9627c43efbf9e98c813/server/bin/remote-cli:/opt/xm_toolchain/arm-xmv5-linux/usr/bin/:/opt/xm_toolchain/arm-xm-linux/usr/bin/:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/ai/.dotnet/tools:/media/pc/data/tasks/.vscode-server/cli/servers/Stable-f1a4fb101478ce6ec82fe9627c43efbf9e98c813/server/bin/remote-cli:/opt/xm_toolchain/arm-xmv5-linux/usr/bin/:/opt/xm_toolchain/arm-xm-linux/usr/bin/:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/ai/.dotnet/tools',
        'DBUS_SESSION_BUS_ADDRESS': 'unix:path=/run/user/1006/bus',
        'LC_NUMERIC': 'zh_CN.UTF-8',
        '_': '/media/pc/data/tasks/.vscode-server/cli/servers/Stable-f1a4fb101478ce6ec82fe9627c43efbf9e98c813/server/node',
        'VSCODE_CWD': '/home/ai',
        'VSCODE_NLS_CONFIG': '{"userLocale":"zh-cn","osLocale":"zh-cn","resolvedLanguage":"en","defaultMessagesFile":"/media/pc/data/tasks/.vscode-server/cli/servers/Stable-f1a4fb101478ce6ec82fe9627c43efbf9e98c813/server/out/nls.messages.json","locale":"zh-cn","availableLanguages":{}}',
        'VSCODE_HANDLES_SIGPIPE': 'true',
        'PKG_CONFIG_PATH': ':/usr/local/lib/pkgconfig',
        'XDG_DATA_DIRS': '/usr/share/gnome:/usr/local/share:/usr/share:/var/lib/snapd/desktop',
        'VSCODE_ESM_ENTRYPOINT': 'vs/workbench/api/node/extensionHostProcess',
        'VSCODE_HANDLES_UNCAUGHT_ERRORS': 'true',
        'BROWSER': '/media/pc/data/tasks/.vscode-server/cli/servers/Stable-f1a4fb101478ce6ec82fe9627c43efbf9e98c813/server/bin/helpers/browser.sh',
        'ELECTRON_RUN_AS_NODE': '1',
        'VSCODE_IPC_HOOK_CLI': '/run/user/1006/vscode-ipc-fe2f7cea-7f3e-4542-9bda-f72377914116.sock',
        'VSCODE_L10N_BUNDLE_LOCATION': 'vscode-local:/c%3A/Users/XMICUser/.vscode/extensions/ms-ceintl.vscode-language-pack-zh-hans-1.95.2024103009/translations/extensions/vscode.html-language-features.i18n.json',
        'CONDA_PREFIX': '/media/pc/data/lxw/envs/anaconda3x/envs/xxx',
        'PYDEVD_IPYTHON_COMPATIBLE_DEBUGGING': '1',
        'PYTHONUNBUFFERED': '1',
        'PYTHONIOENCODING': 'utf-8',
        'PYTHON_FROZEN_MODULES': 'on',
        'PYDEVD_USE_FRAME_EVAL': 'NO',
        'TERM': 'xterm-color',
        'CLICOLOR': '1',
        'FORCE_COLOR': '1',
        'CLICOLOR_FORCE': '1',
        'PAGER': 'cat',
        'GIT_PAGER': 'cat',
        'MPLBACKEND': 'module://matplotlib_inline.backend_inline'}