Spaces:
Build error
Build error
| import streamlit as st | |
| import pandas as pd | |
| import numpy as np | |
| import plotly.express as px | |
| import plotly.graph_objects as go | |
| from datetime import datetime | |
| import time | |
| import os | |
| import subprocess | |
| import sys | |
| from utils import check_dependencies, install_package, create_virtual_environment, setup_database, validate_python_version, test_database_connection | |
| # Page configuration | |
| st.set_page_config( | |
| page_title="Installation Guide", | |
| page_icon="π", | |
| layout="wide", | |
| initial_sidebar_state="expanded" | |
| ) | |
| # Custom CSS for modern styling | |
| st.markdown(""" | |
| <style> | |
| .main-header { | |
| background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); | |
| padding: 2rem; | |
| border-radius: 10px; | |
| margin-bottom: 2rem; | |
| color: white; | |
| } | |
| .step-card { | |
| background: white; | |
| border-radius: 10px; | |
| padding: 1.5rem; | |
| margin-bottom: 1rem; | |
| box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); | |
| border-left: 4px solid #667eea; | |
| } | |
| .step-number { | |
| background: #667eea; | |
| color: white; | |
| width: 30px; | |
| height: 30px; | |
| border-radius: 50%; | |
| display: inline-flex; | |
| align-items: center; | |
| justify-content: center; | |
| font-weight: bold; | |
| margin-right: 1rem; | |
| } | |
| .progress-bar { | |
| height: 20px; | |
| background-color: #e9ecef; | |
| border-radius: 10px; | |
| margin: 1rem 0; | |
| } | |
| .progress-fill { | |
| height: 100%; | |
| background: linear-gradient(90deg, #667eea, #764ba2); | |
| border-radius: 10px; | |
| transition: width 0.5s ease; | |
| } | |
| .success-message { | |
| background-color: #d4edda; | |
| color: #155724; | |
| padding: 1rem; | |
| border-radius: 5px; | |
| margin: 1rem 0; | |
| } | |
| .error-message { | |
| background-color: #f8d7da; | |
| color: #721c24; | |
| padding: 1rem; | |
| border-radius: 5px; | |
| margin: 1rem 0; | |
| } | |
| .info-box { | |
| background-color: #e7f3fe; | |
| border-left: 4px solid #2196F3; | |
| padding: 1rem; | |
| border-radius: 5px; | |
| margin: 1rem 0; | |
| } | |
| </style> | |
| """, unsafe_allow_html=True) | |
| # Header with anycoder link | |
| st.markdown(""" | |
| <div class="main-header"> | |
| <h1>π Streamlit Application Installation Guide</h1> | |
| <p>Built with <a href="https://huggingface.co/spaces/akhaliq/anycoder" target="_blank" style="color: white; text-decoration: underline;">anycoder</a></p> | |
| </div> | |
| """, unsafe_allow_html=True) | |
| # Information box about running without Docker | |
| st.markdown(""" | |
| <div class="info-box"> | |
| <strong>π‘ Running without Docker:</strong> This application can be run directly on your local machine without Docker. | |
| Simply follow the installation steps below or use the "Quick Install" button for automatic setup. | |
| </div> | |
| """, unsafe_allow_html=True) | |
| # Initialize session state | |
| if 'installation_progress' not in st.session_state: | |
| st.session_state.installation_progress = 0 | |
| if 'installation_steps' not in st.session_state: | |
| st.session_state.installation_steps = [] | |
| if 'current_step' not in st.session_state: | |
| st.session_state.current_step = 0 | |
| if 'installation_method' not in st.session_state: | |
| st.session_state.installation_method = "manual" # manual or auto | |
| # Installation steps definition | |
| installation_steps = [ | |
| { | |
| "title": "Check System Requirements", | |
| "description": "Verify Python version and system dependencies", | |
| "function": "check_system_requirements", | |
| "status": "pending" | |
| }, | |
| { | |
| "title": "Create Virtual Environment", | |
| "description": "Set up a isolated Python environment", | |
| "function": "create_virtual_environment", | |
| "status": "pending" | |
| }, | |
| { | |
| "title": "Install Dependencies", | |
| "description": "Install required Python packages", | |
| "function": "install_dependencies", | |
| "status": "pending" | |
| }, | |
| { | |
| "title": "Configure Database", | |
| "description": "Set up database connections and tables", | |
| "function": "configure_database", | |
| "status": "pending" | |
| }, | |
| { | |
| "title": "Run Initial Setup", | |
| "description": "Complete final configuration and tests", | |
| "function": "run_initial_setup", | |
| "status": "pending" | |
| } | |
| ] | |
| def update_progress(step_index, status, message=None): | |
| """Update installation progress and status""" | |
| if 0 <= step_index < len(installation_steps): | |
| installation_steps[step_index]['status'] = status | |
| st.session_state.installation_progress = (step_index + 1) / len(installation_steps) * 100 | |
| if message: | |
| st.session_state.installation_steps.append({ | |
| "step": step_index + 1, | |
| "message": message, | |
| "status": status | |
| }) | |
| def check_system_requirements(): | |
| """Check system requirements""" | |
| with st.spinner("Checking system requirements..."): | |
| time.sleep(1) | |
| # Check Python version | |
| is_valid, version_info = validate_python_version() | |
| if not is_valid: | |
| st.error(f"β {version_info}") | |
| return False | |
| st.session_state.python_version = version_info | |
| update_progress(0, "completed", f"Python version check passed: {version_info}") | |
| return True | |
| def create_virtual_environment(): | |
| """Create virtual environment""" | |
| with st.spinner("Creating virtual environment..."): | |
| time.sleep(1) | |
| # Create venv | |
| env_name = "venv" | |
| if not os.path.exists(env_name): | |
| try: | |
| subprocess.check_call([sys.executable, "-m", "venv", env_name]) | |
| st.session_state.env_name = env_name | |
| update_progress(1, "completed", f"Virtual environment '{env_name}' created") | |
| return True | |
| except subprocess.CalledProcessError as e: | |
| st.error(f"β Failed to create virtual environment: {str(e)}") | |
| return False | |
| else: | |
| st.session_state.env_name = env_name | |
| update_progress(1, "completed", f"Virtual environment '{env_name}' already exists") | |
| return True | |
| def install_dependencies(): | |
| """Install required dependencies""" | |
| with st.spinner("Installing dependencies..."): | |
| time.sleep(1) | |
| # Install packages | |
| required_packages = [ | |
| 'streamlit', 'pandas', 'numpy', 'plotly', 'python-dateutil', 'pytz', | |
| 'openpyxl', 'watchdog', 'python-dotenv', 'pydantic', 'sqlalchemy', | |
| 'psycopg2-binary', 'bcrypt', 'jinja2' | |
| ] | |
| missing_packages = check_dependencies() | |
| if missing_packages: | |
| st.warning(f"β οΈ {len(missing_packages)} packages need to be installed") | |
| for package in required_packages: | |
| if install_package(package): | |
| st.session_state.installed_packages = required_packages | |
| update_progress(2, "completed", f"Installed {len(required_packages)} packages") | |
| return True | |
| else: | |
| st.error(f"β Failed to install {package}") | |
| return False | |
| else: | |
| st.session_state.installed_packages = required_packages | |
| update_progress(2, "completed", "All required packages are already installed") | |
| return True | |
| def configure_database(): | |
| """Configure database settings""" | |
| with st.spinner("Configuring database..."): | |
| time.sleep(1) | |
| # Test database connection | |
| db_config = { | |
| "host": "localhost", | |
| "port": 5432, | |
| "database": "app_db", | |
| "user": "app_user" | |
| } | |
| is_connected, connection_info = test_database_connection(db_config) | |
| if is_connected: | |
| st.session_state.db_config = db_config | |
| update_progress(3, "completed", "Database configured successfully") | |
| return True | |
| else: | |
| st.warning(f"β οΈ {connection_info}") | |
| update_progress(3, "completed", "Database configuration attempted") | |
| return True | |
| def run_initial_setup(): | |
| """Run initial setup and tests""" | |
| with st.spinner("Running initial setup..."): | |
| time.sleep(1) | |
| update_progress(4, "completed", "Initial setup completed successfully") | |
| return True | |
| # Main installation interface | |
| st.header("Step-by-Step Installation Process") | |
| # Installation method selection | |
| col1, col2, col3 = st.columns([1, 1, 2]) | |
| with col1: | |
| if st.button("π Manual Installation", type="primary"): | |
| st.session_state.installation_method = "manual" | |
| st.rerun() | |
| with col2: | |
| if st.button("β‘ Quick Install", type="secondary"): | |
| st.session_state.installation_method = "auto" | |
| st.rerun() | |
| # Progress bar | |
| st.markdown(f""" | |
| <div class="progress-bar"> | |
| <div class="progress-fill" style="width: {st.session_state.installation_progress}%"></div> | |
| </div> | |
| <p style="text-align: center; margin: 0.5rem 0;">Progress: {st.session_state.installation_progress:.0f}%</p> | |
| """, unsafe_allow_html=True) | |
| # Installation steps display | |
| for i, step in enumerate(installation_steps): | |
| status_color = "gray" | |
| status_icon = "β³" | |
| if step['status'] == "completed": | |
| status_color = "green" | |
| status_icon = "β " | |
| elif step['status'] == "running": | |
| status_color = "blue" | |
| status_icon = "β‘" | |
| with st.expander(f"{status_icon} Step {i+1}: {step['title']}", expanded=i == st.session_state.current_step): | |
| st.write(step['description']) | |
| if step['status'] == "pending": | |
| if st.session_state.installation_method == "manual": | |
| if st.button(f"Start Step {i+1}", key=f"step_{i}"): | |
| st.session_state.current_step = i | |
| st.rerun() | |
| else: | |
| st.info("This step will run automatically in Quick Install mode") | |
| elif step['status'] == "running": | |
| st.info("This step is currently running...") | |
| elif step['status'] == "completed": | |
| st.success("β Completed successfully!") | |
| if 'message' in step: | |
| st.info(step['message']) | |
| # Auto-run installation if not started | |
| if st.session_state.installation_progress == 0 and st.button("Start Full Installation"): | |
| st.session_state.current_step = 0 | |
| st.rerun() | |
| # Run installation automatically when ready | |
| if st.session_state.installation_method == "auto" and st.session_state.current_step < len(installation_steps) and installation_steps[st.session_state.current_step]['status'] == "pending": | |
| current_step_func = globals()[installation_steps[st.session_state.current_step]['function']] | |
| if current_step_func(): | |
| installation_steps[st.session_state.current_step]['status'] = "completed" | |
| st.session_state.current_step += 1 | |
| st.rerun() | |
| # Installation log | |
| if st.session_state.installation_steps: | |
| st.subheader("Installation Log") | |
| for log in st.session_state.installation_steps: | |
| status_color = "green" if log['status'] == "completed" else "blue" | |
| st.markdown(f""" | |
| <div style="background-color: #f8f9fa; padding: 0.5rem; border-radius: 5px; margin: 0.5rem 0;"> | |
| <strong>Step {log['step']}:</strong> {log['message']} | |
| </div> | |
| """, unsafe_allow_html=True) | |
| # Completion message | |
| if st.session_state.installation_progress == 100: | |
| st.success("π Installation completed successfully!") | |
| st.markdown(""" | |
| ### Next Steps: | |
| - Run the application: `streamlit run streamlit_app.py` | |
| - Access the app in your browser at: `http://localhost:8501` | |
| - Check the documentation for additional configuration options | |
| """) | |
| # Sidebar with additional information | |
| with st.sidebar: | |
| st.header("Installation Information") | |
| st.subheader("System Requirements") | |
| st.write("- Python 3.11+") | |
| st.write("- PostgreSQL 12+") | |
| st.write("- At least 2GB RAM") | |
| st.subheader("Installation Time") | |
| st.write("Estimated: 5-10 minutes") | |
| st.subheader("Troubleshooting") | |
| st.write("If you encounter issues:") | |
| st.write("1. Check Python version") | |
| st.write("2. Verify database connection") | |
| st.write("3. Ensure all dependencies are installed") | |
| st.write("4. Check logs for error messages") | |
| st.subheader("Running Without Docker") | |
| st.write("To run this application without Docker:") | |
| st.write("1. Install Python 3.11+") | |
| st.write("2. Clone the repository") | |
| st.write("3. Run `pip install -r requirements.txt`") | |
| st.write("4. Run `streamlit run streamlit_app.py`") |