Model-View-Controller and Unicode
Model-View-Controller (MVC) with Flask & Unicode Essentials
This post introduces the Model-View-Controller (MVC) pattern using Flask and demystifies Unicode encoding for modern software development.
1. MVC Pattern in Flask
MVC divides an application into three components: - Model: Handles data and business logic. - View: Manages UI and presentation. - Controller: Processes user input and updates Model/View.
Example: Flask Counter App
Project Structure
counter_app/
├── app.py
├── models.py
├── templates/
│ ├── index.html
│ └── layout.html
└── static/
└── style.css
Model (models.py
)
class Counter:
def __init__(self):
self.value = 0
def increment(self):
self.value += 1
return self.value
def decrement(self):
self.value -= 1
return self.value
def reset(self):
self.value = 0
return self.value
def get_value(self):
return self.value
View (templates/index.html
)
{% extends "layout.html" %}
{% block content %}
<div class="counter-container">
<h1>Flask Counter App</h1>
<div class="counter-display">{{ count }}</div>
<div class="counter-controls">
<form method="post" action="{{ url_for('increment') }}">
<button type="submit">Increment</button>
</form>
<form method="post" action="{{ url_for('decrement') }}">
<button type="submit">Decrement</button>
</form>
<form method="post" action="{{ url_for('reset') }}">
<button type="submit">Reset</button>
</form>
</div>
</div>
{% endblock %}
Controller (app.py
)
from flask import Flask, render_template, redirect, url_for
from models import Counter
app = Flask(__name__)
counter = Counter()
@app.route('/')
def index():
count = counter.get_value()
return render_template('index.html', count=count)
@app.route('/increment', methods=['POST'])
def increment():
counter.increment()
return redirect(url_for('index'))
@app.route('/decrement', methods=['POST'])
def decrement():
counter.decrement()
return redirect(url_for('index'))
@app.route('/reset', methods=['POST'])
def reset():
counter.reset()
return redirect(url_for('index'))
if __name__ == '__main__':
app.run(debug=True)
Summary:
- Model: Counter logic
- View: HTML templates
- Controller: Flask routes
This separation improves maintainability and scalability.
2. Unicode: The Universal Character Set
What is Unicode?
Unicode assigns a unique number to every character, supporting all major writing systems. It solves compatibility issues from legacy encodings.
Unicode vs. ASCII
- ASCII: 7 bits, 128 characters (mainly English)
- Unicode: Supports all languages and symbols
UTF-8 vs. UTF-16
- UTF-8: 1–4 bytes/character, ASCII-compatible, web standard
- UTF-16: 2 or 4 bytes/character, used by Windows/Java/JS
Character | Description | Code Point | UTF-8 | UTF-16 |
---|---|---|---|---|
A | Latin A | U+0041 | 41 | 0041 |
é | e with acute | U+00E9 | C3 A9 | 00E9 |
π | Greek pi | U+03C0 | CF 80 | 03C0 |
€ | Euro sign | U+20AC | E2 82 AC | 20AC |
你 | Chinese (nǐ) | U+4F60 | E4 BD A0 | 4F60 |
😊 | Smiling face | U+1F60A | F0 9F 98 8A | D83D DE0A |
Unicode in Python
Python 3 strings are Unicode by default:
hello_world = "Hello, World! 你好,世界! こんにちは! 안녕하세요!"
print(hello_world)
print("Euro symbol: \u20AC")
print("Snowman: \u2603")
print("Smiling face: \U0001F60A")
print(ord('A')) # 65
print(ord('€')) # 8364
print(ord('😊')) # 128522
print(chr(65)) # A
print(chr(8364)) # €
print(chr(128522)) # 😊
Key Takeaways
- MVC: Cleanly separates logic, UI, and control flow.
- Unicode: Enables global, multilingual software.
- Python: Unicode support is built-in and straightforward.
Understanding these concepts is essential for building robust, internationalized applications.