Creating a Python App using the Flask framework
From the cPanel SOFTWARE section, click the Setup Python App icon.
Click the + CREATE APPLICATION button. For this tutorial, we are going to create a simple application using the FLASK framework - external link opens in a new window. Complete the fields.
In our example, we will install the application in
/home/krystald/myapp, and serve it from
http://krystaldemo.co.uk/flaskapp, using Python version 3.7.
Once the application has been created, you will be shown the following information, which includes how to switch to the virtual environment for your python application from the command line.
Log into your cPanel account via SSH.
Enter the virtual environment command line above.
[[email protected] ~]$ source /home/krystald/virtualenv/myapp/3.7/bin/activate && cd /home/krystald/myapp
(myapp:3.7)[[email protected] myapp]$
You will notice that the command prompt changes, and is now prefixed with (
myapp:3.7). This tells us that our shell environment is now setup for our application's own python installation. List the files in the current directory, which should look like this.
(myapp:3.7)[[email protected] myapp]$ ls -la
drwxr-xr-x 4 krystald krystald 4096 Oct 16 06:29 .
drwx--x--x 26 krystald krystald 4096 Oct 16 06:29 ..
-rw-r--r-- 1 krystald krystald 147 Oct 16 06:29 passenger_wsgi.py
drwxr-xr-x 2 krystald krystald 4096 Oct 16 06:29 public
-rw-r--r-- 1 krystald krystald 329 Oct 16 06:29 start.py
drwxr-xr-x 2 krystald krystald 4096 Oct 16 06:29 tmp
We can see that our application startup file
start.py has been created. Indeed, if we browse to our URL, in this case
http://krystaldemo.co.uk/flaskapp, we will see a simple message giving the Python version.
This is because cPanel created a basic application startup file
start.py. You may also notice another file,
passenger_wsgi.py. This file is used by Passenger, a web application server that uses the WSGI (Web Server Gateway Interface) convention to provide the glue between your python application and our webservers.
Now we need to install the Python FLASK framework and install our actual FLASK startup file. We can install FLASK , simply with.
$ pip install flask
Once complete, we can now install our basic FLASK startup file.
start.py file using your favourite editor, e.g.:
$ nano start.py
... and edit the contents so they look like this:
from flask import Flask
app = Flask(__name__)
return "Welcome to your FLASK application!\n"
if __name__ == "__main__":
To save the file in Nano, hold
Ctrl-x and confirm the changes.
If all has gone well, we can test the script using the FLASK built in web server.
$ python start.py
* Serving Flask app "start" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
Ctrl-c to shut down the temporary web server, because we actually want our application to be served to the public via Passenger.
Browse again to our application URL, in this case
http://krystaldemo.co.uk/flaskapp, and now you should see the message:
Welcome to your FLASK application!