본문 바로가기
python web framework

direction tree to web with flask, 폴더 트리를 웹에 적용하기, 디렉션 웹에 적용하기

by 타닥타닥 토다토닥 부부 2023. 4. 22.
반응형

direction tree to web with flask, 폴더 트리를 웹에 적용하기, 디렉션 웹에 적용하기 (파이썬, 플라스크)

 

  • 플라스크 앱 활용은 위한 디렉션 구성이 아래와 같다면 ,
├── templates
│    └── main.html
└── app.py
  • app.py 는 아래와 같고
import os
import imghdr
from flask import Flask, render_template

app = Flask(__name__)

@app.route("/<path:path>")
def main(path):
    root_path = f"/home/{path}"
    file_list = os.listdir(root_path)
    display_list = []
    for f in file_list:
        full_path = os.path.join(root_path,f)
        dict_in = {}
        path_name = full_path.replace("/home", "")
        if os.path.isdir(full_path):
            dict_in["type"] = "dir"
            dict_in["name"] = f
            dict_in["path"] = f"'/main{path_name}'"
        else:
            dict_in["type"] = "etc"
            dict_in["name"] = f

        display_list.append(dict_in)
            
    return render_template("main.html", display_list=display_list)
 
 if __name__ == '__main__':
    app.run(host="0.0.0.0", port=1234)
  • main.html 파일은 아래와 같습니다.
<style>
ul {
    list-style-type: none;
    display: grid;
    grid-template-columns: repeat(5, 1fr);
}
</style>
<ul>
    {% for num in range(display_list | length)  %}
        {% if display_list[num]["type"] == "dir" %}
        <li>
            <div style="width:150px; height:150px; border: 1px solid black;">
                <input type="submit" value="{{ display_list[num]['name'] }}" style="width:100%; height:100%;" onClick="location.href={{ display_list[num]['path']}}"></input>
            </div>
        </li>
        {% else %}
        <li>
           <div style="width:150px; height:150px; border: 1px solid black; overflow:hidden; word-break:break-all;">{{ display_list[num]['name'] }}</div>
        </li>
        {% endif %}
    {% endfor %}
</ul>

 

 

반응형

댓글