0

I'm having a problem with my Flask app where it doesn't update the data fetched from the database when I insert new data. The app only fetches the old data that was fetched when I started the app. No matter how much new data I insert, the fetched data doesn't update until I restart the Flask app by stopping it with ctrl+c and running it again with flask run. I tried disabling the cache like this:

cache = Cache(app, config={'CACHE_TYPE': 'null'})

However, it didn't work. I also checked the user class, the queries, and the functions, and everything seems to be working perfectly and showing results. Additionally, I'm using commit like this:

cursor.execute(query, data)
self.conn.commit()

**Here's my configuration: **

async_mode = None

app = Flask(__name__)

class MyApp:
    def __init__(self):
        self.app = app
        self.app.add_url_rule('/', 'home', self.home,
                              methods=["GET", "POST"])
        self.app.config['DEBUG'] = True
        # Secure session configuration
        self.app.config['SESSION_TYPE'] = 'filesystem'
        self.app.config['SESSION_FILE_DIR'] = '/tmp/flask_session'
        # Whether the session cookie should be secure (i.e., HTTPS only)
        self.app.config['SESSION_COOKIE_SECURE'] = True
        # Prevents JavaScript from accessing the session cookie
        self.app.config['SESSION_COOKIE_HTTPONLY'] = True
        self.app.config['SESSION_PERMANENT'] = False
        self.app.config['SESSION_COOKIE_SAMESITE'] = 'Strict'
        self.app.config['SECURITY_PASSWORD_SALT'] = 'password_salt'
        # 'development', 'production', or 'testing'
        self.app.config['ENV'] = 'development'
        # Disables testing
        self.app.config['TESTING'] = False
        self.app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0

    def run(self):
        self.app.run()

Is there any way to see the update without restarting the Flask app? Any help would be appreciated. Thanks in advance!

I tried disabling the cache like this:

cache = Cache(app, config={'CACHE_TYPE': 'null'})

However, it didn't work. I also checked the user class, the queries, and the functions, and everything seems to be working perfectly and showing results. Additionally, I'm using commit like this:

cursor.execute(query, data)
self.conn.commit()

2 Answers 2

0

You have to use a task for get the database everytime. There is no event in flask (that i know) who change everytime you insert something in database. (if this is the problem, if not, could you be more specific?)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your response! Yes, that is the problem I'm facing. Can you explain how I can use a task or a background process to periodically update my app's data from the database? Also, is there a way to make Flask update the data every time new data is inserted into the database without having to restart the app? Any help would be greatly appreciated.
0

You may not believe it, but insert a print() statement between the code that fetched data and the code that you use and put that data as a parameter.

I confirmed that the problem was solved by inserting the print() statement. *Even if it is not necessarily a print() statement, it will work the same way if you run a function that takes fetched data as a parameter and returns it as it is.

I don't know why it works, and I can't believe this phenomenon. But if you read this answer, please give it a try.

For your information, the code I used is as follows.

@celebration_blueprint.route('/celebration/add_form.html')
def celebration_add_form():
  users = user_service.get_all()
  print(users) #here
  return render_template('celebration_add.html', users=users)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.