Spaces:
Runtime error
Runtime error
| # Dockerfile | |
| FROM python:3.9-slim | |
| EXPOSE 8501 | |
| WORKDIR /app | |
| # Install system dependencies | |
| RUN apt-get update && apt-get install -y \ | |
| build-essential \ | |
| software-properties-common \ | |
| git \ | |
| nginx \ | |
| && rm -rf /var/lib/apt/lists/* | |
| # Copy requirements and install Python packages | |
| COPY requirements.txt . | |
| RUN pip3 install -r requirements.txt | |
| # Copy the Streamlit app and configuration | |
| COPY . . | |
| # Create Streamlit config directory and add config.toml | |
| RUN mkdir -p /root/.streamlit | |
| RUN echo "\ | |
| [server]\n\ | |
| enableCORS = true\n\ | |
| enableXsrfProtection = false\n\ | |
| " > /root/.streamlit/config.toml | |
| # Create Nginx configuration | |
| RUN echo "\ | |
| events {\n\ | |
| worker_connections 1024;\n\ | |
| }\n\ | |
| http {\n\ | |
| upstream streamlit {\n\ | |
| server localhost:8501;\n\ | |
| }\n\ | |
| server {\n\ | |
| listen 80;\n\ | |
| server_name localhost;\n\ | |
| \n\ | |
| # CORS configuration\n\ | |
| add_header 'Access-Control-Allow-Origin' '*' always;\n\ | |
| add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;\n\ | |
| add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization' always;\n\ | |
| add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range' always;\n\ | |
| \n\ | |
| location / {\n\ | |
| proxy_pass http://streamlit;\n\ | |
| proxy_http_version 1.1;\n\ | |
| proxy_set_header Upgrade \$http_upgrade;\n\ | |
| proxy_set_header Connection 'upgrade';\n\ | |
| proxy_set_header Host \$host;\n\ | |
| proxy_cache_bypass \$http_upgrade;\n\ | |
| proxy_set_header X-Real-IP \$remote_addr;\n\ | |
| proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;\n\ | |
| \n\ | |
| # CORS preflight\n\ | |
| if (\$request_method = 'OPTIONS') {\n\ | |
| add_header 'Access-Control-Allow-Origin' '*';\n\ | |
| add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';\n\ | |
| add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization';\n\ | |
| add_header 'Access-Control-Max-Age' 1728000;\n\ | |
| add_header 'Content-Type' 'text/plain; charset=utf-8';\n\ | |
| add_header 'Content-Length' 0;\n\ | |
| return 204;\n\ | |
| }\n\ | |
| }\n\ | |
| }\n\ | |
| }" > /etc/nginx/nginx.conf | |
| # Create start script | |
| RUN echo '#!/bin/bash\n\ | |
| service nginx start\n\ | |
| streamlit run streamlit.py --server.port=8501 --server.address=0.0.0.0\n\ | |
| ' > /app/start.sh | |
| RUN chmod +x /app/start.sh | |
| # Create a sample Streamlit app | |
| RUN echo "\ | |
| import streamlit as st\n\ | |
| \n\ | |
| st.title('Sample Streamlit App with CORS')\n\ | |
| st.write('Hello, this is a sample Streamlit app with CORS enabled!')\n\ | |
| " > /app/streamlit.py | |
| # Create requirements.txt if not exists | |
| RUN if [ ! -f requirements.txt ]; then echo "streamlit>=1.0.0" > requirements.txt; fi | |
| ENTRYPOINT ["/app/start.sh"] |