Skip to content

Instantly share code, notes, and snippets.

@haa-zee
Created October 30, 2023 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haa-zee/3bd2c79efc3561fe4fc2354462d1e80b to your computer and use it in GitHub Desktop.
Save haa-zee/3bd2c79efc3561fe4fc2354462d1e80b to your computer and use it in GitHub Desktop.
index_by_chatgpt.py
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