Created
October 30, 2023 14:15
-
-
Save haa-zee/3bd2c79efc3561fe4fc2354462d1e80b to your computer and use it in GitHub Desktop.
index_by_chatgpt.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from flask import Flask, render_template, request, send_file | |
app = Flask(__name__) | |
@app.route('/') | |
def index(): | |
dir_path = os.path.abspath('.') | |
contents = os.listdir('.') | |
files = [] | |
directories = [] | |
for item in contents: | |
full_path = os.path.join(dir_path, item) | |
if os.path.isdir(full_path): | |
directories.append(item + '/') | |
else: | |
files.append(item) | |
return render_template('index.html', directories=directories, files=files) | |
@app.route('/<path:path>') | |
def navigate(path): | |
dir_path = os.path.abspath(path) | |
contents = os.listdir(dir_path) | |
files = [] | |
directories = [] | |
for item in contents: | |
full_path = os.path.join(dir_path, item) | |
if os.path.isdir(full_path): | |
directories.append(item + '/') | |
else: | |
files.append(item) | |
return render_template('index.html', directories=directories, files=files) | |
@app.route('/<path:filename>') | |
def download(filename): | |
return send_file(filename) | |
if __name__ == '__main__': | |
app.run(debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment