Python Flask:如何提供静态 html?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24578330/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:49:00  来源:igfitidea点击:

Flask: How to serve static html?

pythonhtmlwebflask

提问by shuji

I am trying to serve a static html file, but returns a 500 error (a copy of editor.html is on .py and templates directory) This is all I have tried:

我正在尝试提供静态 html 文件,但返回 500 错误(editor.html 的副本位于 .py 和模板目录中)这就是我尝试过的全部内容:

from flask import Flask
app = Flask(__name__, static_url_path='/templates')
@app.route('/')
def hello_world():
    #return 'Hello World1!' #this works correctly!
    #return render_template('editor.html')
    #return render_template('/editor.html')
    #return render_template(url_for('templates', filename='editor.html'))
    #return app.send_static_file('editor.html') #404 error (Not Found)
    return send_from_directory('templates', 'editor.html')

This is the response:

这是回应:

Title: 500 Internal Server Srror

Internal Server Error

The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.

采纳答案by Martijn Pieters

Reducing this to the simplest method that'll work:

将其简化为最简单的方法:

  1. Put static assets into your staticsubfolder.
  2. Leave Flask set to the default, don't give it a static_url_patheither.
  3. Access static content over the pre-configured /static/to verify the file works
  1. 将静态资产放入您的static子文件夹中。
  2. 将 Flask 设置为默认值,也不要给它一个static_url_path
  3. 通过预配置访问静态内容/static/以验证文件是否有效

If you then still want to reuse a static file, use current_app.send_static_file(), and do not use leading /slashes:

如果您仍然想重用静态文件,请使用current_app.send_static_file(),并且不要使用前导/斜杠:

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

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

This looks for the file editor.htmldirectlyinside the staticfolder.

这会editor.html直接static文件夹中查找文件。

This presumes that you saved the above file in a folder that has a staticsubfolder with a file editor.htmlinside that subfolder.

这假定您将上述文件保存在一个文件夹中,该文件夹具有一个static子文件夹,该子文件夹中有一个文件editor.html

Some further notes:

一些进一步的说明:

  • static_url_pathchanges the URLstatic files are available at, not the location on the filesystem used to load the data from.
  • render_template()assumes your file is a Jinja2 template; if it is really just a static file then that is overkill and canlead to errors if there is actual executable syntax in that file that has errors or is missing context.
  • static_url_path更改URL静态文件可用的位置,而不是用于从中加载数据的文件系统上的位置。
  • render_template()假设您的文件是 Jinja2 模板;如果它真的只是一个静态文件,那么这就是矫枉过正,并且如果该文件中的实际可执行语法存在错误或缺少上下文,则可能会导致错误。

回答by Binoy S Kumar

All the answers are good but what worked well for me is just using the simple function send_filefrom Flask. This works well when you just need to send an html file as response when host:port/ApiNamewill show the output of the file in browser

所有的答案都很好,但对我来说效果很好的只是使用send_fileFlask的简单函数。当您只需要发送一个 html 文件作为响应时,当host:port/ApiName将在浏览器中显示文件的输出时,这很有效


@app.route('/ApiName')
def ApiFunc():
    try:
        #return send_file('relAdmin/login.html')
        return send_file('some-other-directory-than-root/your-file.extension')
    except Exception as e:
        logging.info(e.args[0])```