{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 添加媒体文件目录\n", "\n", "`add_media_files()` 允许从指定的端点(例如 `'/media'`)流式传输本地文件。这应用于支持适当流式传输的媒体文件。否则,浏览器将无法访问和增量加载文件,或者在流中跳转到不同位置。请确保只放置非安全关键文件,因为它们可以被任何人访问。\n", "\n", "要使单个文件可通过流式传输访问,您可以使用 `add_media_file()`。对于小型静态文件,可以使用 `add_static_files()` 或 `add_static_file()`。\n", "\n", "- `url_path`:以斜杠 `\"/\"` 开头的字符串,标识应提供服务的文件路径\n", "- `local_directory`:包含作为媒体内容提供服务的本地文件夹" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import requests\n", "from nicegui import app, ui\n", "from pathlib import Path\n", "\n", "media = Path('media')\n", "media.mkdir(exist_ok=True)\n", "r = requests.get('https://cdn.coverr.co/videos/coverr-cloudy-sky-2765/1080p.mp4')\n", "(media / 'clouds.mp4').write_bytes(r.content)\n", "app.add_media_files('/my_videos', media)\n", "ui.video('/my_videos/clouds.mp4')\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 }