Skip to main content

Command Palette

Search for a command to run...

Hello World, let's learn Flask Development...

Updated
4 min read
F

We are a bunch of Developers who started to mentor beginners who started using Python and Flask in general in the Flask India Telegram Group. So this blog is a way to give it back to the community from where we have received guidance when we started as beginners.

Introduction:

Hey every beginner Python Developer out there or may I say Developer in general. Let us get started with Flask and explore the world of Python Backend Development in general.

Pre-requisites:

  1. Python 3 (Python 2 reached the End of Life status on January 1, 2020).
  2. Virtualenv (don't want to break your system by installing system-wide dependencies).
  3. Text Editor - (Sublime Text, Visual Studio Code, etc).
  4. Workable Computer with anyone of the following OS (Linux / Mac / Windows).

Let's Get Started:

First, we need to check whether Python 3 is installed in our systems or not. Python3 by default is available in Linux distributions and macOS in general, but not in Windows.

The Download Links for Python 3 are as follows:

https://www.python.org/downloads/

Let's create the Folder and App Pre-requisites:

Note: We use a Linux System

Let's fire up bash and check the Python 3 Version using the command below. Your output should be as follows:

flask-india@dev-pc:~ python3 --version
Python 3.8.10
flask-india@dev-pc:~

Let's create a project folder where we will create our app.

flask-india@dev-pc:~ mkdir basic-flask-app
flask-india@dev-pc:~ cd basic-flask-app
flask-india@dev-pc:~/basic-flask-app$

Setup a Virtual Environment:

Install virtual environment in your system, if not already done. First, install Python3-pip since we can install the virtual environment using the pip command. You can do so using the command below:

flask-india@dev-pc:~/basic-flask-app$ sudo apt install virtualenv
Reading package lists... Done
Building dependency tree       
Reading state information... Done
virtualenv is already the newest version (20.0.17-1ubuntu0.4).
0 upgraded, 0 newly installed, 0 to remove and 137 not upgraded.
flask-india@dev-pc:~/basic-flask-app$

Note - I already have it installed in my system.

Create a virtual environment so that we can install our application dependencies which will remain local to the project setup. Installing them system-wide is not a good practice. Your output should be as follows:

flask-india@dev-pc:~/basic-flask-app$ virtualenv - p python3 app-venv
flask-india@dev-pc:~/basic-flask-app$

Let's activate our virtualenv so that we can install our application dependencies in it. You should see the below output:

flask-india@dev-pc:~/basic-flask-app$ . app-venv/bin/activate
(app-venv) flask-india@dev-pc:~/basic-flask-app

Next, let's install flask since that's what we are going to use.

flask-india@dev-pc:~/basic-flask-app pip install flask
Collecting flask
  Downloading Flask-2.1.2-py3-none-any.whl (95 kB)
     |████████████████████████████████| 95 kB 4.2 MB/s 
Collecting click>=8.0
  Downloading click-8.1.3-py3-none-any.whl (96 kB)
     |████████████████████████████████| 96 kB 8.3 MB/s 
Collecting importlib-metadata>=3.6.0; python_version < "3.10"
  Downloading importlib_metadata-4.11.4-py3-none-any.whl (18 kB)
Collecting Werkzeug>=2.0
  Downloading Werkzeug-2.1.2-py3-none-any.whl (224 kB)
     |████████████████████████████████| 224 kB 6.9 MB/s 
Collecting Jinja2>=3.0
  Downloading Jinja2-3.1.2-py3-none-any.whl (133 kB)
     |████████████████████████████████| 133 kB 7.0 MB/s 
Collecting itsdangerous>=2.0
  Using cached itsdangerous-2.1.2-py3-none-any.whl (15 kB)
Collecting zipp>=0.5
  Using cached zipp-3.8.0-py3-none-any.whl (5.4 kB)
Collecting MarkupSafe>=2.0
  Using cached MarkupSafe-2.1.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (25 kB)
Installing collected packages: click, zipp, importlib-metadata, Werkzeug, MarkupSafe, Jinja2, itsdangerous, flask
Successfully installed Jinja2-3.1.2 MarkupSafe-2.1.1 Werkzeug-2.1.2 click-8.1.3 flask-2.1.2 importlib-metadata-4.11.4 itsdangerous-2.1.2 zipp-3.8.0
flask-india@dev-pc:~/basic-flask-app$

Check the installed dependencies by typing the command below:

(app-venv) flask-india@dev-pc:~/basic-flask-app$ pip freeze
click==8.1.3
Flask==2.1.2
importlib-metadata==4.11.4
itsdangerous==2.1.2
Jinja2==3.1.2
MarkupSafe==2.1.1
Werkzeug==2.1.2
zipp==3.8.0
flask-india@dev-pc:~/basic-flask-app$

Let's save our project dependencies to a file so that we can install our project elsewhere.

(app-venv) flask-india@dev-pc:~/basic-flask-app$ pip freeze > requirements.txt
(app-venv) flask-india@dev-pc:~/basic-flask-app$

Let's create our sample Flask App:

Alright, let's write some code to create our first flask application.

from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello_world():
    return 'Hello World, Flask Dev here...'

if __name__ == '__main__':
    app.run(host='0.0.0.0', debug=True, port=5000)

Save your python file as app.py in the same directory and run your Flask App as follows:

(app-venv) flask-india@dev-pc:~/basic-flask-app$  python app.py 
 * Serving Flask app 'app' (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: on
 * Running on all addresses (0.0.0.0)
   WARNING: This is a development server. Do not use it in a production deployment.
 * Running on http://127.0.0.1:5000
 * Running on http://192.168.0.103:5000 (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 140-536-987

Run our sample Flask App:

You can now test your app by doing a simple curl request to it.

flask-india@dev-pc:~/basic-flask-app$ curl http://localhost:5000
Hello World, Flask Dev here...flask-india@dev-pc:~basic-flask-app$

Conclusion:

We can see it is fairly simple to create a basic Flask App that returns a string response.

More from this blog

flask-india

8 posts