
When Data Doesn’t Refresh: Understanding Flask’s State and Persistence | Python Comeback Journey #10
It started with a small confusion: why was “The Great Gatsby” appearing twice? I had edited my books.json file manually to fix an ID issue, but then my app behaved strangely. New entries didn’t appear until I restarted the server. Even stranger — sometimes they did, sometimes they didn’t.
That’s when I began to understand something fundamental about Flask: data stored in memory doesn’t magically stay in sync with what’s stored on disk.
🧩 Step 1: How I Discovered the Issue
At this point, the app already had an /add form that could append new books to books.json. I tried testing it by adding Moby Dick, Frankenstein, and Alice in Wonderland. The flash message appeared — “Book added successfully!” — but when I went back to the homepage, the new titles weren’t there.
It only worked after restarting the Flask server.
That told me something: the homepage wasn’t reloading data fresh from the file each time. Instead, it was using a copy that Flask had kept in memory since startup.
⚙️ Step 2: The Root Cause — Global Variables
Originally, I had something like this at the top of app.py:
|
1 2 |
books = json.load(open(‘data/books.json’)) |
This line runs once, when the app starts — not every time someone visits the page.
That means books stays frozen in memory. So even if the JSON file on disk changes, the running app never knows about it.
🧠 Step 3: The Fix — Load Fresh Data on Each Request
The solution was to move the file reading inside the route function, so Flask loads the data each time / is visited.
Here’s the updated route:
|
1 2 3 4 5 6 7 8 9 10 |
@app.route(‘/’) def home(): current_time = datetime.datetime.now().strftime(‘%Y-%m-%d %H:%M:%S’) # ✅ Load fresh data from file each time with open(‘data/books.json’, ‘r’) as f: books = json.load(f) return render_template(‘home.html’, current_time=current_time, books=books) |
Now, every time the homepage loads, it reads the latest data from disk — no restart needed.
📚 Step 4: The Template Side
The template didn’t need much change. It was already looping through the books list dynamically:
|
1 2 3 4 5 6 7 |
{% for book in books %} <li> <strong>{{ book.title }}</strong> by {{ book.author }} – <em>{{ book.status }}</em> </li> {% endfor %} |
Once the route started passing fresh data, the new entries appeared instantly.
💡 Step 5: The Lesson — Memory vs Persistence
This bug taught me something subtle but important: Flask isn’t a database. When you load data into memory, Flask has no idea if that data changes later. Persistence — whether it’s JSON, SQLite, or PostgreSQL — always lives outside the app’s memory.
If your app needs to reflect real-time data, it must read from (or write to) persistent storage every time.
It’s a small architectural insight that separates toy projects from real ones.
🚀 Reflection
Debugging this was frustrating at first, but ultimately enlightening. I learned that what we think of as a “simple bug” often reveals a deeper truth about how web apps think.
The next step was obvious: if data persistence was this fragile with plain JSON files, maybe it was time to explore proper databases.
Stay tuned for Python Comeback Journey #11: Editing and Deleting Records in Flask Without a Database.





Leave a Reply