Once you have developed your App, you may want to deploy, i.e. run it on a Webpage. One easy and cheap solution is to run you Flask App on Pythonanywhere (affiliate link). Pythonanywhere (PA) is a service which allows you to run, well, Python Code anywhere. It’s a cloud solution like Microsoft Azure or Amazon AWS for running web apps e.g. with the Flask or Django framework.
Get your Flask App to GitHub
The quickest way to get your app running online is by first uploading it to GitHub as new repository. No need to be afraid, that everyone can now see your deepest coding hacks: make your repository private and no one else will have access to it.
If you already have an account: that’s great! We can move on on the cloud side. If not, you can either create an account + repo now or try with my minimal flask app example.
How to get your Flask App on Pythonanywhere
Go to Pythonanywhere (affiliate link) and create a new account. The free account will do it for (this) simple apps. The beginner account is limited to 100 seconds of CPU time per day, 500 MB of space and a limit in outgoing traffic and sites you can reach.
If you need more than 500 MB of diskspace (sounds much but this includes SQL data and all Python packages needed to run the app) a Hacker account with more space and more CPU can be bought for 5$ monthly.
There is already a tutorial for setting up a Flask project on PA, so I’m not going to copy that. Instead, I’ll show you the steps for the minimal flask app example. If you clone this repo and adapt it to your needs e.g. new endpoints / routes), you can follow the steps one to one and be online in 5 minutes.
Clone the repo on a console
Once you’re done with setting up a new web app (see first steps from PA tutorial) you go to the Web tab and open a new console within the created virtual environment. Please match the Python version of your project (3.8 in my case) with the web app created on PA. As of today, only Python up to 3.8 is supported.
On the terminal you clone the repo you want via
git clone https://github.com/btcorgtfo/flask_app_minimalistic.git
The folder name will be the name of the repo (flask_app_minimalistic
in this case). The absolute folder on the drive however is /home/USERNAME/flask_app_minimalistic
. You need this absolute folder to configure you WSGI (web server gateway interface) file.
Configure WSGI
If you are familiar with emacs
or vi
you can directly edit the file from the terminal. If not, you can use the integrated graphical editor, just go to Dashboard -> Files and browse to
/var/www/USERNAME_pythonanaywhere_com_wsgi.py
Scroll down and uncomment this part here, so that it looks like this:
import sys
## specify "/home/USERNAME/myproject"
path = '/home/USERNAME/flask_app_minimalistic'
if path not in sys.path:
sys.path.append(path)
from wsgi import app as application
Note that the last line is different from the PA tutorial, but it fits to my example for the minimal flask app on Pythonanywhere.
Hint: you can check if everything works e.g. without typos or so by running this file directly with a Python interpreter: python /www/var/ACCOUNT_NAME_pythonanaywhere_com_wsgi.py
Start the Flask App
Starting the App is easy, as it is done via the dashboard. Browse to Web tab and run the app. You should be able to see the “hello world” text on www.USERNAME.pythonanywhere.com
.
If this is not the case, look into the error log or the server log to see where it crashes. If you need to debug, you can use Python’s print()
function and look into the Server log. Not nice, but at least there is a way to debug.
As this ‘app’ is doing nothing at all, you have to likely expand it, e.g.
- Have nice html with bootstrap flask
- Connect to a SQL database and have .e.g. user management
- Protect your pages with a user login and / or user registration form
- Create a simple admin menu
- Integrate a PayPal Subscription Button to Flask project