Skip to content

Commit 623c270

Browse files
committed
261225
1 parent 92177dc commit 623c270

12 files changed

Lines changed: 413 additions & 0 deletions

File tree

11.Web/Flask/hallo_falk/app.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from flask import Flask, render_template
2+
3+
app = Flask(__name__)
4+
5+
@app.route("/")
6+
7+
def home():
8+
return render_template("index.html")
9+
10+
if __name__ == "__main__":
11+
app.run(debug=True)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html lang="de">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Mein erstes Flask-Projekt</title>
6+
</head>
7+
<body>
8+
<h1>Hallo Falk 👋, willkommen bei Flask!</h1>
9+
</body>
10+
</html>

11.Web/Flask/hello_world/app.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from flask import Flask
2+
3+
app = Flask(__name__)
4+
5+
@app.route("/")
6+
7+
def hello_world():
8+
return "<p>Hello, World!</p>"
9+
10+
if __name__ == "__main__":
11+
app.run(debug=True)
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Simple Flask example app.
4+
5+
Optimized and commented version of the original `HelloWorld.py`.
6+
Provides an application factory (`create_app`) for easier testing and
7+
reads host/port/debug configuration from environment variables.
8+
"""
9+
10+
from __future__ import annotations
11+
import os
12+
from typing import Optional
13+
from flask import Flask
14+
15+
16+
def create_app(debug: bool = False) -> Flask:
17+
"""
18+
Application factory. Returns a configured Flask application.
19+
20+
Args:
21+
debug: Whether to enable Flask debug mode.
22+
23+
Returns:
24+
A configured `Flask` instance with routes registered.
25+
"""
26+
app = Flask(__name__)
27+
app.debug = debug
28+
29+
@app.route("/")
30+
def hello() -> str:
31+
"""Return a friendly greeting for the root path.
32+
33+
Kept intentionally simple for demonstration and tests.
34+
"""
35+
return "Hello, World!"
36+
37+
return app
38+
39+
40+
# Module-level app useful for WSGI servers (e.g., gunicorn) or imports.
41+
app: Flask = create_app()
42+
43+
44+
def _env_bool(name: str, default: bool = False) -> bool:
45+
"""Read an environment variable and interpret common truthy values.
46+
47+
Accepts: '1', 'true', 'yes', 'on' (case-insensitive) as True.
48+
"""
49+
val = os.getenv(name)
50+
if val is None:
51+
return default
52+
return val.lower() in ("1", "true", "yes", "on")
53+
54+
55+
if __name__ == "__main__":
56+
# Configuration from environment variables with safe defaults.
57+
host = os.environ.get("FLASK_RUN_HOST", "127.0.0.1")
58+
port = int(os.environ.get("PORT", "5000"))
59+
debug = _env_bool("FLASK_DEBUG", False)
60+
61+
# Create the app with the chosen debug setting and run it.
62+
app = create_app(debug=debug)
63+
app.run(host=host, port=port, debug=debug)
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Minimal Flask application.
4+
5+
This file provides a small, documented and test-friendly Flask app.
6+
It uses an application factory (`create_app`) and reads runtime
7+
configuration from environment variables to avoid hardcoding debug
8+
settings in the `__main__` block.
9+
"""
10+
11+
from __future__ import annotations
12+
import os
13+
from flask import Flask
14+
15+
16+
def create_app(debug: bool = False) -> Flask:
17+
"""
18+
Create and configure a Flask application.
19+
20+
Args:
21+
debug: Whether to enable Flask debug mode for development.
22+
23+
Returns:
24+
Configured `Flask` application instance.
25+
"""
26+
app = Flask(__name__)
27+
app.debug = debug
28+
29+
@app.route("/")
30+
def hello() -> str:
31+
"""Return a friendly German greeting for the root path."""
32+
return "Hallo von Flask!"
33+
34+
return app
35+
36+
37+
# Module-level application (useful for WSGI servers or imports)
38+
app: Flask = create_app()
39+
40+
41+
def _env_bool(name: str, default: bool = False) -> bool:
42+
"""Interpret common truthy environment variable values as boolean.
43+
44+
Recognised truthy values: '1', 'true', 'yes', 'on' (case-insensitive).
45+
"""
46+
val = os.getenv(name)
47+
if val is None:
48+
return default
49+
return val.lower() in ("1", "true", "yes", "on")
50+
51+
52+
if __name__ == "__main__":
53+
host = os.environ.get("FLASK_RUN_HOST", "127.0.0.1")
54+
port = int(os.environ.get("PORT", "5000"))
55+
debug = _env_bool("FLASK_DEBUG", False)
56+
57+
app = create_app(debug=debug)
58+
app.run(host=host, port=port, debug=debug)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Print the current local date and time.
4+
5+
This script prints a compact ISO-like timestamp and a human-friendly
6+
timestamp including time zone information.
7+
"""
8+
9+
from datetime import datetime
10+
11+
12+
def main() -> None:
13+
"""Get current local time and print two readable formats."""
14+
now = datetime.now().astimezone()
15+
16+
# ISO-like format (date and time) with seconds precision
17+
iso_like = now.isoformat(sep=" ", timespec="seconds")
18+
19+
# Human-friendly format, includes timezone name and offset when available
20+
human = now.strftime("%Y-%m-%d %H:%M:%S %Z%z")
21+
22+
print(iso_like)
23+
print(human)
24+
25+
26+
if __name__ == "__main__":
27+
main()
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#!/usr/bin/env python3
2+
"""
3+
A small Flask app that displays the current local date and time.
4+
5+
Provides an application factory `create_app()` for easy testing and WSGI
6+
integration. The root route `/` renders a simple template showing the
7+
current timestamp.
8+
"""
9+
from __future__ import annotations
10+
import os
11+
from datetime import datetime
12+
from flask import Flask, render_template, jsonify
13+
14+
15+
def create_app(debug: bool = False) -> Flask:
16+
"""Create and configure the Flask application.
17+
18+
Args:
19+
debug: Enable Flask debug mode when True.
20+
21+
Returns:
22+
Configured Flask app instance.
23+
"""
24+
app = Flask(__name__, template_folder="templates")
25+
app.debug = debug
26+
27+
@app.route("/")
28+
29+
def index() -> str:
30+
"""Render the index template with the current datetime."""
31+
now = datetime.now().astimezone()
32+
# ISO-like compact timestamp and human-friendly format
33+
iso_ts = now.isoformat(sep=" ", timespec="seconds")
34+
human_ts = now.strftime("%Y-%m-%d %H:%M:%S %Z%z")
35+
return render_template("index.html", iso=iso_ts, human=human_ts)
36+
37+
@app.route('/api/time')
38+
39+
def api_time():
40+
"""Return the current datetime as JSON (iso and human-readable).
41+
42+
This endpoint is polled by the client every second to update the
43+
displayed time without reloading the page.
44+
"""
45+
now = datetime.now().astimezone()
46+
iso_ts = now.isoformat(sep=" ", timespec="seconds")
47+
human_ts = now.strftime("%Y-%m-%d %H:%M:%S %Z%z")
48+
return jsonify({"iso": iso_ts, "human": human_ts})
49+
50+
return app
51+
52+
53+
# Module-level app for WSGI servers / convenience imports
54+
app: Flask = create_app()
55+
56+
57+
def _env_bool(name: str, default: bool = False) -> bool:
58+
val = os.getenv(name)
59+
if val is None:
60+
return default
61+
return val.lower() in ("1", "true", "yes", "on")
62+
63+
64+
if __name__ == "__main__":
65+
host = os.environ.get("FLASK_RUN_HOST", "127.0.0.1")
66+
port = int(os.environ.get("PORT", "5000"))
67+
debug = _env_bool("FLASK_DEBUG", False)
68+
69+
app = create_app(debug=debug)
70+
app.run(host=host, port=port, debug=debug)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"folders": [
3+
{
4+
"path": "."
5+
}
6+
],
7+
"settings": {
8+
"python-envs.pythonProjects": [
9+
{
10+
"path": "app.py",
11+
"envManager": "ms-python.python:venv",
12+
"packageManager": "ms-python.python:pip"
13+
},
14+
{
15+
"path": "current_datetime.py",
16+
"envManager": "ms-python.python:venv",
17+
"packageManager": "ms-python.python:pip"
18+
},
19+
{
20+
"path": "HelloWorld.py",
21+
"envManager": "ms-python.python:venv",
22+
"packageManager": "ms-python.python:pip"
23+
}
24+
]
25+
}
26+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Flask>=2.0

0 commit comments

Comments
 (0)