簡體   English   中英

如何在 html 頁面中添加 flask 自動索引

[英]How to add flask autoindex in an html page

我想在 HTML 頁面中插入一個 AutoIndex,其中包含更多內容..類似這樣的東西

<html>
<body>
//HTML Content here like IMG, DIV, Table
// Autoindex here
</body>
</html>

我正在使用這樣的自動索引,但它涵蓋了整個頁面

import os.path
from flask import Flask
from flask_autoindex import AutoIndex


app = Flask(__name__)

return AutoIndex(app, browse_root='templates/computer1')

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

我通過執行以下操作來管理這個:

創建您的自定義.html模板

# mytemplate.html
{% extends '__autoindex__/autoindex.html' %}

{% block meta %}
  {{ super() }}
  <link rel="stylesheet"
    href="{{ url_for('static', filename='main.css') }}" />
    <!-- Here you can specify your own .css -->
{% endblock %}

{% block header %}
  <div style="width: 500px; margin: 30px auto;">
    <h2>My Application</h2>
{% endblock %}

{% block footer %}
  </div>
{% endblock %}

(這是所示的模板的文檔,你可以自定義此)

在 python 中,為了在該模板上運行 AutoIndex:

# faicustom.py
from flask import Flask
from flask_autoindex import AutoIndex
app = Flask(__name__)

spath = "/" # Update your own starting directory here, or leave "/" for parent directory

files_index = AutoIndex(app, browse_root=spath, add_url_rules=False)

@app.route('/files')
@app.route('/files/<path:path>')
def autoindex(path='.'):
    return files_index.render_autoindex(path, template='mytemplate.html')
    # Here is where you specify your template

# And if you want:
if __name__ == '__main__':
    app.run()

然后啟動flask應用程序,應該能夠在您自己的自定義站點上運行AutoIndex(在本例中為/files

有關render_autoindex函數的更多信息,請查看注釋源代碼

注意:如果函數的名稱不是autoindex ,它將引發 AssertionError。

無論如何,我也試過這個,但它似乎為我返回了逃脫的 HTML。 為什么?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM