Bootstrap FreeKB - Flask - Getting Started with Flask on Linux
Flask - Getting Started with Flask on Linux

Updated:   |  Flask articles

Typically, VSCode is used to develop a Flask app. However, you can get a Flask app up and running on a Linux system.

On a Debian distribution (Mint, Ubuntu), apt-get can be used to install python-virtualenv. On a Red Hat Distribution (CentOS, Fedora, Red Hat), dnf install or yum install can be used.

dnf install python-virtualenv

 

If using version 2 of Python, the python -m virtualenv <something unique> command can be used to create a virtual environment. In this example, a virtual environment named demo is created.

python -m virtualenv demo

 

If using version 3 of Python, the python3 -m venv <something unique> command can be used to create a virtual environment.

python3 -m venv demo

 

The source command to activate the virtual environment.

source <base directory of your virtual environment>/bin/activate

 

pip install can be used to install Flask in the virtual environment.

(demo) [john.doe@localhost ~]$ pip install flask

 

Create a file named main.py that contains something like this.

from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
    return "Hello World \n"

 

export the FLASK_APP variable.

(demo) [john.doe@localhost ~]$ export FLASK_APP=main.py

 

Run the Flask app.

(demo) [john.doe@localhost ~]$ flask run

 

Something like this should be displayed.

 * Serving Flask app 'main.py' (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)

 

In another Terminal on the same Linux system, the curl command can be used to see if you can get a response from your Flask app.

[john.doe@localhost ~]$ curl http://127.0.0.1:5000/
Hello World

 

Optionally, you could include if __name__ == '__main__': app.run() which allows you to pass in additional options, such as host and port and debug.

from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
    return "Hello World \n"

if __name__ == '__main__':
    app.run(port=12345)

 

And then you can run the Flask app using python.

(demo) [john.doe@localhost ~]$ python main.py
 * Serving Flask app 'main.py' (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:12345/ (Press CTRL+C to quit)

 

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 6e86f9 in the box below so that we can be sure you are a human.