Created
May 1, 2021 13:06
-
-
Save shinysu/2aaadaa42fd462b677858d2f939950d5 to your computer and use it in GitHub Desktop.
todolist
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, render_template, request, redirect, url_for | |
from dbfunctions import add_new_task, get_complete_tasks, get_incomplete_tasks, mark_task_as_complete | |
app = Flask( | |
__name__, | |
template_folder = 'client/templates' | |
) | |
@app.route("/") | |
def index(): | |
complete = get_complete_tasks() | |
incomplete = get_incomplete_tasks() | |
return render_template('index.html', complete=complete, incomplete=incomplete) | |
@app.route("/add", methods=['POST']) | |
def add(): | |
task = request.form['todoitem'] | |
add_new_task(task) | |
return redirect(url_for('index')) | |
@app.route("/complete/<task>") | |
def complete(task): | |
mark_task_as_complete(task) | |
return redirect(url_for('index')) | |
if __name__ == '__main__': | |
app.run(debug=True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sqlite3 | |
sql_query = """ | |
CREATE TABLE IF NOT EXISTS Todo( | |
id INTEGER PRIMARY KEY, | |
task TEXT, | |
complete BOOLEAN); | |
""" | |
def execute_query(sql_query): | |
with sqlite3.connect('mydb.db') as conn: | |
cur = conn.cursor() | |
cur.execute(sql_query) | |
conn.commit() | |
if __name__ == '__main__': | |
execute_query(sql_query) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sqlite3 | |
def execute_query(sql_query): | |
with sqlite3.connect('mydb.db') as conn: | |
cur = conn.cursor() | |
result = cur.execute(sql_query) | |
conn.commit() | |
return result | |
def add_new_task(task): | |
sql_query = "INSERT INTO Todo(task, complete) VALUES ('%s', %s) " %(task, 0) | |
execute_query(sql_query) | |
def get_incomplete_tasks(): | |
sql_query = "SELECT task FROM Todo WHERE complete=%s " %(0) | |
incomplete = execute_query(sql_query) | |
return [task[0] for task in incomplete.fetchall()] | |
def get_complete_tasks(): | |
sql_query = "SELECT task FROM Todo WHERE complete=%s " %(1) | |
complete = execute_query(sql_query) | |
return [task[0] for task in complete.fetchall()] | |
def mark_task_as_complete(task): | |
sql_query = "UPDATE Todo SET complete=%s WHERE task='%s' and complete=%s " %(1, task, 0) | |
execute_query(sql_query) | |
if __name__ == '__main__': | |
get_incomplete_tasks() | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Todo list</title> | |
</head> | |
<body> | |
<h1> Todo list</h1> | |
<form action="{{ url_for('add') }}" method="POST"> | |
<label> | |
Enter the new item: | |
</label> | |
<input type="text" name="todoitem"> | |
<input type="submit" value="Submit"> | |
</form> | |
<h3> Incomplete tasks </h3> | |
<ul> | |
{% for task in incomplete %} | |
<li> | |
{{ task }} | |
<a href="{{ url_for('complete', task=task) }}"> | |
Mark as complete | |
</a> | |
</li> | |
{% endfor %} | |
</ul> | |
<h3> Complete tasks</h3> | |
<ul> | |
{% for task in complete %} | |
<li>{{ task }}</li> | |
{% endfor %} | |
</ul> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment