A Flask application is typically structured as follows:
1. Create the application instance:
app = Flask(__name__)
2. Configure the application:
app.config[‘SECRET_KEY’] = ‘your_secret_key_here’
3. Define routes:
@app.route(‘/’)
def index():
return ‘Hello World!’
4. Create the database:
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy(app)
5. Create models:
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True, nullable=False)
email = db.Column(db.String(120), unique=True, nullable=False)
6. Create views:
@app.route(‘/users’)
def show_users():
users = User.query.all()
return render_template(‘users.html’, users=users)
7. Create templates:
Users
{% for user in users %}
{{ user.username }} – {{ user.email }}
{% endfor %}
8. Run the application:
if __name__ == ‘__main__’:
app.run()