|
| 1 | +# Tutorial - Deploy to PythonAnywhere |
| 2 | + |
| 3 | +This tutorial picks up where [Tutorial 6 - Put it on the web!](https://tutorial.beeware.org/en/latest/tutorial/tutorial-6/) of the BeeWare Tutorial leaves off. In that tutorial, you built a static web app and ran it locally with `briefcase run web`. Now we'll publish it to PythonAnywhere so it's accessible on the internet. |
| 4 | + |
| 5 | +/// admonition | Prerequisites |
| 6 | + type: info |
| 7 | + |
| 8 | +Before starting, make sure you have: |
| 9 | + |
| 10 | +* Completed the BeeWare tutorial through [Tutorial 6](https://tutorial.beeware.org/en/latest/tutorial/tutorial-6/) (or at least run `briefcase create web static` and `briefcase build web static`) |
| 11 | +* A [PythonAnywhere](https://www.pythonanywhere.com/) account |
| 12 | +* An API token from your [Account page](https://www.pythonanywhere.com/account/#api_token) |
| 13 | + |
| 14 | +/// |
| 15 | + |
| 16 | +## Install the plugin |
| 17 | + |
| 18 | +With your BeeWare tutorial virtual environment active, install the plugin: |
| 19 | + |
| 20 | +/// tab | macOS |
| 21 | + |
| 22 | +```console |
| 23 | +(beeware-venv) $ pip install pythonanywhere-briefcase-plugin |
| 24 | +``` |
| 25 | + |
| 26 | +/// |
| 27 | + |
| 28 | +/// tab | Linux |
| 29 | + |
| 30 | +```console |
| 31 | +(beeware-venv) $ pip install pythonanywhere-briefcase-plugin |
| 32 | +``` |
| 33 | + |
| 34 | +/// |
| 35 | + |
| 36 | +/// tab | Windows |
| 37 | + |
| 38 | +```doscon |
| 39 | +(beeware-venv) C:\...>pip install pythonanywhere-briefcase-plugin |
| 40 | +``` |
| 41 | + |
| 42 | +/// |
| 43 | + |
| 44 | +## Package the app |
| 45 | + |
| 46 | +In Tutorial 6, you ran the app locally with `briefcase run web`. To publish it, we first need to package the app into a distributable artifact: |
| 47 | + |
| 48 | +/// tab | macOS |
| 49 | + |
| 50 | +```console |
| 51 | +(beeware-venv) $ briefcase package web static |
| 52 | + |
| 53 | +[helloworld] Packaging Hello World... |
| 54 | +... |
| 55 | + |
| 56 | +[helloworld] Packaged dist/Hello World-0.0.1.web.zip |
| 57 | +``` |
| 58 | + |
| 59 | +/// |
| 60 | + |
| 61 | +/// tab | Linux |
| 62 | + |
| 63 | +```console |
| 64 | +(beeware-venv) $ briefcase package web static |
| 65 | + |
| 66 | +[helloworld] Packaging Hello World... |
| 67 | +... |
| 68 | + |
| 69 | +[helloworld] Packaged dist/Hello World-0.0.1.web.zip |
| 70 | +``` |
| 71 | + |
| 72 | +/// |
| 73 | + |
| 74 | +/// tab | Windows |
| 75 | + |
| 76 | +```doscon |
| 77 | +(beeware-venv) C:\...>briefcase package web static |
| 78 | +
|
| 79 | +[helloworld] Packaging Hello World... |
| 80 | +... |
| 81 | +
|
| 82 | +[helloworld] Packaged dist\Hello World-0.0.1.web.zip |
| 83 | +``` |
| 84 | + |
| 85 | +/// |
| 86 | + |
| 87 | +This creates a ZIP file containing all the static files needed to serve your app. |
| 88 | + |
| 89 | +## Set your API token |
| 90 | + |
| 91 | +The plugin needs your PythonAnywhere API token to upload files and configure the webapp. Set it as an environment variable: |
| 92 | + |
| 93 | +/// tab | macOS |
| 94 | + |
| 95 | +```console |
| 96 | +(beeware-venv) $ export API_TOKEN=your-api-token-here |
| 97 | +``` |
| 98 | + |
| 99 | +/// |
| 100 | + |
| 101 | +/// tab | Linux |
| 102 | + |
| 103 | +```console |
| 104 | +(beeware-venv) $ export API_TOKEN=your-api-token-here |
| 105 | +``` |
| 106 | + |
| 107 | +/// |
| 108 | + |
| 109 | +/// tab | Windows |
| 110 | + |
| 111 | +```doscon |
| 112 | +(beeware-venv) C:\...>set API_TOKEN=your-api-token-here |
| 113 | +``` |
| 114 | + |
| 115 | +/// |
| 116 | + |
| 117 | +Replace `your-api-token-here` with the token from your [Account page](https://www.pythonanywhere.com/account/#api_token). |
| 118 | + |
| 119 | +//// admonition | Username |
| 120 | + type: note |
| 121 | + |
| 122 | +The plugin needs to know your PythonAnywhere username to upload files to the right place. By default, it uses the `PYTHONANYWHERE_USERNAME` environment variable, falling back to your local system username. |
| 123 | + |
| 124 | +If you're running this from a PythonAnywhere console, the username is detected automatically -- no extra configuration needed. |
| 125 | + |
| 126 | +If you're running locally and your PythonAnywhere username is different from your local system username, either set the environment variable: |
| 127 | + |
| 128 | +/// tab | macOS |
| 129 | + |
| 130 | +```console |
| 131 | +(beeware-venv) $ export PYTHONANYWHERE_USERNAME=your-pa-username |
| 132 | +``` |
| 133 | + |
| 134 | +/// |
| 135 | + |
| 136 | +/// tab | Linux |
| 137 | + |
| 138 | +```console |
| 139 | +(beeware-venv) $ export PYTHONANYWHERE_USERNAME=your-pa-username |
| 140 | +``` |
| 141 | + |
| 142 | +/// |
| 143 | + |
| 144 | +/// tab | Windows |
| 145 | + |
| 146 | +```doscon |
| 147 | +(beeware-venv) C:\...>set PYTHONANYWHERE_USERNAME=your-pa-username |
| 148 | +``` |
| 149 | + |
| 150 | +/// |
| 151 | + |
| 152 | +or add it to the `pyproject.toml` of your app: |
| 153 | + |
| 154 | +```toml |
| 155 | +[tool.briefcase.app.helloworld.pythonanywhere] |
| 156 | +username = "your-pa-username" |
| 157 | +``` |
| 158 | + |
| 159 | +//// |
| 160 | + |
| 161 | +## Publish |
| 162 | + |
| 163 | +Now, publish the app to PythonAnywhere: |
| 164 | + |
| 165 | +/// tab | macOS |
| 166 | + |
| 167 | +```console |
| 168 | +(beeware-venv) $ briefcase publish web static |
| 169 | +``` |
| 170 | + |
| 171 | +/// |
| 172 | + |
| 173 | +/// tab | Linux |
| 174 | + |
| 175 | +```console |
| 176 | +(beeware-venv) $ briefcase publish web static |
| 177 | +``` |
| 178 | + |
| 179 | +/// |
| 180 | + |
| 181 | +/// tab | Windows |
| 182 | + |
| 183 | +```doscon |
| 184 | +(beeware-venv) C:\...>briefcase publish web static |
| 185 | +``` |
| 186 | + |
| 187 | +/// |
| 188 | + |
| 189 | +The plugin will: |
| 190 | + |
| 191 | +1. Extract the packaged ZIP file. |
| 192 | +2. Upload the contents to `/home/<your-username>/helloworld/` on PythonAnywhere. |
| 193 | +3. Create a webapp (or update the existing one) with a static file mapping pointing `/` to that directory. |
| 194 | +4. Reload the webapp. |
| 195 | + |
| 196 | +Once it's done, your app is live at `https://<your-username>.pythonanywhere.com/`! |
| 197 | + |
| 198 | +## How does this work? |
| 199 | + |
| 200 | +In Tutorial 6, Briefcase started a local web server to serve your static web app. The `publish` command takes the same static files and deploys them to PythonAnywhere's infrastructure instead. |
| 201 | + |
| 202 | +PythonAnywhere serves the files as a static website -- no Python process runs on the server. The Python code in your app runs in the *browser*, using [PyScript](https://pyscript.net), just as it did when you ran it locally. |
| 203 | + |
| 204 | +The plugin uses the [PythonAnywhere API](https://help.pythonanywhere.com/pages/API/) to upload files and configure the webapp, which is why the API token is required. |
| 205 | + |
| 206 | +## Next steps |
| 207 | + |
| 208 | +Your app is now live on the internet. From here you might want to: |
| 209 | + |
| 210 | +* **Use a custom domain** -- configure a `domain` in your `pyproject.toml` to serve the app from your own domain name. See the [Configuration](index.md#configuration) section. |
| 211 | +* **Continue the BeeWare tutorial** -- [Tutorial 7](https://tutorial.beeware.org/en/latest/tutorial/tutorial-7/) covers adding third-party libraries to your app. |
0 commit comments