(base) root@e8cf64ce12e9:/home/continuumio#pip install flask
Html files used by Flask need to be stored in a folder named "templates" in the project folder. Note: It's called a template, but the point is that all Html files used by Flask should be stored here.
.
├── __pycache__
├── hello.py
└── templates
├── hello.html
└── layout.html
Digression: In order to get the above directory structure, you need to install the "tree" command separately. Since the Docker environment was Ubuntu, I was able to install it with "apt install tree". Installation for Mac local seems to require additional work.
The role of each file is as follows --hello.py: Application routing file. <2020/08/09 postscript> It seems that the routing file must always be in this location. I tried to adjust the path of the template to be routed by storing it in another folder, but it didn't work. It may be decided within the framework.
--template / hello.html: An html file that forms part of the screen. In other words, the parts that make up the screen. --templates / layout.html: The html file as the base for all Html files. An image in which the html file as a part like hello.html is incorporated in a part of this file.
layout.html
layout.html
<!DOCTYPE html>
<html>
<head>
<titile>{{ title }}</titile>
</head>
<body>
{% block content %}
<!-- main-->
{% endblock %}
</body>
</html>
-{{val}}: The value of the variable with the same name is reflected in the val part. -{% block content%} ~ {% endblock%}: Image in which the script of {% block content%} ~ {% endblock%} described in the html file as an extension part is inserted in this tag range.
hello.html
hello.html
<!-- layout.Extend html to template-->
{% extends "layout.html" %}
<!-- block content ~ endblock-Is inserted into the same declaration range of the template-->
{% block content %}
<h3>Hello</h3>
Hello.{{ name }}Mr.
{% endblock %}
--Reflect the contents of block content in this html in the html specified by extends. Image of inheritance.
hello.py
hello.py
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def hello():
name = 'TEST'
# return name
return render_template('hello.html', tite="flask test", name=name)
if __name__ == "__main__":
app.run(host='0.0.0.0', port=5000, debug=True)
--By importing "render_template", you can use the functions of Jinja2 template. --render_template ([call html], [[variable name = value passed to template], ...] -"If name ==" main ":" It is OK to recognize that it is a magic when executing a python file from the command line. --Start the application with app_run ([host = 0,0,0,0], [port = XXXX], [debug = XXXX]). There seems to be a way to move the host or port without specifying it here, but I will investigate it.
(base) root@e8cf64ce12e9:/home/continuumio# python hello.py
* Serving Flask app "hello" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: on
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
* Restarting with inotify reloader
* Debugger is active!
* Debugger PIN: 197-268-519
The screen could be displayed.
For the time being, I knew only the minimum usage, but I don't think I would use Flask in practice, and the operating company is Django. Is it worth studying as a stepping stone to the framework?
Recommended Posts