在烧瓶

时间:2020-01-19 22:49:07

标签: python-3.x flask static-files

我有几个文件: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,...)。 如何为每个文件将这些图像加载到flask中。

我的路线是:

@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}

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

2 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:0)

我不知道可以通过“烧瓶”完成任何操作。我可以想到两种方法,无论如何您都想手动执行此操作(验证,授权,安全性)。

但是要回答这个问题,有多种方法可以解决,这取决于您希望解决方案的“神奇程度”。 (我假设,如果您想让烧瓶来处理它,那您就想要魔术)

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

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)