├── .gitignore
├── part1
├── templates
│ └── home.html
└── main.py
└── part2
├── templates
├── home.html
└── create_note.html
└── main.py
/.gitignore:
--------------------------------------------------------------------------------
1 | *.sqlite
2 | *.pyc
3 | .DS_Store
4 |
--------------------------------------------------------------------------------
/part1/templates/home.html:
--------------------------------------------------------------------------------
1 |
Note Taking App
2 | View all notes
3 |
4 | Create a note
5 |
--------------------------------------------------------------------------------
/part2/templates/home.html:
--------------------------------------------------------------------------------
1 | Note Taking App
2 | View all notes
3 |
4 | Create a note
5 |
--------------------------------------------------------------------------------
/part2/templates/create_note.html:
--------------------------------------------------------------------------------
1 |
10 |
--------------------------------------------------------------------------------
/part1/main.py:
--------------------------------------------------------------------------------
1 | from flask import Flask, render_template, redirect, request
2 | from flask_sqlalchemy import SQLAlchemy
3 | import os
4 |
5 |
6 | app = Flask(__name__)
7 |
8 | basedir = os.path.abspath(os.path.dirname(__file__))
9 | app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'app.sqlite')
10 | db = SQLAlchemy(app)
11 |
12 |
13 | class Note(db.Model):
14 | id = db.Column(db.Integer, primary_key=True)
15 | title = db.Column(db.String(80))
16 | body = db.Column(db.Text)
17 |
18 | def __init__(self, title, body):
19 | self.title = title
20 | self.body = body
21 |
22 |
23 | @app.route("/")
24 | def home():
25 | return render_template("home.html")
26 |
27 |
28 | if __name__ == "__main__":
29 | app.run(debug=True)
30 |
--------------------------------------------------------------------------------
/part2/main.py:
--------------------------------------------------------------------------------
1 | from flask import Flask, render_template, redirect, request
2 | from flask_sqlalchemy import SQLAlchemy
3 | import os
4 |
5 |
6 | app = Flask(__name__)
7 |
8 | basedir = os.path.abspath(os.path.dirname(__file__))
9 | app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'app.sqlite')
10 | db = SQLAlchemy(app)
11 |
12 |
13 | class Note(db.Model):
14 | id = db.Column(db.Integer, primary_key=True)
15 | title = db.Column(db.String(80))
16 | body = db.Column(db.Text)
17 |
18 |
19 | @app.route("/")
20 | def home():
21 | return render_template("home.html")
22 |
23 | @app.route("/notes/create", methods=["GET", "POST"])
24 | def create_note():
25 | if request.method == "GET":
26 | return render_template("create_note.html")
27 | else:
28 | title = request.form["title"]
29 | body = request.form["body"]
30 |
31 | note = Note(title=title, body=body)
32 |
33 | db.session.add(note)
34 | db.session.commit()
35 |
36 | return redirect("/notes/create")
37 |
38 | if __name__ == "__main__":
39 | app.run(debug=True)
40 |
--------------------------------------------------------------------------------