Flask Message Flashing

 Flask Message Flashing

All applications and GUI(or the graphic user interfaces) are all about the feedback. If the users do not like the website then they start hating the application. To avoid this situation, an app or website must have enough feedback of the user. In case, the user gets stuck somewhere, through the feedback, he/she can come out from it without getting irritated.

Alert boxes provided in JavaScript and dialog box or message box in Python Framework are used for providing information messages to the user. Similarly, flask also offers a function with the same functionality and this method is known as "Flash()".The flash() function is used to generate informational messages in the flask. It creates an informative message in a view and assigns it to a template view function named next.

It can be defined as a pop-up window or a dialog box that appears on the web page or as a notice in JavaScript, which is used to inform the user. It can be done using the flash () method in Flask. It passes the message to the next template. Flashing message is nothing, but it is a way to provide the feedback to the user for better interaction between the application and user it’ self.  If the user does not get enough feedback from the website or application, the user would not be going to come back on that website.

Example-When the user visits on the website, there is a possibility that the user forgets to fill any field, or he may fill incorrect information. A message will come to render to provide information about what mistake is done by the user, and after that user can correct that mistake and can try again. If all the information is correct, then the user also gets another notification  "registration completed" or "registration successful".

As we discussed above, the role of the feedback is essential for both users and applications.             

Syntax:

flash(message, category)

In the above function, there are two parameters 

  1. Message:-  It is the text message that displayed to the user. It renders just as written without any change.
  2. Category:-  It is optional but very useful because it specifies the message category like error, warning, etc.

The flash () function is used to generate informational messages in the flask. It creates a message in a view and assigns it to a template view function named “next()” function.

Syntax to use this method

get_flashed_messages(with_categories, category_filter) 

It contains two parameters

1. With_category:- It is not essential, and they are used if the category is mentioned in the message. 

2. Category_filter:- It is also optional. It only used for the specific kind of message.

Example

In this example, there are some html files and a server-side script.

Home1.html

<html>  
 <head>  
 <title>home</title>  
 </head>  
 <body>  
 {% with messages = get_flashed_messages() %}  
 {% if messages %}  
 {% for message in messages %}   
 <p>{{ message }}</p>  
 {% endfor %}  
 {% endif %}  
 {% endwith %}  
 <h3>Welcome</h3>  
 <a href = "{{ url_for('login') }}">login</a>  
 </body>  
 </html>  

Loginpage.html

<html>  
 <head>  
 <title>login</title>  
 </head>  
 <body>  
 {% if error %}  
 <p><strong>Error</strong>: {{error}}</p>  
 {% endif %}   
 <form method = "post" action = "/login">  
 <table>  
 <tr><td>Email</td><td><input type = 'email' name = 'email'></td></tr>  
 <tr><td>Password</td><td><input type = 'password' name = 'pass'></td></tr>  
 <tr><td><input type = "submit" value = "Submit"></td></tr>  
 </table>  
 </form>  
 </body>  
 </html> 

This python script flashes a message and redirects the user to different html pages according to event conditions like successful and unsuccessful attempts of the user.

Flashmessage.py

from flask import *  
 app = Flask(__name__)  
 app.secret_key = "abc"  
 @app.route('/index')  
 def home():  
 return render_template("home1.html")   
 @app.route('/login',methods = ["GET","POST"])  
 def login():  
 error = None;  
 if request.method == "POST":  
 if request.form['pass'] != 'akash':  
 error = "invalid password"  
 else:   
 flash("you are successfuly logged in")  
 return redirect(url_for('home'))  
 return render_template('loginpage1.html',error=error)  
 if __name__ == '__main__':  
 app.run(debug = True) 

Now run the server and execute the above code as shown in the screenshot below using the following commands.

  • Py –m  venv env
  • App\scripts\activate
  • Set FLASK_APP=flashmessage.py
  • Flask  run
Flask Message Flashing

Now copy the address ( http://1270.0.:5000/) and paste it into the web browsers and press enter now we can see the output in the form of the web page, as shown in the given screenshot.

Flask Message Flashing

Now click on the login button.

Flask Message Flashing

Fill the details, fill any of them incorrectly, and press enter to see the flash message.

Flask Message Flashing