├── .gitignore ├── README.md ├── flashcards.py ├── flashcards_db.json ├── model.py ├── requirements.txt ├── static ├── flasklogo.png └── style.css └── templates ├── add_card.html ├── base.html ├── card.html ├── remove_card.html └── welcome.html /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | __pycache__ 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Flask: Getting Started 2 | Demo code for the course "Flask: Getting Started" on [Pluralsight](https://www.pluralsight.com). 3 | 4 | There's a commit for each module in the course, as well as a tag: 5 | 6 | 7 | - [After module 1: Starting the Project](https://github.com/codesensei-courses/flask-getting-started/releases/tag/project-start) 8 | - [Exercise: A page that shows how many times it has been viewed](https://github.com/codesensei-courses/flask-getting-started/releases/tag/m2-exercise-count-views) 9 | - [After module 2: First Steps](https://github.com/codesensei-courses/flask-getting-started/releases/tag/m2-first-steps) 10 | - [After module 3: Understanding the Model-Template-View Pattern](https://github.com/codesensei-courses/flask-getting-started/releases/tag/m3-model-template-view) 11 | - [Exercise: Add a link to the welcome page](https://github.com/codesensei-courses/flask-getting-started/releases/tag/m4-exercise-link-to-welcome-page) 12 | - [After module 4: Adding Logic to Your Application](https://github.com/codesensei-courses/flask-getting-started/releases/tag/m4-logic) 13 | - [Exercise: Redirect to the card view](https://github.com/codesensei-courses/flask-getting-started/releases/tag/m5-exercise-redirect) 14 | - [Exercise: Write a view that removes a card](https://github.com/codesensei-courses/flask-getting-started/releases/tag/m5-exercise-remove-card) 15 | - [After module 5: Adding User Interaction](https://github.com/codesensei-courses/flask-getting-started/releases/tag/m5-user-interaction) 16 | - [After module 6: Applying Styling and Jinja Inheritance](https://github.com/codesensei-courses/flask-getting-started/releases/tag/m6-styling-and-inheritance) 17 | -------------------------------------------------------------------------------- /flashcards.py: -------------------------------------------------------------------------------- 1 | from flask import (Flask, render_template, abort, jsonify, request, 2 | redirect, url_for) 3 | 4 | from model import db, save_db 5 | 6 | app = Flask(__name__) 7 | 8 | 9 | @app.route("/") 10 | def welcome(): 11 | return render_template( 12 | "welcome.html", 13 | cards=db 14 | ) 15 | 16 | 17 | @app.route('/card/') 18 | def card_view(index): 19 | try: 20 | card = db[index] 21 | return render_template("card.html", 22 | card=card, 23 | index=index, 24 | max_index=len(db)-1) 25 | except IndexError: 26 | abort(404) 27 | 28 | 29 | @app.route('/add_card', methods=["GET", "POST"]) 30 | def add_card(): 31 | if request.method == "POST": 32 | # form has been submitted, process data 33 | card = {"question": request.form['question'], 34 | "answer": request.form['answer']} 35 | db.append(card) 36 | save_db() 37 | return redirect(url_for('card_view', index=len(db)-1)) 38 | else: 39 | return render_template("add_card.html") 40 | 41 | 42 | @app.route('/remove_card/', methods=["GET", "POST"]) 43 | def remove_card(index): 44 | try: 45 | if request.method == "POST": 46 | del db[index] 47 | save_db() 48 | return redirect(url_for('welcome')) 49 | else: 50 | return render_template("remove_card.html", card=db[index]) 51 | except IndexError: 52 | abort(404) 53 | 54 | 55 | @app.route("/api/card/") 56 | def api_card_list(): 57 | return jsonify(db) 58 | 59 | 60 | @app.route("/api/card/") 61 | def api_card_detail(index): 62 | try: 63 | return db[index] 64 | except IndexError: 65 | abort(404) 66 | -------------------------------------------------------------------------------- /flashcards_db.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "question": "Hello (formal)", 4 | "answer": "Здравствуйте" 5 | }, 6 | { 7 | "question": "Goodbye (formal)", 8 | "answer": "До свидания" 9 | }, 10 | { 11 | "question": "Hello (informal)", 12 | "answer": "Привет" 13 | }, 14 | { 15 | "question": "Goodbye (informal)", 16 | "answer": "Пока" 17 | } 18 | ] 19 | -------------------------------------------------------------------------------- /model.py: -------------------------------------------------------------------------------- 1 | """ 2 | model.py 3 | -------- 4 | Implements the model for our website by simulating a database. 5 | 6 | Note: although this is nice as a simple example, don't do this in a real-world 7 | production setting. Having a global object for application data is asking for 8 | trouble. Instead, use a real database layer, like 9 | https://flask-sqlalchemy.palletsprojects.com/. 10 | """ 11 | 12 | import json 13 | 14 | 15 | def load_db(): 16 | with open("flashcards_db.json") as f: 17 | return json.load(f) 18 | 19 | 20 | def save_db(): 21 | with open("flashcards_db.json", 'w') as f: 22 | return json.dump(db, f) 23 | 24 | 25 | db = load_db() 26 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | flask>=1.1.1 -------------------------------------------------------------------------------- /static/flasklogo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codesensei-courses/flask-getting-started/d10bc7dd92b5b6d7cf2adb6c2f11443ccd46570b/static/flasklogo.png -------------------------------------------------------------------------------- /static/style.css: -------------------------------------------------------------------------------- 1 | body { 2 | font-family: sans-serif; 3 | color: cornflowerblue; 4 | background-color: floralwhite; 5 | } 6 | -------------------------------------------------------------------------------- /templates/add_card.html: -------------------------------------------------------------------------------- 1 | {% extends "base.html" %} 2 | 3 | {% block title %}Add Card{% endblock %} 4 | 5 | {% block content %} 6 |

Add a new card

7 |
8 |

9 | Question: 10 | 11 |

12 |

13 | Answer: 14 | 15 |

16 | 17 |
18 | {% endblock %} 19 | -------------------------------------------------------------------------------- /templates/base.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {% block title %}{% endblock %} 6 | 8 | 9 | 10 | {% block content %} 11 | {% endblock %} 12 | 13 | 14 | -------------------------------------------------------------------------------- /templates/card.html: -------------------------------------------------------------------------------- 1 | {% extends "base.html" %} 2 | 3 | {% block title %}Flash Card{% endblock %} 4 | 5 | {% block content %} 6 |

Flash Card

7 | Question: {{ card.question }}
8 | Answer: {{ card.answer }} 9 |

10 | Home 11 | 22 |

23 | Remove this card 24 | {% endblock %} 25 | -------------------------------------------------------------------------------- /templates/remove_card.html: -------------------------------------------------------------------------------- 1 | {% extends "base.html" %} 2 | 3 | {% block title %}Remove Card{% endblock %} 4 | 5 | {% block content %} 6 |

Remove Flash Card

7 | Question: {{ card.question }}
8 | Answer: {{ card.answer }} 9 | 10 |

Are you sure?

11 |
12 | 13 | No 14 |
15 | {% endblock %} 16 | -------------------------------------------------------------------------------- /templates/welcome.html: -------------------------------------------------------------------------------- 1 | {% extends "base.html" %} 2 | 3 | {% block title %}Welcome{% endblock %} 4 | 5 | {% block content %} 6 |

Welcome to my page!

7 |

8 | This is the demo application for the course 9 | Getting Started with Flask 10 | on 11 | Pluralsight. 12 |

13 | 14 |

Flash Cards

15 | 24 | 25 | Add a new card 26 | {% endblock %} 27 | --------------------------------------------------------------------------------