# PythonAnywhere Deployment Guide for Sales Dashboard

## Complete Step-by-Step Deployment

### **Step 1: Sign Up (Already Done)**
- Go to: https://www.pythonanywhere.com/
- Sign up for account (Free or €5/month Hacker plan)
- Note your username: `YOUR_USERNAME`

---

### **Step 2: Upload Your Code**

#### **Option A: Upload via Web Interface (Easiest)**

1. **Create a ZIP file of your project:**
   - Compress these files into `sales-dashboard.zip`:
     ```
     app.py
     client_intelligence_helper.py
     priority_actions_helper.py
     requirements.txt
     templates/ (folder)
     static/ (folder)
     credentials.json
     ```

2. **Upload to PythonAnywhere:**
   - Log into PythonAnywhere
   - Go to **"Files"** tab
   - Click **"Upload a file"**
   - Upload `sales-dashboard.zip`
   - Click on the uploaded file
   - Extract it: `unzip sales-dashboard.zip`

#### **Option B: Upload via Git (Recommended)**

1. **In PythonAnywhere Console:**
   ```bash
   cd ~
   git clone https://github.com/YOUR_REPO/sales-dashboard.git
   cd sales-dashboard
   ```

---

### **Step 3: Set Up Virtual Environment**

1. **Open a Bash console** in PythonAnywhere
2. Run these commands:

```bash
# Create virtual environment
mkvirtualenv --python=/usr/bin/python3.10 sales-env

# Activate it (should happen automatically)
workon sales-env

# Navigate to your app directory
cd ~/sales-dashboard  # or wherever you uploaded

# Install dependencies
pip install -r requirements.txt
```

**Wait 2-5 minutes** for installation to complete.

---

### **Step 4: Create Web App**

1. Go to **"Web"** tab in PythonAnywhere
2. Click **"Add a new web app"**
3. Choose:
   - **Manual configuration**
   - **Python 3.10** (or latest available)
4. Click through the wizard

---

### **Step 5: Configure WSGI File**

1. In the **Web** tab, find **"Code"** section
2. Click on the **WSGI configuration file** link
3. **Delete all content** and replace with:

```python
import sys
import os

# Add your project directory to the sys.path
project_home = '/home/YOUR_USERNAME/sales-dashboard'
if project_home not in sys.path:
    sys.path = [project_home] + sys.path

# Set environment variables
os.environ['FLASK_MODE'] = 'production'
os.environ['SECRET_KEY'] = 'your-secret-key-here-change-this'
os.environ['BASE_URL'] = 'https://YOUR_USERNAME.pythonanywhere.com'

# Import Flask app
from app import app as application
```

**IMPORTANT:** Replace:
- `YOUR_USERNAME` with your actual PythonAnywhere username
- `your-secret-key-here-change-this` with a secure random string

---

### **Step 6: Configure Virtual Environment in Web Tab**

1. Still in the **Web** tab
2. Find **"Virtualenv"** section
3. Enter path: `/home/YOUR_USERNAME/.virtualenvs/sales-env`
4. Click checkmark to save

---

### **Step 7: Set Working Directory**

1. In **Web** tab, find **"Code"** section
2. Set **"Working directory"** to: `/home/YOUR_USERNAME/sales-dashboard`

---

### **Step 8: Upload Google Credentials**

1. Go to **"Files"** tab
2. Navigate to `/home/YOUR_USERNAME/sales-dashboard/`
3. Upload your `credentials.json` file
4. **Set permissions:**
   ```bash
   chmod 600 /home/YOUR_USERNAME/sales-dashboard/credentials.json
   ```

---

### **Step 9: Reload Web App**

1. Go back to **"Web"** tab
2. Click big green **"Reload"** button
3. Wait 10-20 seconds

---

### **Step 10: Test Your App**

1. Click the link at top: `https://YOUR_USERNAME.pythonanywhere.com`
2. You should see your login page!

---

### **Step 11: Set Up Custom Domain (Optional - Requires Paid Plan)**

If you have the €5/month plan:

1. In **Web** tab, find **"Configuration for YOUR_USERNAME.pythonanywhere.com"**
2. Click **"Add a new web app"** (for custom domain)
3. Enter your domain: `sales.globalerc.pt`
4. Follow prompts to configure

Then in your **domain registrar** (where you bought globalerc.pt):
- Add CNAME record: `sales` → `YOUR_USERNAME.pythonanywhere.com`

---

## **Troubleshooting**

### **Error: "ImportError: No module named..."**
- Check virtual environment is activated
- Run: `pip install -r requirements.txt` again

### **Error: "Internal Server Error"**
- Check error log in **Web** tab
- Look for Python errors
- Common issue: Wrong paths in WSGI file

### **Error: "Google credentials not found"**
- Check `credentials.json` is in correct directory
- Check file permissions: `ls -la credentials.json`

### **App loads but login fails**
- Check `BASE_URL` environment variable matches your domain
- Check Google OAuth redirect URIs include your PythonAnywhere URL

---

## **Environment Variables**

In your WSGI file, you can set these:

```python
os.environ['FLASK_MODE'] = 'production'
os.environ['SECRET_KEY'] = 'generate-random-string-here'
os.environ['BASE_URL'] = 'https://YOUR_USERNAME.pythonanywhere.com'
os.environ['DEFAULT_SPREADSHEET_ID'] = 'your-spreadsheet-id'
```

---

## **Updating Your App**

When you make changes:

1. **Upload new files** via Files tab (or `git pull` if using Git)
2. Go to **Web** tab
3. Click **"Reload"** button

---

## **Security Checklist**

- ✅ Set strong `SECRET_KEY` in WSGI file
- ✅ Set `credentials.json` permissions to 600
- ✅ Use HTTPS (automatic on PythonAnywhere)
- ✅ Keep `FLASK_MODE=production`
- ✅ Don't commit credentials to Git

---

## **Cost**

- **Free tier:** Limited CPU, no custom domain, `username.pythonanywhere.com`
- **€5/month (Hacker):** More CPU, custom domain, better for production
- **€12/month (Web Developer):** Even more resources, multiple apps

---

## **Support**

- PythonAnywhere Help: https://help.pythonanywhere.com/
- Forum: https://www.pythonanywhere.com/forums/

---

## **Next Steps After Deployment**

1. Test all functionality (login, data loading, charts)
2. Update Google OAuth redirect URIs to include new URL
3. Share URL with sales team
4. Monitor error logs regularly
5. Set up custom domain (if paid plan)
