[Python] How to list all image files in Flask static subdirectory?

Discussão em 'Python' iniciado por Stack, Dezembro 17, 2024.

  1. Stack

    Stack Membro Participativo

    def get_path():
    imgs = []

    for img in os.listdir('/Users/MYUSERNAME/Desktop/app/static/imgs/'):
    imgs.append(img)
    image = random.randint(0, len(imgs)-1) #gen random image path from images in directory
    return imgs[image].split(".")[0] #get filename without extension

    @app.route("/blahblah")
    def show_blah():
    img = get_path()
    return render_template('blahblah.html', img=img) #template just shows image


    What I want to do is not have to get the files using os unless there is a way to do it using flask methods. I know this way will only work with my computer and not any server I try and upload it on.

    Continue reading...

Compartilhe esta Página