This tutorial picks up where Tutorial 6 - Put it on the web! 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.
/// admonition | Prerequisites type: info
Before starting, make sure you have:
- Completed the BeeWare tutorial through Tutorial 6 (or at least run
briefcase create web staticandbriefcase build web static) - A PythonAnywhere account
- An API token from your Account page
///
With your BeeWare tutorial virtual environment active, install the plugin:
/// tab | macOS
(beeware-venv) $ pip install pythonanywhere-briefcase-plugin///
/// tab | Linux
(beeware-venv) $ pip install pythonanywhere-briefcase-plugin///
/// tab | Windows
(beeware-venv) C:\...>pip install pythonanywhere-briefcase-plugin
///
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:
/// tab | macOS
(beeware-venv) $ briefcase package web static
[helloworld] Packaging Hello World...
...
[helloworld] Packaged dist/Hello World-0.0.1.web.zip///
/// tab | Linux
(beeware-venv) $ briefcase package web static
[helloworld] Packaging Hello World...
...
[helloworld] Packaged dist/Hello World-0.0.1.web.zip///
/// tab | Windows
(beeware-venv) C:\...>briefcase package web static
[helloworld] Packaging Hello World...
...
[helloworld] Packaged dist\Hello World-0.0.1.web.zip
///
This creates a ZIP file containing all the static files needed to serve your app.
The plugin needs your PythonAnywhere API token to upload files and configure the webapp. Set it as an environment variable:
/// tab | macOS
(beeware-venv) $ export API_TOKEN=your-api-token-here///
/// tab | Linux
(beeware-venv) $ export API_TOKEN=your-api-token-here///
/// tab | Windows
(beeware-venv) C:\...>set API_TOKEN=your-api-token-here
///
Replace your-api-token-here with the token from your Account page.
//// admonition | Username type: note
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.
If you're running this from a PythonAnywhere console, the username is detected automatically -- no extra configuration needed.
If you're running locally and your PythonAnywhere username is different from your local system username, either set the environment variable:
/// tab | macOS
(beeware-venv) $ export PYTHONANYWHERE_USERNAME=your-pa-username///
/// tab | Linux
(beeware-venv) $ export PYTHONANYWHERE_USERNAME=your-pa-username///
/// tab | Windows
(beeware-venv) C:\...>set PYTHONANYWHERE_USERNAME=your-pa-username
///
or add it to the pyproject.toml of your app:
[tool.briefcase.app.helloworld.pythonanywhere]
username = "your-pa-username"////
Now, publish the app to PythonAnywhere:
/// tab | macOS
(beeware-venv) $ briefcase publish web static///
/// tab | Linux
(beeware-venv) $ briefcase publish web static///
/// tab | Windows
(beeware-venv) C:\...>briefcase publish web static
///
The plugin will:
- Extract the packaged ZIP file.
- Upload the contents to
/home/<your-username>/helloworld/on PythonAnywhere. - Create a webapp (or update the existing one) with a static file mapping pointing
/to that directory. - Reload the webapp.
Once it's done, your app is live at https://<your-username>.pythonanywhere.com/!
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.
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, just as it did when you ran it locally.
The plugin uses the PythonAnywhere API to upload files and configure the webapp, which is why the API token is required.
Your app is now live on the internet. From here you might want to:
- Use a custom domain -- configure a
domainin yourpyproject.tomlto serve the app from your own domain name. See the Configuration section. - Continue the BeeWare tutorial -- Tutorial 7 covers adding third-party libraries to your app.