在烧瓶中为每个路由(URL)定义静态文件目录

Define static file directory per route (URL) in flask

我有几个文件:1.html2.html 等,其中包含以下内容:

1.html 有

<h1>Hello</h1>
<img src="1-directory/image.jpg" />

2.html 有

<h1>Hello</h1>
<img src="2-directory/image.jpg" />

等等

因此,每个文件在 <i>-directory/ 中都有一个图像(i 变化:1、2、3,...)。 我如何为每个文件将此图像加载到烧瓶中。

我的路线是:

@app.route('/docs/show/<int:i>')
def show(i):
    with open(i + '.html', 'r') as f:
        content = f.read()
        return render_template('show.html', content = content)

我的模板 show.html

{content | safe}

所以,我想在 flask 中为每个文件设置 <i>-directory,以便显示相应的图像。

我不知道有什么方法可以做到这一点 "in flask"。无论如何,我可以想到您想要手动执行此操作的几种方法(验证、授权、安全)。

但是要回答这个问题,有几种方法可以解决,具体取决于 "magical" 您希望的解决方案。 (我假设如果你想要烧瓶来处理它,那么你就需要魔法)

一种解决方案是将加载的代码段作为新参数注入视图函数的装饰器。

import functools

def inject_snippet(func):
    @functools.wraps(func)
    def wrapper(i, *args, **kwargs):
        filepath = f"{i}-directory/{i}.html"
        with open(filepath) as f:
            snippet = f.read()
            return func(snippet, *args, **kwargs)
    return wrapper


@app.route('/docs/show/<int:i>')
@inject_snippet
def show(snippet):
    return render_template('show.html', content=snippet)

您必须创建静态文件夹,然后您必须添加带有图像的所有文件夹。您可以遵循以下目录结构:

PROJECT NAME
>> static
  >> 1-directory
      >> 1.jpg
  >> 2-directory
      >> 2.jpg
  >> 3-directory
      >> 3.jpg
  >> 4-directory
      >> 4.jpg
>> templates
  >> 1.html
  >> 2.html
  >> 3.html
  >> 4.html
  >> show.html
>> venv folder
>> app.py

您可以将此代码用于 app.py:

import flask

app = flask.Flask(__name__)

@app.route('/docs/show/<string:i>', methods=['GET'])
def show(i):
    with open('templates/' + str(i) + '.html', 'r') as f:
        content = f.read()
    return flask.render_template('show.html', content = content)

if __name__=='__main__':
    app.run('0.0.0.0',port=<your_port_name>)

您可以保留 1.html 如下:

<h1>Hello</h1>
<img src="/static/1-directory/1.jpg" />

您可以保留 2.html 如下:

<h1>Hello</h1>
<img src="/static/2-directory/2.jpg" />

您可以保留 3.html 如下:

<h1>Hello</h1>
<img src="/static/3-directory/3.jpg" />

您可以保留 4.html 如下:

<h1>Hello</h1>
<img src="/static/4-directory/4.jpg" />

并且,您可以在 show.html 中显示您的代码(与您显示代码的方式相同):

<html>
<body>{{content | safe}}</body>
</html>

编辑(根据您的评论):

我已根据您的意见按以下方式创建了文件结构:

PROJECT NAME
>> templates
  >> 1-directory
      >> 1.jpg
  >> 2-directory
      >> 2.jpg
  >> 3-directory
      >> 3.jpg
  >> 4-directory
      >> 4.jpg
  >> 1.html
  >> 2.html
  >> 3.html
  >> 4.html
  >> show.html
>> venv folder
>> app.py

您可以像这样为 app.py 编码:

@app.route('/docs/show/<string:i>', methods=['GET'])
def show(i):
    internal_html = flask.render_template(str(i)+'.html', file_name = str(i)+'-directory/'+str(i)+'.jpg')
    return flask.render_template('show.html', content = internal_html)

@app.route('/serve_file/<path:filename>')
def serve_file(filename):
    return flask.send_from_directory('templates/', filename)

您的所有 HTML 文件将如下所示:

<h1>Hello</h1>
<img src="{{ url_for('serve_file', filename=file_name) }}" />

此外,您的show.html将与上述代码相同。 在这里,我们使用这个 send_from_directory,因为除了 /static 文件夹外,烧瓶不提供服务。因此,对于文件夹的外部使用,我们必须使用 send_from_directory.