Hint
Start by setting up your Flask app:
from flask import Flask, render_template, request, redirect, url_for, session
import random
app = Flask(__name__)
app.secret_key = 'your_secret_key' # Required for session handling
Now create a list of flashcards stored as dictionaries with 'front'
and 'back'
keys:
flashcards = [
{'id': 0, 'front': 'What is the capital of France?', 'back': 'Paris'},
{'id': 1, 'front': 'What is the largest planet?', 'back': 'Jupiter'}
]
Next, create the home route (/
) that:
Picks a random flashcard if one isn’t already stored in the session.
Uses
render_template()
to showindex.html
and pass the current card andshow_back
state.
Add two more routes:
/flip
to toggle showing the answer/next
to show a different random card
For adding cards, create a route /add_card
that handles both GET
(show form) and POST
(process form).
Use session like this to store state between requests:
session['current_card_id'] = random.choice(flashcards)['id']
session['show_back'] = False
Once your routes are ready, build the HTML templates (index.html
and add_card.html
) to display the card, flip button, next button, and a form for new cards.