How To Start Web Development With Flask
Tags: Flask, Python

Photo by Halacious on Unsplash

Selecting Python and Flask for web development is the most rewarding experience. One can start with web development using Flask within a few hours.

This article initiates that journey by listing down easy to understand resources on the web with the intentions of providing quick information.

Let us start with understanding the web, internet, web server, etc.

What Is The Web (WWW)?

The World Wide Web, also known as the Web, is an information system.

This information system has a collection of documents and other resources that are identified by a URI/URL (Uniform Resource Indicator/Uniform Resource Locators).

These resources are usually interlinked by hypertext and are accessible over the Internet.

What Is Internet?

A global system of interconnected computer networks to communicate between networks and devices is called the Internet.

The computers and devices on the networks use TCP/IP suite for this communication.

People can keep documents, media, files etc. on this network and also can provide and use services concerning serving these resources, email, and telephony.

What Is A Web application?

Software that runs on a web server ( A computer that is connected to the internet ), which can be accessed by users using their browser is called a web application.

A web application is different than that software that runs on local computers on an Operating System, also called Desktop Applications.

Now that you know about the web, internet, and web application, it is time to begin learning to develop a web application using Flask.

Three Things That You Need To Start Development of a Flask Application

There are only three things required for anyone to start developing a web application with Flask.

A Computer An Internet connection Python Runtime

Three Steps That You Need To Take To Begin Web Development with Flask

Flask can give you superpowers and it is an apt framework for anyone taking their first steps in web development using Python.

Now connect your computer to the internet and then follow these three steps to be the next coveted web developer.

Step 1. Install the required software

On Windows, if you do not already have Python then install it by downloading it from https://www.python.org/. Take the latest version and follow the default steps to install it using the installation wizard.

Note: On Windows, when the installation wizard gives an option of giving the installation path then give a simple and short path. I usually keep it as given below.

D:\Python

Keeping simple and short installation path and on a drive different than C: helps me avoid the step of reinstalling it across system reformat.

If you installed Python on the above path, then Go to Advanced tab under System properties and ensure that you add the below-given paths to the user’s Path variable under environment variables.

D:\Python

D:\Python\Scripts

Now, open a new command prompt and use the below-given command to know to check the successful installation.

python

On Linux, you may not need to install because Python is by default present on almost all Linux distributions.

However, you might not find the latest version present on your system. Moreover, you might find its multiple versions.

On Linux, check various versions of Python using the below command.

whereis python

We are going to use Python version 3 for this and the future articles, so create an alias in .bashrc or .bash_aliases, using the below-given syntax.

alias python=python3

Now, use a new terminal session to check the default python.

python

You will get an output like the one shown below.

$ python

Python 3.8.5 (default, Aug 2 2020, 15:09:07)

[GCC 10.2.0] on linux

Type "help", "copyright", "credits" or "license" for more information.

>>>

Note on Python’s virtual environments : It is good to know about Python’s virtual environments. One might find themselves working on multiple projects at a time.

Therefore, It is good to create a separate virtual environment for each project that one works on.

On both Windows and Linux, the simplest way to create a Python virtual environment is with this command.

python -m venv venv

I usually use this command inside a directory with the name of the project. For example, let us create a directory/folder with the name as learn_flask and then change into that directory before using the above-given command.

Before working on a project you need to activate the virtual environment, using the below-given commands.

On Linux, one needs to use the source command.

source learnflask/venv/bin/activate

On Windows, one can activate using the below command.

learnflask\venv\Scripts\activate

Once the virtual environment is active, all the future installations of Python packages will remain isolated in this virtual environment and will not impact other projects.

Always activate the respective virtual environment before working on a project.

Now let us move ahead and install Flask in the virtual environment. Use the below command to do the same.

pip install Flask

Step 2. Write Your First Flask App

Now that we have installed Python and Flask in a virtual environment, let us write our first Flask App.

Create a Python file inside the project directory learnflask and name it app.py.

Now copy the below-given code into the file and save it.

from flask import Flask app = Flask(__name__) @app.route("/")

def index():

return "Hello from Flask"



In the above-given code, I import Flask from the flask package and create an instance with the name app.

Then I use route decorator to indicate that code inside the index function should be executed when user tries to access the resource at the root URL i.e. http://localhost:5000. This portion of the route is usually replaced by the domain name or Fully Qualified Domain Name when you deploy your application on a production server. More on that in my next tutorials.

To run this smallest Flask app inside your local machine go into the learnflask directory and use the below-given command to run it.

FLASK_ENV=development flask run

The output of the above command is like the one shown below.



* Debug mode: on

* Running on

* Restarting with stat

* Debugger is active!

* Debugger PIN: 139-301-594 * Environment: development* Debug mode: on* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)* Restarting with stat* Debugger is active!* Debugger PIN: 139-301-594

In Flask Debug Mode, the files will automatically reload on every change that you make in the file.

Now navigate to the http://localhost:5000/ using your browser and you will be able to see the Hello Flask message like the one shown below.

Hello from Flask

Step 3. Read Next Tutorials

Step 1 and Step 2 give the required inspiration to begin the journey of doing more with the knowledge.

Stay tuned for my next post in this series — Learn basics of the Flask framework.

Step 4. Read My Quick Guide on Flask

However, you may want to read my quick step by step guide to learning web development with Python and Flask. Following the steps in this book, you can deploy your first application on a free internet-facing computer or a web server over a weekend in your free hours.

I have written this book in a step by step format to keep it simple and easy to understand. It helps in remaining connected to the concepts even when you have only a few minutes to spare.