Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

My-Personal-Journal Completed #74

Merged
merged 1 commit into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions My-Personal-Journal/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Personal Journal

A simple web-based application for maintaining a personal journal. Users can create entries, tag them, and search through past entries by date or keywords. The application is built using Flask and SQLite for a lightweight and efficient experience.

## Features

- **Add New Entries**: Users can add journal entries with mood, content, and tags.
- **Search Entries**: Search through entries using keywords or specific dates.
- **Tag Management**: Create and view tags associated with each entry, and filter entries by tags.
- **User-Friendly Interface**: A clean and professional UI for easy navigation and use.

## Technologies Used

- Python
- Flask
- SQLite
- HTML/CSS

## Installation

1. **Clone the Repository**:
```bash
git clone <repository-url>
cd your_project

2. **Install Required Packages: Make sure you have Python installed (preferably Python 3). Install the required packages using pip**:
```bash
pip install -r requirements.txt

3. **Run the Application: Start the Flask application**:
```bash
python app.py

4. **Access the App: Open your web browser and navigate to http://127.0.0.1:8080 to access the application.**
78 changes: 78 additions & 0 deletions My-Personal-Journal/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
from flask import Flask, render_template, request, redirect, url_for
from journal_app import add_entry, search_entries
from datetime import datetime
import sqlite3

app = Flask(__name__)

# Route to show the home page
@app.route('/')
def index():
return render_template('index.html')

# Route to add a new journal entry
@app.route('/add', methods=['GET', 'POST'])
def add():
if request.method == 'POST':
mood = request.form['mood']
content = request.form['content']
tags = request.form['tags']
add_entry("user_1", mood, content, tags)
return redirect(url_for('index'))
return render_template('add.html')

# Route to search journal entries


@app.route('/search', methods=['GET', 'POST'])
def search():
if request.method == 'POST':
search_input = request.form['search_term']

# Try to parse the input as a date
try:
search_date = datetime.strptime(search_input, '%Y-%m-%d').date()
results = search_entries("user_1", date=search_date)
except ValueError:
# If parsing fails, treat it as a search term
results = search_entries("user_1", search_term=search_input)

return render_template('search_results.html', results=results)

return render_template('search.html')


# Route to list all unique tags
@app.route('/tags')
def list_tags():
conn = sqlite3.connect('journal.db')
cursor = conn.cursor()

# Fetch unique tags from all entries
cursor.execute("SELECT DISTINCT tags FROM journal_entries")
tags_data = cursor.fetchall()
conn.close()

# Flatten the tags and remove duplicates
tags = set()
for row in tags_data:
if row[0]:
tags.update(tag.strip() for tag in row[0].split(','))

return render_template('tags.html', tags=sorted(tags))

# Route to show journal entries by tag
@app.route('/tags/<tag>')
def entries_by_tag(tag):
conn = sqlite3.connect('journal.db')
cursor = conn.cursor()

# Search for entries that contain the selected tag
cursor.execute("SELECT * FROM journal_entries WHERE tags LIKE ?", ('%' + tag + '%',))
results = cursor.fetchall()
conn.close()

return render_template('search_results.html', results=results)

if __name__ == '__main__':
app.run(debug=True, port=8080)
21 changes: 21 additions & 0 deletions My-Personal-Journal/database.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from sqlalchemy.orm import sessionmaker
from models import JournalEntry, Base
from sqlalchemy import create_engine

engine = create_engine('sqlite:///journal.db')
Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
session = Session()

def add_entry(user_id, mood, content, tags):
entry = JournalEntry(user_id=user_id, mood=mood, content=content, tags=tags)
session.add(entry)
session.commit()

def search_entries(user_id, search_term=None, date=None):
query = session.query(JournalEntry).filter(JournalEntry.user_id == user_id)
if search_term:
query = query.filter(JournalEntry.content.contains(search_term))
if date:
query = query.filter(JournalEntry.date == date)
return query.all()
55 changes: 55 additions & 0 deletions My-Personal-Journal/journal_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import sqlite3
from datetime import datetime

DB_NAME = "journal.db"

def connect_db():
return sqlite3.connect(DB_NAME)

def create_table():
with connect_db() as conn:
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS journal_entries (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id TEXT,
date TEXT,
mood TEXT,
content TEXT,
tags TEXT
)
''')
conn.commit()

def add_entry(user_id, mood, content, tags):
with connect_db() as conn:
cursor = conn.cursor()
cursor.execute('''
INSERT INTO journal_entries (user_id, date, mood, content, tags)
VALUES (?, ?, ?, ?, ?)
''', (user_id, datetime.now().strftime('%Y-%m-%d %H:%M:%S'), mood, content, tags))
conn.commit()

def search_entries(user_id, search_term=None, date=None):
with connect_db() as conn:
cursor = conn.cursor()

if search_term:
query = '''
SELECT * FROM journal_entries
WHERE user_id = ? AND (content LIKE ? OR tags LIKE ?)
'''
cursor.execute(query, (user_id, f'%{search_term}%', f'%{search_term}%'))
elif date:
query = '''
SELECT * FROM journal_entries
WHERE user_id = ? AND date(date) = ?
'''
cursor.execute(query, (user_id, date.strftime('%Y-%m-%d')))
else:
return []

return cursor.fetchall()

# Create the journal table at the start
create_table()
8 changes: 8 additions & 0 deletions My-Personal-Journal/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
flask
sqlalchemy
# sqlite3
bcrypt
pandas
matplotlib
# tkinter
flask
Loading
Loading