如何正确配置路由以仅显示子目录



我无法从子目录下载CSV,但我可以从主目录中完成。

我已经尝试将路线更改为子目录,但是它会引发404错误。

@app.route('/', defaults={'req_path': 'Categorized%20Output/2019-06-27'})
@app.route('/<path:req_path>')
def index(req_path):
    # data = dataset.html
    #return dataset.html
    folderLocation = os.path.dirname(__file__)
    print(folderLocation)
    abs_path = os.path.join(folderLocation,req_path)
    print(abs_path)
    # Return 404 if path doesn't exist
    if not os.path.exists(abs_path):
        return abort(404)
    # Check if path is a file and serve
    if os.path.isfile(abs_path):
        return send_file(abs_path)
    # Show directory contents
    files = os.listdir(abs_path)
    return render_template('index.html',files = files)
if __name__ == "__main__":
    app.run()

我的CSV位于多个子文件夹中,称为文件列表/分类的输出/ lot的日期文件夹/

来自模板的index.html文件

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8" />
  <link rel=stylesheet type=text/css href="{{ url_for('static', filename='css/style.css') }}"/>
  <title>Show CSV</title>
</head>
<body>
  <div class="table">
    <ul>
    {% for file in files %}
    <li><a href="{{ file }}">{{ file }}</a></li>
    {% endfor %}
</ul>
  </div>
</body>

我希望能够从所有不同日期子文件夹中下载CSV。CSV位置的一个示例是:" 127.0.0.1:5000/categorized Output/2019-06-27"

我最近通过使用EasyGui找到了一种方便的方法来获取文件/目录。

pip install easygui
import easygui
@app.route('/')
def index():
    get_a_file = easygui.fileopenbox()
    files = get_a_file # do some stuff with your file
    return render_template('index.html', files=files)

此答案无法解决问题的根源,但是如果它有帮助并且您有兴趣,则可以阅读EasyGui文档。

如果要从特定目录开始,则可以自定义FileOpenbox,例如..

files = easygui.fileopenbox(
                    msg='some message',
                    title='some title', 
                    default='c:some_directorysome_directory*.file_type',
                    filetype='*.file_type'
                    )

file_type是任何类型的文件.. .txt,.dat等,

最新更新