(For myself) Flask_2 (list and for, extends, and a little more)

item

  1. Pass the list created in py to html
  2. python format for statement in html
  3. Omit the beginning of html with ʻextends or block content`
  4. Miscellaneous notes

1. Pass the list created in py to html

from flask import Flask, render_template
app = Flask(__name__)

@app.route("/")
def hello_world():
    name = "Flask"
    players = ["Brave", "Warrior", "Wizard"]
    return render_template("index.html", name_value = name, players = players)

--Create the list itself as usual --The way to pass to html is the same as other variables

2. python format for statement in html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    {% for player in players: %}
        <p>{{ player + "Fought monsters" }}</p>
    {% endfor %}
</body>
</html>

--The for statement itself is enclosed in {% ~~%} as in the previous if statement. --Finally, insert {% endfor%} and tighten

3. Omit the beginning of html with ʻextends or block content`

When ʻindex.html` is referenced on the py side

index.html


{% extends "layout.html" %}
{% block content %}
    <h1>aiueo</h1>
{% endblock %}

layout.html


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    {% block content %}
    {% endblock %}
</body>
</html>

--For the html you want to use as a template source, put {% endblock%} immediately after {% block content%} in <body> --The html you want to use the template is {% extends" ~~ .html "%}, and select the person to refer to. --After that, delete the tags such as <head> and <body>, and put what you want to enter instead in {% block content%} and {% endblock%}. --The image of what you typed in ʻindex was transferred to {% block content%} in layoutthrough the warp gate{% block content%}`.

4. Miscellaneous notes

--For items displayed in a similar format, it will be easier if you combine them into one on the html side and change variables and paths as shown below on the py side.

python


player = "player"

@app.route("/walk")
def walk():
    message = player + "Was walking in the wilderness."
    return render_template("action.html", player = player, message = message)

@app.route("/attack")
def attack():
    message = player + "Fought a monster."
    return render_template("action.html", player = player, message = message)

html


{% extends "layout.html" %}
{% block content %}
    <h1>{{ player }}Action</h1>
    <p>{{ message }}</p>
{% endblock %}

――I can't think of anything like this until I get used to it

5. At the end

--Flask I have no time to do it, I expect it on weekends and next week

Recommended Posts

(For myself) Flask_2 (list and for, extends, and a little more)
(For myself) Flask_3 (form, POST and GET)
[Python] Create a date and time list for a specified period
A little more about references ~ Using Python and Java as examples ~
A little script for malware self-defense
(For myself) Flask_ex (templates and static)
(For myself) Flask_5 (Add to txt file)
A little scrutiny of pandas 1.0 and dask
Impressions of using Flask for a month
Change the list in a for statement
(For myself) Put Flask in VS Code
[Python] Create a list of date and time (datetime type) for a certain period
Receives and processes n objects in a list
Launch a web server with Python and Flask
A memorandum for touching python Flask on heroku
(For myself) AWS_Flask_3 (Install / Run Flask on AWS)