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:
- Go to the server ‘/’ location. The program shows an add.html form.
- Add expense info and click the Add button.
- 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.