What is Flask?
Flask is a micro web framework for Python that allows unlimited possibilities to the structure and format for building powerful web applications. This article demonstrates how to get started with Flask using Python 3.7 inside of a virtual environment.
- Python3 Installed
- PIP3 Installed
- Virtualenv Installed
In this article, I will show you how to build a Flask web application and run it locally. Building the application on your local computer can allow you to build and test your application prior to moving it to a production server. Let’s get started.
If virtualenv is not already installed, this can be installed using:
pip3 install virtualenv
First, open a terminal, navigate to the directory you want to create the virtual environment in and create the virtual environment that will be housing the application. A directory will be created at the location you are currently at in your terminal. The directory will have the same name as the virtual environment created.
Run the following command to create the virtual environment:
virtualenv --python=python3 python-tutorial￼
After this, enter the python-tutorial directory and activate the virtual environment with the following command:
Next, install the Flask package using PIP.
pip install flask
Now using your favorite text editor, create a new file inside this directory and name the file flask-tut.py
Add the following code to the file flask-tut.py:
(click the insert key on your keyboard and then copy/paste to insert the code below)
# Import the Flask package
from flask import Flask
# Initialize Flask
app = Flask(__name__)
# Define the index route
return "Hello from Flask!"
# Run Flask if the __name__ variable is equal to __main__
if __name__ == "__main__":
Lastly, execute the file by running:
This starts the development web server. Part of the output when executed provides the address the server is listening on. Navigate to that address in a browser to display the Flask web application just created.
(flask_tutorial) Johns-MBP:flask_tutorial john$ python flask_tut.py
* Serving Flask app "flask_tut" (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)
All requests and response codes will be logged to the output from the running server in the terminal.
Need More info about how you can have your own dev/staging environment to build on?
Our Solutions Team is standing by with real-world application data that can bolster your development or staging environment. With an intimate knowledge of these and other technologies, we can assist in getting you set up and configured to better utilize this platform to improve your dev environment! Give us a call today at 800.580.4985, or open a chat or ticket with us to speak with one of our knowledgable advisors tight away!