├── .gitignore ├── README.md ├── app.py └── templates └── base.html /.gitignore: -------------------------------------------------------------------------------- 1 | *.sqlite 2 | 3 | # Byte-compiled / optimized / DLL files 4 | __pycache__/ 5 | *.py[cod] 6 | *$py.class 7 | 8 | # C extensions 9 | *.so 10 | 11 | # Distribution / packaging 12 | .Python 13 | build/ 14 | develop-eggs/ 15 | dist/ 16 | downloads/ 17 | eggs/ 18 | .eggs/ 19 | lib/ 20 | lib64/ 21 | parts/ 22 | sdist/ 23 | var/ 24 | wheels/ 25 | share/python-wheels/ 26 | *.egg-info/ 27 | .installed.cfg 28 | *.egg 29 | MANIFEST 30 | 31 | # PyInstaller 32 | # Usually these files are written by a python script from a template 33 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 34 | *.manifest 35 | *.spec 36 | 37 | # Installer logs 38 | pip-log.txt 39 | pip-delete-this-directory.txt 40 | 41 | # Unit test / coverage reports 42 | htmlcov/ 43 | .tox/ 44 | .nox/ 45 | .coverage 46 | .coverage.* 47 | .cache 48 | nosetests.xml 49 | coverage.xml 50 | *.cover 51 | *.py,cover 52 | .hypothesis/ 53 | .pytest_cache/ 54 | cover/ 55 | 56 | # Translations 57 | *.mo 58 | *.pot 59 | 60 | # Django stuff: 61 | *.log 62 | local_settings.py 63 | db.sqlite3 64 | db.sqlite3-journal 65 | 66 | # Flask stuff: 67 | instance/ 68 | .webassets-cache 69 | 70 | # Scrapy stuff: 71 | .scrapy 72 | 73 | # Sphinx documentation 74 | docs/_build/ 75 | 76 | # PyBuilder 77 | .pybuilder/ 78 | target/ 79 | 80 | # Jupyter Notebook 81 | .ipynb_checkpoints 82 | 83 | # IPython 84 | profile_default/ 85 | ipython_config.py 86 | 87 | # pyenv 88 | # For a library or package, you might want to ignore these files since the code is 89 | # intended to run in multiple environments; otherwise, check them in: 90 | # .python-version 91 | 92 | # pipenv 93 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 94 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 95 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 96 | # install all needed dependencies. 97 | #Pipfile.lock 98 | 99 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 100 | __pypackages__/ 101 | 102 | # Celery stuff 103 | celerybeat-schedule 104 | celerybeat.pid 105 | 106 | # SageMath parsed files 107 | *.sage.py 108 | 109 | # Environments 110 | .env 111 | .venv 112 | env/ 113 | venv/ 114 | ENV/ 115 | env.bak/ 116 | venv.bak/ 117 | 118 | # Spyder project settings 119 | .spyderproject 120 | .spyproject 121 | 122 | # Rope project settings 123 | .ropeproject 124 | 125 | # mkdocs documentation 126 | /site 127 | 128 | # mypy 129 | .mypy_cache/ 130 | .dmypy.json 131 | dmypy.json 132 | 133 | # Pyre type checker 134 | .pyre/ 135 | 136 | # pytype static type analyzer 137 | .pytype/ 138 | 139 | # Cython debug symbols 140 | cython_debug/ -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Simple Flask Todo App using SQLAlchemy and SQLite database. 2 | 3 | For styling [semantic-ui](https://semantic-ui.com/) is used. 4 | 5 | ### Setup 6 | Create project with virtual environment 7 | 8 | ```console 9 | $ mkdir myproject 10 | $ cd myproject 11 | $ python3 -m venv venv 12 | ``` 13 | 14 | Activate it 15 | ```console 16 | $ . venv/bin/activate 17 | ``` 18 | 19 | or on Windows 20 | ```console 21 | venv\Scripts\activate 22 | ``` 23 | 24 | Install Flask 25 | ```console 26 | $ pip install Flask 27 | $ pip install Flask-SQLAlchemy 28 | ``` 29 | 30 | Set environment variables in terminal 31 | ```console 32 | $ export FLASK_APP=app.py 33 | $ export FLASK_ENV=development 34 | ``` 35 | 36 | or on Windows 37 | ```console 38 | $ set FLASK_APP=app.py 39 | $ set FLASK_ENV=development 40 | ``` 41 | 42 | Run the app 43 | ```console 44 | $ flask run 45 | ``` 46 | -------------------------------------------------------------------------------- /app.py: -------------------------------------------------------------------------------- 1 | from flask import Flask, render_template, request, redirect, url_for 2 | from flask_sqlalchemy import SQLAlchemy 3 | 4 | app = Flask(__name__) 5 | 6 | # /// = relative path, //// = absolute path 7 | app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.sqlite' 8 | app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False 9 | db = SQLAlchemy(app) 10 | 11 | 12 | class Todo(db.Model): 13 | id = db.Column(db.Integer, primary_key=True) 14 | title = db.Column(db.String(100)) 15 | complete = db.Column(db.Boolean) 16 | 17 | 18 | @app.route("/") 19 | def home(): 20 | todo_list = Todo.query.all() 21 | return render_template("base.html", todo_list=todo_list) 22 | 23 | 24 | @app.route("/add", methods=["POST"]) 25 | def add(): 26 | title = request.form.get("title") 27 | new_todo = Todo(title=title, complete=False) 28 | db.session.add(new_todo) 29 | db.session.commit() 30 | return redirect(url_for("home")) 31 | 32 | 33 | @app.route("/update/") 34 | def update(todo_id): 35 | todo = Todo.query.filter_by(id=todo_id).first() 36 | todo.complete = not todo.complete 37 | db.session.commit() 38 | return redirect(url_for("home")) 39 | 40 | 41 | @app.route("/delete/") 42 | def delete(todo_id): 43 | todo = Todo.query.filter_by(id=todo_id).first() 44 | db.session.delete(todo) 45 | db.session.commit() 46 | return redirect(url_for("home")) 47 | 48 | if __name__ == "__main__": 49 | db.create_all() 50 | app.run(debug=True) 51 | -------------------------------------------------------------------------------- /templates/base.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Todo App 8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 |

To Do App

16 | 17 |
18 |
19 | 20 |
21 |
22 | 23 |
24 | 25 |
26 | 27 | {% for todo in todo_list %} 28 |
29 |

{{todo.id }} | {{ todo.title }}

30 | 31 | {% if todo.complete == False %} 32 | Not Complete 33 | {% else %} 34 | Completed 35 | {% endif %} 36 | 37 | Update 38 | Delete 39 |
40 | {% endfor %} 41 |
42 | 43 | 44 | --------------------------------------------------------------------------------