Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add flask autoindex in an html page

I want to insert an AutoIndex inside a HTML page with more content..Something like this

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

I am using autoindex like this, but it covers whole page

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()
like image 553
John Doe Avatar asked Nov 07 '22 15:11

John Doe


1 Answers

I've managed this by doing the following:

Create your customized .html template

# 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 %}

(This is the template shown in the docs, you can customize this)

And in python, in order to run AutoIndex on that template:

# 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()

Then start the flask app and should be able to run AutoIndex on your own custom site (in this case at /files)

For more info on the render_autoindex function, check the commented source code

like image 144
Lucas Mullen Avatar answered Nov 14 '22 20:11

Lucas Mullen