Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Can';不要将HTML与Flask一起使用_Python_Flask - Fatal编程技术网

Python Can';不要将HTML与Flask一起使用

Python Can';不要将HTML与Flask一起使用,python,flask,Python,Flask,我正在尝试构建一个基本的Flask web应用程序。当我尝试将简单的html代码(如下所示)添加到编辑器中,然后在终端中“Flask run”时,它给出了“无效语法”。我试过很多不同的html代码,但它一个都不喜欢 from flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): return 'Hello, world' <iframe width="420" height="315" s

我正在尝试构建一个基本的Flask web应用程序。当我尝试将简单的html代码(如下所示)添加到编辑器中,然后在终端中“Flask run”时,它给出了“无效语法”。我试过很多不同的html代码,但它一个都不喜欢

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
return 'Hello, world'

<iframe width="420" height="315"
src="https://www.youtube.com/watch?v=ZEcqHA7dbwM">
</iframe>
从烧瓶导入烧瓶
app=烧瓶(名称)
@应用程序路径(“/”)
def hello_world():
返回“你好,世界”

这段代码完全被破坏了,因为最后的HTML实际上不是有效的Python语法。最简单的方法就是创建一个包含该HTML的python字符串,然后让
hello\u world
函数返回该字符串:

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    html = """<iframe width="420" height="315"
        src="https://www.youtube.com/watch?v=ZEcqHA7dbwM">
        </iframe>
    """
    return html

if __name__ == '__main__':
    app.run(host='0.0.0.0')

请注意,如果您只想提供一个静态HTML页面,那么实际上并不需要Flask,因为它是用于动态内容的。相反,您可以将所有静态HTML文件放在一个目录中,然后运行
python3-mhttp.server
来启动一个简单、轻量级的http服务器,该服务器可以提供静态HTML内容

$ cat <<EOF > index.html
<iframe width="420" height="315"
src="https://www.youtube.com/watch?v=ZEcqHA7dbwM">
</iframe>
EOF

$ python3 -m http.server

$cat你对你刚写的代码有什么想法吗

from flask import Flask
app = Flask(__name__)

html = """<iframe width="420" height="315" src="https://www.youtube.com/watch?v=ZEcqHA7dbwM"></iframe>"""

@app.route('/')
    def hello_world():
    return html
从烧瓶导入烧瓶
app=烧瓶(名称)
html=“”
@应用程序路径(“/”)
def hello_world():
返回html

然后运行
flask run
启动flask服务器

这在技术上是正确的,但现在建议运行flask应用程序的方法是使用
flask
CLI脚本,而不是
app.run
。看见
from flask import Flask
app = Flask(__name__)

html = """<iframe width="420" height="315" src="https://www.youtube.com/watch?v=ZEcqHA7dbwM"></iframe>"""

@app.route('/')
    def hello_world():
    return html