Adventures in Flask: Building a Simple Python Web App
Phew — close call!
A few hours after publishing this article I noticed a sensitive link had been included in the post that pointed to an internal repository. That link has been removed from this post. If it had been exposed to threat actors, it could have led to attacks against our organization and potentially exposed sensitive internal information. Lesson learned: always double-check links before publishing!
Anyways! If you're just starting with Python web development, Flask is a fantastic place to begin: it's tiny, clear, and gives you full control over how your app is structured. In this post we'll build a small "Notes" app that demonstrates the core Flask concepts: routes, templates, forms, and configuration.
What you'll need
Python 3.8+ installed
pip (Python package manager)
A text editor (VS Code, vim, etc.)
Quick setup
1. Create a virtual environment:
python -m venv venv
source venv/bin/activate # or venv\Scripts\activate on Windows
2. Install Flask:
pip install Flask
3. Project layout (simple):
/notes-app
/templates
index.html
app.py
requirements.txt
Minimal app.py
The smallest Flask app looks like:
from flask import Flask, render_template, request, redirect, url_for
app = Flask(__name__)
# Simple in-memory storage for demo purposes
NOTES = []
@app.route('/', methods=['GET'])
def index():
return render_template('index.html', notes=NOTES)
@app.route('/add', methods=['POST'])
def add_note():
content = request.form.get('content')
if content:
NOTES.append(content)
return redirect(url_for('index'))
if __name__ == '__main__':
app.run(debug=True)
Templates
Create templates/index.html with a form to submit notes and a list to display them. This demonstrates how Flask renders templates and uses request data.
Configuration & Debug Mode
In development you can set FLASK_ENV=development or run app.run(debug=True) for auto-reload and better error pages. Never leave debug enabled on production servers — it can reveal internal information.
Continuing the tutorial
Below are some safe next steps to grow your Flask skills that don't require any external repo:
Add form validation with WTForms or simple server-side checks.
Persist notes to SQLite using SQLAlchemy: define a Note model and perform CRUD.
Add simple authentication with Flask-Login and protect the note-creation route.
Containerize the app with Docker for consistent deployment.
Write unit tests for your routes using Flask's test client.
Comments
Post a Comment