|
| 1 | +```python exec |
| 2 | +import reflex as rx |
| 3 | +from pcweb.styles.styles import get_code_style, cell_style |
| 4 | + |
| 5 | +``` |
| 6 | + |
| 7 | +# Deploy Reflex to Databricks |
| 8 | + |
| 9 | +This guide walks you through deploying a Reflex web application on Databricks using the Apps platform. |
| 10 | + |
| 11 | +## Prerequisites |
| 12 | + |
| 13 | +- Databricks workspace with Unity Catalog enabled |
| 14 | +- GitHub repository containing your Reflex application |
| 15 | +- Reflex Enterprise license (for single-port deployment) |
| 16 | + |
| 17 | +## Step 1: Connect Your Repository |
| 18 | + |
| 19 | +1. **Link GitHub Repository** |
| 20 | + - Navigate to your Databricks workspace |
| 21 | + - Go to your user directory |
| 22 | + - Click **Create** → **Git folder** |
| 23 | + - Paste the URL of your GitHub repository containing the Reflex application |
| 24 | + |
| 25 | +## Step 2: Configure Application Settings |
| 26 | + |
| 27 | +### Create Configuration File |
| 28 | + |
| 29 | +Create a new file called `app.yaml` directly in Databricks (not in GitHub): |
| 30 | + |
| 31 | +```yaml |
| 32 | +command: [ |
| 33 | + "reflex", |
| 34 | + "run", |
| 35 | + "--env", |
| 36 | + "prod", |
| 37 | + "--backend-port", |
| 38 | + "$DATABRICKS_APP_PORT" |
| 39 | +] |
| 40 | + |
| 41 | +env: |
| 42 | + - name: "HOME" |
| 43 | + value: "/tmp/reflex" |
| 44 | + - name: "REFLEX_ACCESS_TOKEN" |
| 45 | + value: "your-token-here" |
| 46 | + - name: "DATABRICKS_WAREHOUSE_ID" |
| 47 | + valueFrom: "sql_warehouse" |
| 48 | + - name: "DATABRICKS_CATALOG" |
| 49 | + value: "your-catalog-name" |
| 50 | + - name: "DATABRICKS_SCHEMA" |
| 51 | + value: "your-schema-name" |
| 52 | + - name: "REFLEX_SHOW_BUILT_WITH_REFLEX" |
| 53 | + value: 0 |
| 54 | +``` |
| 55 | +
|
| 56 | +### Obtain Required Tokens |
| 57 | +
|
| 58 | +1. **Reflex Access Token** |
| 59 | + - Visit [Reflex Cloud Tokens](https://cloud.reflex.dev/tokens/) |
| 60 | + - Navigate to Account Settings → Tokens |
| 61 | + - Create a new token and copy the value |
| 62 | + - Replace `your-token-here` in the configuration |
| 63 | +2. **Databricks Resources** |
| 64 | + - Update `DATABRICKS_CATALOG` with your target catalog name |
| 65 | + - Update `DATABRICKS_SCHEMA` with your target schema name |
| 66 | + |
| 67 | +## Step 3: Enable Single-Port Deployment |
| 68 | + |
| 69 | +Update your Reflex application for Databricks compatibility: |
| 70 | + |
| 71 | +### Update rxconfig.py |
| 72 | + |
| 73 | +```python |
| 74 | +import reflex as rx |
| 75 | +import reflex_enterprise as rxe |
| 76 | +
|
| 77 | +rxe.Config(app_name="app", use_single_port=True) |
| 78 | +``` |
| 79 | + |
| 80 | +### Update Application Entry Point |
| 81 | + |
| 82 | +Modify your main application file where you define `rx.App`: |
| 83 | + |
| 84 | +```python |
| 85 | +import reflex_enterprise as rxe |
| 86 | +
|
| 87 | +app = rxe.App( |
| 88 | + # your app configuration |
| 89 | +) |
| 90 | +``` |
| 91 | + |
| 92 | +```md alert info |
| 93 | +# Also add `reflex-enterprise` and `asgiproxy` to your `requirements.txt` file. |
| 94 | +``` |
| 95 | + |
| 96 | +## Step 4: Create Databricks App |
| 97 | + |
| 98 | +1. **Navigate to Apps** |
| 99 | + - Go to **Compute** → **Apps** |
| 100 | + - Click **Create App** |
| 101 | +2. **Configure Application** |
| 102 | + - Select **Custom App** |
| 103 | + - Configure SQL warehouse for your application |
| 104 | + |
| 105 | +## Step 5: Set Permissions |
| 106 | + |
| 107 | +### Catalog Permissions |
| 108 | + |
| 109 | +1. Navigate to **Catalog** → Select your target catalog |
| 110 | +2. Go to **Permissions** |
| 111 | +3. Add the app's service principal user |
| 112 | +4. Grant the following permissions: |
| 113 | + - **USE CATALOG** |
| 114 | + - **USE SCHEMA** |
| 115 | + |
| 116 | +### Schema Permissions |
| 117 | + |
| 118 | +1. Navigate to the specific schema |
| 119 | +2. Go to **Permissions** |
| 120 | +3. Grant the following permissions: |
| 121 | + - **USE SCHEMA** |
| 122 | + - **EXECUTE** |
| 123 | + - **SELECT** |
| 124 | + - **READ VOLUME** (if required) |
| 125 | + |
| 126 | +## Step 6: Deploy Application |
| 127 | + |
| 128 | +1. **Initiate Deployment** |
| 129 | + - Click **Deploy** in the Apps interface |
| 130 | + - When prompted for the code path, provide your Git folder path or select your repository folder |
| 131 | +2. **Monitor Deployment** |
| 132 | + - The deployment process will begin automatically |
| 133 | + - Monitor logs for any configuration issues |
| 134 | + |
| 135 | +## Updating Your Application |
| 136 | + |
| 137 | +To deploy updates from your GitHub repository: |
| 138 | + |
| 139 | +1. **Pull Latest Changes** |
| 140 | + - In the deployment interface, click **Deployment Source** |
| 141 | + - Select **main** branch |
| 142 | + - Click **Pull** to fetch the latest changes from GitHub |
| 143 | +2. **Redeploy** |
| 144 | + - Click **Deploy** again to apply the updates |
| 145 | + |
| 146 | +## Configuration Reference |
| 147 | + |
| 148 | +```python eval |
| 149 | +rx.table.root( |
| 150 | + rx.table.header( |
| 151 | + rx.table.row( |
| 152 | + rx.table.column_header_cell("Environment Variable"), |
| 153 | + rx.table.column_header_cell("Description"), |
| 154 | + rx.table.column_header_cell("Example"), |
| 155 | + ), |
| 156 | + ), |
| 157 | + rx.table.body( |
| 158 | + rx.table.row( |
| 159 | + rx.table.cell(rx.code("HOME")), |
| 160 | + rx.table.cell("Application home directory"), |
| 161 | + rx.table.cell(rx.code("/tmp/reflex")), |
| 162 | + ), |
| 163 | + rx.table.row( |
| 164 | + rx.table.cell(rx.code("REFLEX_ACCESS_TOKEN")), |
| 165 | + rx.table.cell("Authentication for Reflex Cloud"), |
| 166 | + rx.table.cell(rx.code("rx_token_...")), |
| 167 | + ), |
| 168 | + rx.table.row( |
| 169 | + rx.table.cell(rx.code("DATABRICKS_WAREHOUSE_ID")), |
| 170 | + rx.table.cell("SQL warehouse identifier"), |
| 171 | + rx.table.cell("Auto-assigned"), |
| 172 | + ), |
| 173 | + rx.table.row( |
| 174 | + rx.table.cell(rx.code("DATABRICKS_CATALOG")), |
| 175 | + rx.table.cell("Target catalog name"), |
| 176 | + rx.table.cell(rx.code("main")), |
| 177 | + ), |
| 178 | + rx.table.row( |
| 179 | + rx.table.cell(rx.code("DATABRICKS_SCHEMA")), |
| 180 | + rx.table.cell("Target schema name"), |
| 181 | + rx.table.cell(rx.code("default")), |
| 182 | + ), |
| 183 | + rx.table.row( |
| 184 | + rx.table.cell(rx.code("REFLEX_SHOW_BUILT_WITH_REFLEX")), |
| 185 | + rx.table.cell("Show Reflex branding (Enterprise only)"), |
| 186 | + rx.table.cell([rx.code("0"), " or ", rx.code("1")]), |
| 187 | + ), |
| 188 | + ), |
| 189 | + variant="surface", |
| 190 | + margin_y="1em", |
| 191 | +) |
| 192 | +``` |
| 193 | + |
| 194 | +## Troubleshooting |
| 195 | + |
| 196 | +- **Permission Errors**: Verify that all catalog and schema permissions are correctly set |
| 197 | +- **Port Issues**: Ensure you're using `$DATABRICKS_APP_PORT` and single-port configuration |
| 198 | +- **Token Issues**: Verify your Reflex access token is valid and properly configured |
| 199 | +- **Deployment Failures**: Check the deployment logs for specific error messages |
| 200 | + |
| 201 | +## Notes |
| 202 | + |
| 203 | +- Single-port deployment requires Reflex Enterprise |
| 204 | +- Configuration must be created directly in Databricks, not pushed from GitHub |
| 205 | +- Updates require manual pulling from the deployment interface |
0 commit comments