
Create a web application in Python using Flask
In this article you will learn how to create web applications in Python using the Flask microframework.
Flask is a web application framework written in Python. It enables a web application developer to create applications without having to bother about low-level details such as protocols, thread management etc. In fact, you don’t need to be a Python expert to create a Flask application.
Well begun is half done and the first thing you have to do is to install the proper python package. That is the Flask package. Install it according to your operating system. If you are a windows user, all you have to do is to run the following command in the cmd.
So, let’s see a simple ‘Hello World’ web application written in Flask and then we will discuss about it. A simple web application written in Flask looks like this:
app = Flask(__name__)
@app.route(‘/’)
def hello_world():
return ‘Hello World’ if __name__ == ‘__main__’:
app.run()
Now let’s analyze the script step by step:
1. The first thing you have to do is to import the Flask class.
2. Create an object of Flask class. Flask constructor takes the name of current module (__name__) as argument.
3. The route() function of the Flask class is a decorator, which tells the application which URL should call the associated function.
4. In this case the associated function is just a simple hello_world() function.
5. Finally, the run() method of Flask class runs the application on the local development server.
Running this script, the following message appears in Python shell.
Open the above URL (http://127.0.0.1:5000/) in your browser. ‘Hello World’ message will be displayed on it.
This is the simplest example, based on which we can create a much more complicated web application.
But what if we want to display one or more html pages that we have already created? In this case we will probably have some static files such as pictures, .css files etc. Therefore, it is necessary to create the folders needed for this application. At first, create a folder and name it as you want to, i.e. ‘Application’. Inside this folder we will store the hello.py file. You need two more folders inside the ‘Application’ folder. One for your static files and one for your html files. The names of these two folders must be ‘static’ and ‘templates’.
Below you can see the folder structure of your application.
- Application
- hello.py
- static
- pictures
- css files
- templates
- hello.html
Create the following hello.py file.
from flask import Flask, render_template
app = Flask(__name__)
@app.route(‘/’)
def hello_world():
return render_template(‘hello.html’)
if __name__ == ‘__main__’:
app.run()
The above code is very similar to the simple code that we saw before. The only thing that has changed is that the hello_world() function doesn’t return a string. Instead of that, the hello.html file is rendered by the render_template() function. Don’t forget to import render_template before using it.
Run this python file and visit http://127.0.0.1:5000/ in your browser. Depending on the html file you have chosen, different content will be displayed on your screen.
This is the basic functionality of Flask microframework. Depending on your needs, you can create a simple or a more complicated web application. For further information about Flask’s functionality, feel free to search the web. Python has a very large community and you will definitely find anything you need.
Yours,
Dimosthenis Beleveslis
- Posted by
- On November 14, 2017
0 Comments