diff --git a/To-Do-List/README.md b/To-Do-List/README.md new file mode 100644 index 000000000..a7721732f --- /dev/null +++ b/To-Do-List/README.md @@ -0,0 +1,10 @@ +# To-Do List +A to-do list application made using Python's flask framework + +# Usage +Run the script in the terminal and access http://localhost:5000 in your web browser to view the application + +# Requirements +- install the flask module as: pip install flask +- install the flask_sqlalchemy module as: pip install flask_sqlalchemy +- install sqlite if required diff --git a/To-Do-List/app.py b/To-Do-List/app.py new file mode 100644 index 000000000..310807b10 --- /dev/null +++ b/To-Do-List/app.py @@ -0,0 +1,70 @@ +from flask import Flask, render_template, request, flash, redirect +from flask_sqlalchemy import SQLAlchemy +from datetime import datetime + +app = Flask(__name__) +app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite://' +app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False +app.secret_key = 'any random secret' +db = SQLAlchemy(app) + + +class TODO(db.Model): + id = db.Column(db.Integer, primary_key=True) + content = db.Column(db.String(200), nullable=False) + date_created = db.Column(db.DateTime, default=datetime.utcnow) + + def __repr__(self): + return '' % self.id + + +@app.route('/', methods=['POST', 'GET']) +def hello(): + db.create_all() + if request.method == 'POST': + task = request.form['content'] + new_task = TODO(content=task) + try: + db.session.add(new_task) + db.session.commit() + flash('Successfully added!') + return redirect('/') + except Exception: + flash('An error occurred') + return redirect('/') + else: + tasks = TODO.query.order_by(TODO.date_created).all() + return render_template("toDoListTemplate.html", tasks=tasks) + + +@app.route('/delete/') +def delete(id): + task = TODO.query.get_or_404(id) + try: + db.session.delete(task) + db.session.commit() + flash('Successfully deleted') + return redirect('/') + except Exception: + flash('An error occurred') + return redirect('/') + + +@app.route('/modify/', methods=['POST', 'GET']) +def modify(id): + task = TODO.query.get_or_404(id) + if request.method == 'POST': + task.content = request.form['content'] + try: + db.session.commit() + flash("Successfully updated") + return redirect('/') + except Exception: + flash("An error occurred") + return redirect('/') + else: + return render_template("toDoListTemplate.html", tasks=task) + + +if __name__ == '__main__': + app.run(debug=True) diff --git a/To-Do-List/static/styles.css b/To-Do-List/static/styles.css new file mode 100644 index 000000000..ea478413e --- /dev/null +++ b/To-Do-List/static/styles.css @@ -0,0 +1,19 @@ +.content{ + text-align: center; + display: inline-block; + overflow-x: auto; +} +body{ + font-family:Abel; + text-align: center; + background-color:azure; +} +#modifyTask{ + text-align:left; +} +a:link { + color: blue; +} +a:visited{ + color: blue; +} diff --git a/To-Do-List/templates/toDoListTemplate.html b/To-Do-List/templates/toDoListTemplate.html new file mode 100644 index 000000000..9c40575fa --- /dev/null +++ b/To-Do-List/templates/toDoListTemplate.html @@ -0,0 +1,91 @@ + + + + {% block head %} + + + + + + + + {% endblock %} + + + {% block body %} +
+ {% with msg=get_flashed_messages() %} + {% if msg %} + {% for message in msg %} + {{ message }} +
+ {% endfor %} + {% endif %} + {% endwith %} +

To-Do List

+
+
+ + +
+
+ + {% if tasks %} + + + + + + {% for task in tasks %} + + + + + + + {% endfor %} + {% else %} + No items in list + {% endif %} +
Task Added Actions
{{ task.content }} {{ task.date_created.date() }} + Delete +
+ Update +
+
+
+
+ + {% endblock %} + +