{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 添加静态文件目录\n", "\n", "`add_static_files()` 使本地目录在指定的端点(例如 `'/static'`)可用。这对于向前端提供本地数据(如图像)非常有用。否则,浏览器将无法访问这些文件。请确保只放置非安全关键文件,因为它们可以被任何人访问。\n", "\n", "要使单个文件可访问,您可以使用 `add_static_file()`。对于应该流式传输的媒体文件,可以使用 `add_media_files()` 或 a`dd_media_file()`。\n", "\n", "- `url_path`:以斜杠 `\"/\"` 开头的字符串,标识应提供服务的文件路径\n", "- `local_directory`:包含作为静态内容提供服务的本地文件夹\n", "- `follow_symlin`k:是否遵循符号链接(默认:`False`)\n", "\n", "```python\n", "from nicegui import app, ui\n", "\n", "app.add_static_files('/examples', 'examples')\n", "ui.label('Some NiceGUI Examples').classes('text-h5')\n", "ui.link('AI interface', '/examples/ai_interface/main.py')\n", "ui.link('Custom FastAPI app', '/examples/fastapi/main.py')\n", "ui.link('Authentication', '/examples/authentication/main.py')\n", "\n", "ui.run()\n", "```" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "ename": "RuntimeError", "evalue": "Directory 'examples' does not exist", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mRuntimeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[1;32mIn[1], line 3\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mnicegui\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m app, ui\n\u001b[1;32m----> 3\u001b[0m \u001b[43mapp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43madd_static_files\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43m/examples\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mexamples\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[0;32m 4\u001b[0m ui\u001b[38;5;241m.\u001b[39mlabel(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mSome NiceGUI Examples\u001b[39m\u001b[38;5;124m'\u001b[39m)\u001b[38;5;241m.\u001b[39mclasses(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mtext-h5\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[0;32m 5\u001b[0m ui\u001b[38;5;241m.\u001b[39mlink(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mAI interface\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124m/examples/ai_interface/main.py\u001b[39m\u001b[38;5;124m'\u001b[39m)\n", "File \u001b[1;32mc:\\Users\\lxw\\.conda\\envs\\py311\\Lib\\site-packages\\nicegui\\app\\app.py:157\u001b[0m, in \u001b[0;36mApp.add_static_files\u001b[1;34m(self, url_path, local_directory, follow_symlink)\u001b[0m\n\u001b[0;32m 154\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m url_path \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124m/\u001b[39m\u001b[38;5;124m'\u001b[39m:\n\u001b[0;32m 155\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124m'''\u001b[39m\u001b[38;5;124mPath cannot be \u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m/\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m, because it would hide NiceGUI\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124ms internal \u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m/_nicegui\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m route.\u001b[39m\u001b[38;5;124m'''\u001b[39m)\n\u001b[1;32m--> 157\u001b[0m handler \u001b[38;5;241m=\u001b[39m \u001b[43mStaticFiles\u001b[49m\u001b[43m(\u001b[49m\u001b[43mdirectory\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mlocal_directory\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mfollow_symlink\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mfollow_symlink\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 159\u001b[0m \u001b[38;5;129m@self\u001b[39m\u001b[38;5;241m.\u001b[39mget(url_path \u001b[38;5;241m+\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124m/\u001b[39m\u001b[38;5;124m{\u001b[39m\u001b[38;5;124mpath:path}\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[0;32m 160\u001b[0m \u001b[38;5;28;01masync\u001b[39;00m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mstatic_file\u001b[39m(request: Request, path: \u001b[38;5;28mstr\u001b[39m \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124m'\u001b[39m) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Response:\n\u001b[0;32m 161\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;01mawait\u001b[39;00m handler\u001b[38;5;241m.\u001b[39mget_response(path, request\u001b[38;5;241m.\u001b[39mscope)\n", "File \u001b[1;32mc:\\Users\\lxw\\.conda\\envs\\py311\\Lib\\site-packages\\starlette\\staticfiles.py:59\u001b[0m, in \u001b[0;36mStaticFiles.__init__\u001b[1;34m(self, directory, packages, html, check_dir, follow_symlink)\u001b[0m\n\u001b[0;32m 57\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mfollow_symlink \u001b[38;5;241m=\u001b[39m follow_symlink\n\u001b[0;32m 58\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m check_dir \u001b[38;5;129;01mand\u001b[39;00m directory \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m os\u001b[38;5;241m.\u001b[39mpath\u001b[38;5;241m.\u001b[39misdir(directory):\n\u001b[1;32m---> 59\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mRuntimeError\u001b[39;00m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mDirectory \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mdirectory\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m does not exist\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", "\u001b[1;31mRuntimeError\u001b[0m: Directory 'examples' does not exist" ] } ], "source": [] }, { "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 }