Simple Web Application Using Python flask

/
1 Comments
Python is awesome programming language. Anyone can learn it easily because its syntax is very simple. Because of that Most of scientist are choosing it for their researches. So that variety of packages available for different areas like image processing, machine learning, AI, etc. Building a web application using python is very import because above mentioned libraries are supported for python other than any other programming language.

In this tutorial I am going to show you how to build a simple web application using python. For that I am going to use flask as a web framework. The reason of using flask is it is easy to use and also it is a micro web framework with lot of features. In order to implement the application you should install python for your computer. I am going to use python 2.7 for this tutorial. For Ubuntu Machines python 2.7 is installed by default.

For windows machines,

Download python 2.7.11 using below link and install it to your computer.
https://www.python.org/ftp/python/2.7.10/python-2.7.10.msi

Then you have to configure the environment variables. If you are not aware of it, Use the below link.
http://www.computerhope.com/issues/ch000549.htm
You should add below line to the path variable.
C:\Python27\;C:\Python27\Scripts\        

Now have python on your computer. Then you have to install flask library to your computer. In order to do that you need to install python PIP to your machine.
For Linux users,
sudo apt-get -y install python-pip

For Windows users,
Go to the below link and select all the text in the page. Open the notepad and paste the text into it and save it as "get-pip.py".
https://bootstrap.pypa.io/get-pip.py
After that open the command prompt in the saved location and type,
python get-pip.py

Now you have python PIP on your computer. In order to install python-flask, Open the command prompt and type,
pip install flask

Now you have python flask on your computer. Now we can moving to the implementation part. I have implemented the application and you can clone it or download it using below link.
https://github.com/tharinda221/simple-flask-web-application
When you download the application exact it. Then open command prompt in the file location and type,
python simple-flask-web-application.py

If you followed above steps correctly below screen should be appeared.

According to the figure server is started at port 5000. If you type below URL on your favorite web browser index page will be appeared.


Now I am moving to the implementation. I have used Pycharm 2016 as a IDE. I recommended it if you are building complex applications in python. In the source code folder there is a file called simple-flask-web-application.py which is included all the implementation. I will go through its line by line.

from flask import *

app = Flask(__name__)


@app.route('/')
def hello_world():
    return render_template("index.html")


if __name__ == '__main__':
    app.run(host="localhost", port=5000)

In the first line flask library is imported. Then the second line is to initiate the application. That means create a Flask instance, in your main module. The idea of the first parameter is to give Flask an idea what belongs to your application. This name is used to find resources on the file system, can be used by extensions to improve debugging information and a lot more. The next line is connected a URL rule. Simply we can say when a request came to the "/" URL it will render "index.html" which is located in the template folder. The next line is to start the application. Host and port values are configured in the program itself. Hope you understand the tutorial. Likewise, We can create a web application using python flask in less amount of time and also you can create more advance applications using python.



You may also like

1 comment :

Powered by Blogger.