diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..2aeb0b7 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,36 @@ +# Use an official Python runtime as a parent image +FROM python:3.12-slim + +# Prevents writing .pyc files and buffering stdout/stderr +ENV PYTHONDONTWRITEBYTECODE=1 +ENV PYTHONUNBUFFERED=1 + +# Set working directory +WORKDIR /app + +# Copy requirements first to leverage Docker cache +COPY requirements.txt . + +# Install system dependencies (if needed) and Python deps +RUN apt-get update && apt-get install -y --no-install-recommends \ + build-essential \ + && rm -rf /var/lib/apt/lists/* + +RUN pip install --upgrade pip +RUN pip install --no-cache-dir -r requirements.txt + +# Copy app code +COPY . . + +# Expose Streamlit default port +EXPOSE 8501 + +# Streamlit config: run headless and bind to 0.0.0.0 +ENV STREAMLIT_SERVER_HEADLESS=true +ENV STREAMLIT_SERVER_ENABLE_CORS=false +ENV STREAMLIT_SERVER_ENABLE_XSRF_PROTECTION=false +ENV STREAMLIT_SERVER_PORT=8501 +ENV STREAMLIT_SERVER_ADDRESS=0.0.0.0 + +# Run Streamlit +CMD ["streamlit", "run", "app.py", "--server.port=8501", "--server.address=0.0.0.0"] diff --git a/docs/install.md b/docs/install.md index e73b135..3b00427 100644 --- a/docs/install.md +++ b/docs/install.md @@ -68,6 +68,25 @@ pip install streamlit-autorefresh openpyxl xlrd cryptography openai 3. Open `http://localhost:8501` in your browser. The home page should load without import errors. +## 5. Run with Docker (optional) + +If you prefer an isolated container build, use the included `Dockerfile`: + +```bash +docker build -t trafficsafeanalyzer . +docker run --rm -p 8501:8501 trafficsafeanalyzer +``` + +To work with local data, mount the host folder containing Excel files: + +```bash +docker run --rm -p 8501:8501 \ + -v "$(pwd)/sample:/app/sample" \ + trafficsafeanalyzer +``` + +The container exposes Streamlit on port 8501 by default. Override configuration via environment variables when needed, for example `-e STREAMLIT_SERVER_PORT=8502`. + ## Troubleshooting tips - **Missing package**: Re-run `pip install -r requirements.txt`. diff --git a/docs/usage.md b/docs/usage.md index c20b079..77943da 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -4,7 +4,7 @@ TrafficSafeAnalyzer delivers accident analytics and decision support through a S ## Start the app -1. Activate your virtual or conda environment. +1. Activate your virtual or conda environment(或在容器中运行,见下). 2. From the project root, run: ```bash @@ -13,6 +13,8 @@ TrafficSafeAnalyzer delivers accident analytics and decision support through a S 3. Open `http://localhost:8501`. Keep the terminal running while you work in the browser. +> 使用 Docker?运行 `docker build -t trafficsafeanalyzer .` 与 `docker run --rm -p 8501:8501 trafficsafeanalyzer` 后,同样访问 `http://localhost:8501`。 + ## Load input data Use the sidebar form labelled “数据与筛选”. diff --git a/readme.md b/readme.md index 492e334..43db306 100644 --- a/readme.md +++ b/readme.md @@ -19,7 +19,7 @@ - Git - 可选:Docker(用于容器化部署) -### 安装 +### 安装(本地环境) 1. 克隆仓库: @@ -63,6 +63,28 @@ streamlit run app.py streamlit run app.py ``` +### 使用 Docker 运行 + +项目根目录已经包含 `Dockerfile`,无需额外配置即可容器化运行: + +```bash +# 构建镜像 +docker build -t trafficsafeanalyzer . + +# 以临时容器方式启动 +docker run --rm -p 8501:8501 trafficsafeanalyzer +``` + +运行后访问 `http://localhost:8501` 即可。若需加载主机上的数据文件,可通过挂载方式注入: + +```bash +docker run --rm -p 8501:8501 \ + -v "$(pwd)/sample:/app/sample" \ + trafficsafeanalyzer +``` + +容器内默认启用了示例 AI 凭据与 Streamlit Headless 模式,如需调整可在 `docker run` 时追加环境变量(例如 `-e STREAMLIT_SERVER_PORT=8502`)。 + ## 依赖项 列于 `requirements.txt`: