Python Flask Introduction

Today we will learn something really cool. Its a simple and famous library in Python called Flask. Flask is a web development micro framework for Python. Flask is called a micro framework because it does not require particular tools or libraries.

First let us see how to install Flask.

1. We go to the Windows terminal(command prompt) by entering cmd in the search bar and pressing Enter.

2. Enter ‘pip install Flask’ and press Enter.

3. Flask gets installed on your system.

Today instead of using Python’s IDLE ON Windows, I will be using Linux’s terminal to execute my python program. The steps to be taken to execute a program in python on linux are:

1.Type Terminal in search.The linux terminal appears.

2. Type ‘nano myprogram.py’ and press Enter. GNU Nano is an editor that we are using to write the python code. myprogram.py is my program’s name with .py extension.

3. The editor window appears. Write your code there. To exit the editor press Ctrl+X

4. To run the program enter ‘python myprogram.py’ in the terminal and press Enter.

The trial program we will be seeing is the one below.

screenshot-from-2018-04-09-09-03-48.png

  • First we import Flask.
  • Next we define app as an instance of the flask class.
  • We define the function hello which returns “Hello World!”
  • In the above example, ā€˜/ā€™ URL is bound with hello() function. When the home page is opened in the browser, the output of this function will be displayed.
  • When this code is run, the output will generate a url with a port number as 5000 by default. When this url is typed into your browser as shown below the following result is displayed. 127.0.0.1 is same as your system’s ip also called localhost ip address. Now the flask server is running on your system. To stop the server you need to press Ctrl+C.
  • Hello World! output is displayed.

fg-e1523246930371.png

f.png

  • Now this code will work only on the browser of your computer. Now if you want to run this code and see “Hello World!” on every device (laptop,phone,tablet) connected to your home wifi network,replace app.run() by app. run(host = ‘0.0.0.0’).

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Powered by WordPress.com.

Up ↑

%d bloggers like this: