Reading Time: 3 minutes

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.

Flask_logo
Getting Started With Flask
Note:
The setup instructions are for systems running MacOS or Linux. The code for the web application will work on Windows/MacOS/Linux but the steps for setup and execution will be different for Windows.

Prerequisites

  • 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:

source bin/activate

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
touch flask-tut.py
vim 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
@app.route("/")
def index():
return "Hello from Flask!"
# Run Flask if the __name__ variable is equal to __main__
if __name__ == "__main__":
app.run()

Lastly, execute the file by running:

python3 flask-tut.py

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 knowledgeable advisors tight away!

Series Navigation
<< Previous Article
Avatar for John Long

About the Author: John Long

Latest Articles

How to use kill commands in Linux

Read Article

Change cPanel password from WebHost Manager (WHM)

Read Article

Change cPanel password from WebHost Manager (WHM)

Read Article

Change cPanel password from WebHost Manager (WHM)

Read Article

Change the root password in WebHost Manager (WHM)

Read Article