FLASK VE VS CODE ================ 1) nový projekt 2) venv In VS Code, open the Command Palette (View > Command Palette or (Ctrl+Shift+P)). Then select the Python: Create Environment command to create a virtual environment in your workspace. Select venv and then the Python environment you want to use to create it. 3) terminál s aktivovaným venv After your virtual environment creation has been completed, run Terminal: Create New Terminal (Ctrl+Shift+`)) from the Command Palette, which creates a terminal and automatically activates the virtual environment by running its activation script. nebo "activate" nebo 4) nainstalovat Flask python -m pip install flask MINIMÁLNÍ APLIKACE ================== 1) create a new file in your project folder named app.py from flask import Flask app = Flask(__name__) @app.route("/") def home(): return "Hello, Flask!" 2) spustit v terminálu python -m flask run The development server looks for app.py by default. When you run Flask, you should see output similar to the following: (.venv) D:\py\\hello_flask>python -m flask run * Environment: production WARNING: Do not use the development server in a production environment. Use a production WSGI server instead. * Debug mode: off * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) 3) otevřít v prohlížeci Ctrl+click na adresu v terminálu (http://127.0.0.1:5000/) 4) zastavení aplikace v terminálu Ctrl+c --- 5) různé cesty 6) proměnné (DEBUGGER - nepovinné) ====================== 1) aplikace musí být zastavená v terminálu Ctrl+c 2) Switch to the Run and Debug view in VS Code (using the left-side activity bar or Ctrl+Shift+D). click "To customize Run and Debug create a launch.json file". 3) Select Flask from the dropdown 4) (upravit soubor, např. cesta k app.py) 5) přidat breakpoint, F5 (zobrazit stránku s breakpointem) TEMPLATES ========== - The default templating engine for Flask is Jinja, which is installed automatically when you install Flask. 1) Inside the prvni folder, create a folder named templates, which is where Flask looks for templates by default. 2) do main.html Hello, Flask {%if name %} Hello there, {{ name }}! It's {{ date.strftime("%A, %d %B, %Y at %X") }}. {% else %} What's your name? Provide it after /hello/ in the URL. {% endif %} 3) do app.py from flask import render_template ... @app.route("/hello/") @app.route("/hello/") def hello_there(name = None): return render_template( "hello.html", name=name, date=datetime.now() ) CSS ===== 1) vytvořit adresář static 2) uvnitř soubor style.css strong { color: red; } 3) do templatu přidat link na styl