├── .github ├── ISSUE_TEMPLATE.md └── PULL_REQUEST_TEMPLATE.md ├── .gitignore ├── Procfile ├── README.md ├── app.py ├── requirements.txt ├── static └── css │ └── main.css ├── templates ├── base.html ├── index.html └── update.html └── test.db /.github/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | Thanks for checking out my Flask tutorial repo! Since this is a tutorial repository, it's not updated unless a major breaking issue is discovered. 2 | Because of the reason mentioned, please follow the checklist below when opening an issue. 3 | 4 | Make sure to add **all the information needed to understand the problem** so that I can help. If the info is missing I'll add the 'Needs more information' label and close the issue until there is enough information. 5 | 6 | - [ ] Provide a **minimal code snippet** that reproduces the bug. 7 | - [ ] Provide **screenshots** where appropriate 8 | - [ ] What's the **version** of Python you're using? 9 | - [ ] Are you using Mac, Linux or Windows? -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | **IMPORTANT: Please do not create a Pull Request without creating an issue first.** 2 | 3 | *Any change needs to be discussed before proceeding. Failure to do so may result in the rejection of the pull request.* 4 | 5 | Please provide enough information so that others can review your pull request: 6 | 7 | 8 | 9 | Explain the **details** for making this change. What existing problem does the pull request solve? 10 | 11 | 12 | 13 | **Test plan (required)** 14 | 15 | Demonstrate the code is solid. Example: The exact commands you ran and their output, screenshots / videos if the pull request changes UI. 16 | 17 | 18 | 19 | **Code formatting** 20 | 21 | 22 | 23 | **Closing issues** 24 | 25 | Put `closes #XXXX` in your comment to auto-close the issue that your PR fixes (if such). -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | __pycache__/ 2 | env -------------------------------------------------------------------------------- /Procfile: -------------------------------------------------------------------------------- 1 | web: gunicorn app:app -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # FlaskIntroduction 2 | 3 | > This repository has been archived and is no longer being updated. 4 | 5 | ## How To Run 6 | 1. Install `virtualenv`: 7 | ``` 8 | $ pip install virtualenv 9 | ``` 10 | 11 | 2. Open a terminal in the project root directory and run: 12 | ``` 13 | $ virtualenv env 14 | ``` 15 | 16 | 3. Then run the command: 17 | ``` 18 | $ .\env\Scripts\activate 19 | ``` 20 | 21 | 4. Then install the dependencies: 22 | ``` 23 | $ (env) pip install -r requirements.txt 24 | ``` 25 | 26 | 5. Finally start the web server: 27 | ``` 28 | $ (env) python app.py 29 | ``` 30 | 31 | This server will start on port 5000 by default. You can change this in `app.py` by changing the following line to this: 32 | 33 | ```python 34 | if __name__ == "__main__": 35 | app.run(debug=True, port=) 36 | ``` 37 | -------------------------------------------------------------------------------- /app.py: -------------------------------------------------------------------------------- 1 | from flask import Flask, render_template, url_for, request, redirect 2 | from flask_sqlalchemy import SQLAlchemy 3 | from datetime import datetime 4 | 5 | app = Flask(__name__) 6 | app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db' 7 | db = SQLAlchemy(app) 8 | 9 | class Todo(db.Model): 10 | id = db.Column(db.Integer, primary_key=True) 11 | content = db.Column(db.String(200), nullable=False) 12 | date_created = db.Column(db.DateTime, default=datetime.utcnow) 13 | 14 | def __repr__(self): 15 | return '' % self.id 16 | 17 | 18 | @app.route('/', methods=['POST', 'GET']) 19 | def index(): 20 | if request.method == 'POST': 21 | task_content = request.form['content'] 22 | new_task = Todo(content=task_content) 23 | 24 | try: 25 | db.session.add(new_task) 26 | db.session.commit() 27 | return redirect('/') 28 | except: 29 | return 'There was an issue adding your task' 30 | 31 | else: 32 | tasks = Todo.query.order_by(Todo.date_created).all() 33 | return render_template('index.html', tasks=tasks) 34 | 35 | 36 | @app.route('/delete/') 37 | def delete(id): 38 | task_to_delete = Todo.query.get_or_404(id) 39 | 40 | try: 41 | db.session.delete(task_to_delete) 42 | db.session.commit() 43 | return redirect('/') 44 | except: 45 | return 'There was a problem deleting that task' 46 | 47 | @app.route('/update/', methods=['GET', 'POST']) 48 | def update(id): 49 | task = Todo.query.get_or_404(id) 50 | 51 | if request.method == 'POST': 52 | task.content = request.form['content'] 53 | 54 | try: 55 | db.session.commit() 56 | return redirect('/') 57 | except: 58 | return 'There was an issue updating your task' 59 | 60 | else: 61 | return render_template('update.html', task=task) 62 | 63 | 64 | if __name__ == "__main__": 65 | app.run(debug=True) 66 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | Click==7.0 2 | Flask==1.1.2 3 | Flask-SQLAlchemy==2.4.4 4 | gunicorn==19.9.0 5 | itsdangerous==1.1.0 6 | Jinja2==2.11.3 7 | MarkupSafe==1.1.1 8 | SQLAlchemy==1.3.22 9 | Werkzeug==1.0.1 10 | -------------------------------------------------------------------------------- /static/css/main.css: -------------------------------------------------------------------------------- 1 | body, html { 2 | margin: 0; 3 | font-family: sans-serif; 4 | background-color: lightblue; 5 | } 6 | 7 | .content { 8 | margin: 0 auto; 9 | width: 400px; 10 | } 11 | 12 | table, td, th { 13 | border: 1px solid #aaa; 14 | } 15 | 16 | table { 17 | border-collapse: collapse; 18 | width: 100%; 19 | } 20 | 21 | th { 22 | height: 30px; 23 | } 24 | 25 | td { 26 | text-align: center; 27 | padding: 5px; 28 | } 29 | 30 | .form { 31 | margin-top: 20px; 32 | } 33 | 34 | #content { 35 | width: 70%; 36 | } -------------------------------------------------------------------------------- /templates/base.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | {% block head %}{% endblock %} 9 | 10 | 11 | {% block body %}{% endblock %} 12 | 13 | -------------------------------------------------------------------------------- /templates/index.html: -------------------------------------------------------------------------------- 1 | {% extends 'base.html' %} 2 | 3 | {% block head %} 4 | Task Master 5 | {% endblock %} 6 | 7 | {% block body %} 8 |
9 |

Task Master

10 | {% if tasks|length < 1 %} 11 |

There are no tasks. Create one below!

12 | {% else %} 13 | 14 | 15 | 16 | 17 | 18 | 19 | {% for task in tasks %} 20 | 21 | 22 | 23 | 28 | 29 | {% endfor %} 30 |
TaskAddedActions
{{ task.content }}{{ task.date_created.date() }} 24 | Delete 25 |
26 | Update 27 |
31 | {% endif %} 32 | 33 |
34 |
35 | 36 | 37 |
38 |
39 |
40 | {% endblock %} -------------------------------------------------------------------------------- /templates/update.html: -------------------------------------------------------------------------------- 1 | {% extends 'base.html' %} 2 | 3 | {% block head %} 4 | Task Master 5 | {% endblock %} 6 | 7 | {% block body %} 8 |
9 |

Update Task

10 | 11 |
12 |
13 | 14 | 15 |
16 |
17 |
18 | {% endblock %} -------------------------------------------------------------------------------- /test.db: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jakerieger/FlaskIntroduction/3cbc7ca0342c868aeccf12542d7ee8cca9bf266c/test.db --------------------------------------------------------------------------------