<!-- Display Entries (we'll populate this later) -->
<div class="entries mt-5">
<!-- Entries will be displayed here -->
</div>
</div>
<!-- Bootstrap JS and dependencies -->
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
.mood-icon {
position: relative;
margin: 10px;
cursor: pointer;
}
.mood-icon input {
opacity: 0;
position: absolute;
}
.mood-icon i {
font-size: 2em;
color: #ccc;
transition: color 0.3s;
}
.mood-icon input:checked + i,
.mood-icon:hover i {
color: #ffcc00;
}
"Save Entry" clicks saves my data in txt file
Assistant
Sure! Let's enhance your Flask application to save the journal entries, along with the selected mood, into a text file whenever the "Save Entry" button is clicked. We'll also update the HTML template to display the saved entries.
Here's what we'll do:
Update the Flask route to handle POST requests by saving the data to a text file.
Modify the template to display the list of saved entries.
Ensure that the entries are read from the text file and passed to the template for display.
Let's go through each step.
1. Update app.py to Handle Form Submission
We'll update the index route to process the form data when a POST request is received.
python
from flask import Flask, render_template, request, redirect, url_for
import os
app = Flask(__name__)
ENTRIES_FILE = 'entries.txt'
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
# Get data from the form
entry_text = request.form.get('entry')
mood = request.form.get('mood')
# Save the entry to a text file
with open(ENTRIES_FILE, 'a', encoding='utf-8') as f:
f.write(f"{entry_text}|||{mood}\n")
# Redirect to the same page to display the entries
return redirect(url_for('index'))
# For GET request, read the entries from the file
entries = []
if os.path.exists(ENTRIES_FILE):
with open(ENTRIES_FILE, 'r', encoding='utf-8') as f:
for line in f:
if '|||' in line:
entry_text, mood = line.strip().split('|||')
entries.append({'text': entry_text, 'mood': mood})
return render_template('index.html', entries=entries)
if __name__ == '__main__':
app.run(debug=True)
Explanation:
Processing POST Request:
We retrieve the entry and mood from request.form.
We append the new entry to entries.txt, separating the entry text and mood with a delimiter (|||).
Processing GET Request:
We check if entries.txt exists.
We read all entries, split each line into entry_text and mood, and collect them into a list of dictionaries.
Passing Entries to Template:
We pass the entries list to the index.html template for rendering.
2. Update index.html to Display Entries
Let's modify your index.html template to display the saved entries.
your_project/
├── app.py
├── entries.txt # This file will be created automatically after the first entry is saved
├── templates/
│ └── index.html # Make sure the template is named index.html
└── static/
└── css/
└── nb.css
Test the Application
Run the Flask App:
bash
python app.py
Navigate to http://127.0.0.1:5000/ in your browser.
Add a Journal Entry:
Write some text in the "Write your thoughts" textarea.
Select a mood by clicking on one of the mood icons.
Click "Save Entry".
View Saved Entries:
After submission, the page will refresh.
Scroll down to see the list of previous entries displayed on the page.
Additional Improvements
Handle Special Characters:
Ensure that special characters in the entry text are properly handled by encoding or escaping them.
Since we're using a text file, make sure to handle newlines and delimiters carefully.
Use a Database for Production:
For a real-world application, consider using a database (like SQLite) instead of a text file.
Databases handle concurrent writes and data integrity better than text files.
Enhance Entry Display:
Format the display of entries to include timestamps.
Use icons to represent moods visually.
Add Entry Deletion:
Implement functionality to delete entries.
This would require updating the storage mechanism to support entry identification.
Conclusion
By updating your Flask application and the HTML template, you now have a functional journal app that saves entries to a text file and displays them. This should meet your requirement of saving data when the "Save Entry" button is clicked.
Feel free to customize and expand the functionality as needed!