This tutorial will show you how to install and configure the Flask micro-framework in a Python virtual environment within a web app.

If you do not have a WebApps section in your hosting Control Panel, then this tutorial is not suitable for your particular hosting environment. You can get in touch with us if you need further assistance.

The official Flask documentation can be found at https://flask.palletsprojects.com/en/2.2.x/.

We will set up a simple Flask environment and use it to run a "Hello World" script.

1. The first step is to create a directory for Flask in the private directory on your account

  • It is strongly recommended that web apps be only deployed in directories within the private directory on the account. We recommend that you create a separate directory there for the app to avoid mixing its contents with other apps or other files in the private directory. You can create the directory using the File Manager in your hosting Control Panel. In this example, the directory is called "flask".
  • Additionally, we will create a "flask" subdomain where our application will be publicly accessibe. You can set up your new subdomain from the Subdomains section of the Control Panel.

2. Create the app using the WebApps section of the hosting Control Panel

  • Choose Custom as engine.
  • Enter a name for your app. It is only for internal reference to the app. In this example, it is Flask.
  • Choose a subdomain at which you wish to access the app. In this example, we choose the "flask" subdomain: flask.mydomain.com.
  • Enter the web access path. For example, if you enter /flask, you will be able to access the app at http://flask.mydomain.com/flask. You do not need to create the directory. In this example, we enter / (forward slash) as web access path, so the app is accessible at http://flask.mydomain.com.
  • The port will be assigned automatically. It will be transparent to your users. You can see the port in the apps list after creating the project. We use 7190 as automatically assigned port in our example.
  • Select the deployment directory. Enter the directory you created in step 1 (/private/flask).
  • We will add the start command of the web app once Flask has been installed and we have prepared our "Hello World" scripts.

Create a new web app

3. Installing Flask

  • Connect to your account via SSH. You can check our online documentation for more information on doing this:

Logging Into Your Account via SSH using Putty
Logging Into Your Account via SSH using Terminal in Mac OS

  • Through your hosting Control Panel > SSH Access section > Additional tools, make sure that the "Compiling tools (gcc, g++)" feature is enabled.
  • On our servers, Python 2 is the primary Python interpreter, so "python" and "pip" commands use Python 2 by default. However, Flask requires Python 3. The path to Python 3 is /usr/bin/python3. The alias for this binary is "python3", and conversely, the Python 3 package installer can be accessed with "pip3". You can add additional custom aliases to the /home/myusername/.bashrc file, where "myusername" is your hosting account username.
  • Your next step is to install Flask. It is time to use Sureapp - our app management CLI tool. Enter the shell of your web app with the following command (replace Flask with the name of your web app):

myusername@s501:/home/myusername$ sureapp project shell Flask

  • Next, run the following command to add a new executables path for this particular project:

myusername@s501 [Flask:custom/-] /home/myusername/private/flask$ echo -e '\n# Additional executables path\nexport PATH=$HOME/.local/bin:$PATH' >> /home/$USER/.bashrc;. /home/$USER/.bashrc

  • Install virtualenv with the following command:

myusername@s501 [Flask:custom/-] /home/myusername/private/flask$ pip3 install virtualenv

  • virtualenv is a virtual environment where you can install software and Python packages in a contained space, which isolates the installed software and packages from the rest of your account's global environment. Multiple virtual environments allow you to run various apps using different versions of software and packages, thus avoiding potential conflicts.
  • You can set up the new virtual environment in the current working directory with the command below:

myusername@s501 [Flask:custom/-] /home/myusername/private/flask$ virtualenv -p /usr/bin/python3 /home/$USER/private/flask

  • Activate the newly created environment:

myusername@s501 [Flask:custom/-] /home/myusername/private/flask$ source bin/activate

  • You will know the virtual env is activated by the new prefix of your shell prompt (flask):

 Activate virtualenv

  • At this point, you can install Flask. Run the following command in the /home/myusername/private/flask directory:

(flask) myusername@s501 [Flask:custom/-] /home/myusername/private/flask$ pip3 install Flask

4. Setting up the Flask "Hello World" scripts

    • To confirm that Flask is installed and running on the account, you should create the following two files in the Flask project directory - hello.py and hello.sh.
      • You can create the hello.py with the following command:

        myusername@s501 [Flask:custom/-] /home/myusername/private/flask$ echo -e 'from flask import Flask\napp = Flask(__name__)\n\[email protected]('/')\ndef hello_world():\n    return "Hello World\!"' > /home/$USER/private/flask/hello.py
        Alternatively, you can create it manually and enter the following code in it:

        from flask import Flask
        app = Flask(__name__)

        @app.route('/')
        def hello_world():
            return 'Hello World!'
      • The following command can be used to create the hello.sh file:

        myusername@s501 [Flask:custom/-] /home/myusername/private/flask$ echo -e '#!/bin/sh\nexport LC_ALL=C.UTF-8\nexport LANG=C.UTF-8\nsource /home/'$USER'/private/flask/bin/activate\nexec env FLASK_APP=/home/'$USER'/private/flask/hello.py flask run --port=$PORT' > /home/$USER/private/flask/hello.sh
        Or you can create the file manually with the following contents, but make sure to replace "myusername" with your hosting account username in it:

        #!/bin/sh
        export LC_ALL=C.UTF-8
        export LANG=C.UTF-8
        source /home/myusername/private/flask/bin/activate
        exec env FLASK_APP=/home/myusername/private/flask/hello.py flask run --port=$PORT
      • When ready, navigate to the WebApps section. Click the Edit button for your Flask web app and enter the following as its start command:

        sh hello.sh
      • Click the Update button to save the changes, and then click the red circle to enable the web app. Click the Refresh button to check if the web app was successfully enabled.

        Flask up and running

      • You can now visit http://flask.mydomain.com in your browser to make sure that Flask is running as expected - if everything has been set up correctly, you should see the "Hello World!" greeting on the page.