Flask: How to get up and running in less than 5 minutes

The following instructions are for running Flask on Windows with Python 3 (Anaconda).

To run Flask, you will first need to install Flask in your Python Scripts folder. To do this, run `pip install flask` in a command window. Then confirm that the installation was successful by loading python and giving it the import flask command.

If there is no error when importing Flask, it means that Flask is installed.

Next, create a Flask folder that you will run your Flask app from. For this example I created a ‘flask_test’ folder on my desktop.

Then you will need to create a .py script written for Flask and place it in the folder. Note: After you run this script, Flask will automatically create a __pycache__ folder. For example:

Let’s back up a step and actually create the code in the hello.py script. The following code represents a very basic Flask script:

from flask import Flask

app = Flask(__name__)

@app.route('/')
@app.route('/hello')
def hello_world():
    return 'Hello World!'

@app.route('/esp')
def hola_mundo():
    return 'Hola mundo!'

Now that the code is saved as hello.py and placed in the flask_test folder, we can run it via the following command prompt commands one at a time. Note: for command window, I am using ConEmu (x64), but the cmd.exe that comes with Windows would work also.

    1. cd to the location of your Python installation: cd C:\Users\christon\AppData\Local\Continuum\Anaconda3
    2. Tell Flask what python script you want to run: set FLASK_APP=C:\Users\christon\Desktop\flask_test\hello.py
    3. Turn on Flask debug mode: set FLASK_DEBUG=1
    4. Tell python to run flask: python -m flask runNote: ‘-m’ means module name.

Finally, point your browser at the server location generated by Flask. This can be either http://127.0.0.1:5000 or http://localhost:5000. For example, if you point your browser at default url / or /hello:

If you point your browser at /esp, Flask will route you accordingly:

To recap, here are the steps to get up and running with Flask:

  1. Make sure Flask is installed in your Python Scripts folder using pip install
  2. Confirm that flask is installed in Python using import flask
  3. Create a Flask project folder
  4. Create your .py script and drop it into the flask folder
  5. Run commands to initiate Flask via command prompt
  6. Point your browser at the web server that Flask provides

Notes: When you make changes to your .py script, you should see the changes in your web browser when reloading.

Leave a Reply