Flask app: flash() won't work

My config: Python 3.9, on Windows 10 Pro. I’m new to Python and have not yet completed a 70 hour tutorial on Python. (I’m using Python 3.9 for the long tutorial I’m doing. I will upgrade Python later.)

I’m making a Flask app and need to show a message box with a message and “OK” button over the client browser window. I have a file templates/base.html which has all the styling which is inherited by other pages.

However flash() is not working. Here’s how the program works:

  1. Go to the server ‘/’ location. The program shows an add.html form.
  2. Add expense info and click the Add button.
  3. The function addexpense() will be called to save the data and show the message.

Here’s my program.

from flask import flash, Flask, redirect, render_template, request
from flask_sqlalchemy import SQLAlchemy
import os
import ctypes

project_dir = os.path.dirname(os.path.abspath(__file__))
database_file = 'sqlite:///{}' . format(os.path.join(project_dir,"mydatabase.db"))
app = Flask(__name__) # Create our app
app.config["SQLALCHEMY_DATABASE_URI"] = database_file
app.secret_key=b'mysecret'
db = SQLAlchemy(app)

# Create db model.
class Expense(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    date = db.Column(db.String(50), nullable=False) # Cannot be null
    expensename = db.Column(db.String(50), nullable=False) # Cannot be null
    amount = db.Column(db.Integer, nullable=False) # Cannot be null
    category = db.Column(db.String(50), nullable=False)

# Routes.
@app.route('/')
def add():
    return render_template('add.html')

@app.route('/addexpense', methods=['POST'])
def addexpense():
    date = request.form['date'] # This is id of the field.
    expensename = request.form['expensename']
    amount = request.form['amount']
    category = request.form['category']
    print(date + ', ' + expensename + ', ' + amount + ', ' + category)
    # Now add values to database. 
    expense = Expense(date=date, expensename=expensename, amount=amount, category=category)
    db.session.add(expense)
    db.session.commit() # Save the data.
    #ctypes.windll.user32.MessageBoxW(0, "Your data has been saved", "Item added", 1)
    flash("Your data has been saved.")
    # Now return to this page/route. 
    return redirect('/')

if __name__ == '__main__':
    print("database=", database_file)
    app.run(debug=True)

The message won’t show with flash() and there is no error on the console or anywhere else.

What am I missing here?

Thank you!

EDIT. If I use ctypes.windll.user32.MessageBoxW(0, "Your data has been saved", "Item added", 1) then the message box appears but it’s behind all other windows. I didn’t find it for a while.

Yeah, don’t do that one :slight_smile: It’s a popup on the server not the client. Useless and extremely annoying.

To be able to flash messages in Flask, you need to have something somewhere that renders your flashed messages. Do you make use of get_flashed_messages() in your add.html template?

Here’s some info on how to use flashed messages: Message Flashing — Flask Documentation (3.0.x)

So I read the docs and added some code to my templates/base.html file, and this prints an error message at the bottom of my form in add.html, but does not show a popup message box windows-style on top of the browser client.

I’m pretty new to Python and Flask and my tutorial has not covered message boxes with Flask yet.

No, it won’t. Flashed messages aren’t like that.

What are you trying to achieve? It might be necessary to rethink from scratch and go back to your goal, and figure out how to get there.

1 Like