What is Flask?
Flask is a Python Web Server Gateway Interface. That is, Flask is a tool for developing and delivering web sites and web applications.
Flask is light-weight, dynamic, and fairly easy to learn. In this brief tutorial, I’ll describe the steps – from start to finish – of creating a very simple flask website.
Before you can begin building your website or app with Flask, you need to install and configure Python. You can download Python for Windows by doing the following:
1. Navigate to https://www.python.org/downloads/release/python-380/
2. Click the “Windows x86-64 executable installer” link to download Python
3. Launch the executable to begin installation.
4. In the install window, be sure to check “Add Python 3.8 to PATH”
5. Click “Install Now” and wait for the installation to complete
6. Once installation has completed, click “Close” to close out the application.
7. To verify that Python installed correctly, open your command prompt using the “Run as administrator” option as shown below:
8. And enter python –version and hit ENTER. The output should be Python 3.8.0
Along with Python, you’ve installed a command line tool called PIP. Pip allows you to install Python modules through the command line with simple commands.
To ensure that you have pip installed, enter pip into the command line and hit enter. You should see some output as so:
It is often necessary to upgrade Pip after a fresh Python install. Do do so, enter the following command into the command prompt: pip install –upgrade pip
Installing the Virtual Environment
Virtualenv is a Python tool that allows us to create isolated environments that we can use to separate and test our code. When creating a Flask application, it is highly recommended that you use a virtual environment so that any changes you make do not effect your development environment as a whole.
To install virtualenv, simply open a command prompt and enter: pip install virtualenv. You should see output similar to the following:
Now, just to make things a little easier for ourselves, we’re also going to install a virtualenv wrapper for Windows. We do this by entering pip install viertualenvwrapper-win and hitting ENTER. You should see some output similar to the following:
Setting Up Our Project Directory
With all of the prerequisites out of the way, we’re ready to begin setting up our project! We can do that by doing the following.
1. Create the Test environment (you can use another name to fit your project) by entering mkvirtualenv Test. This creates a root directory as Envs\Test that holds our project
2. Enter the test environment any entering cd Envs\Test
3. Set the directory as your current working project by entering setprojectdir .
4. Now install Flask using pip install flask. You should get quite a bit of output from this command, including the following confirmation messages:
Creating the Web Site
With all the setup complete, we are now ready to create a very basic website. To do so, complete the following:
1. Open your favorite text editor, and input the following code (keep in mind that the comments – preceded by a # – are instructive, and not necessary for the app to run):
2. Save the file as test.py in the Envs\Test directory
3. Now, back in your command prompt, enter python test.py. You should see output similar to the following:
4. With that done, you can navigate too 127.0.0.1:5000 in your browser to see the page:
Admittedly, this may not seem very exciting. We went through a lot of work, just to deliver a <h1>Hello Test</h1>. But there is a lot of power and potential behind Flask. You can check out https://appseed.us/apps/flask-apps to see examples of Flask in action.