Skip to content Skip to sidebar Skip to footer

Reference User Supplied File With Flask App

I am new to Flask and web applications in general. I have images that could be located in different locations: -directory1 -image1 -directory2 -subdirectory -image2 -direc

Solution 1:

app/
   app.py
   ...
uploads/
   dir1/
      img1.png
      ...   
...
app.config['UPLOAD_FOLDER'] = 'path/to/uploads/dir1'

@app.route('/file/<path:filename>')
def img_file(filename):
    filename = filename + '.png'
    return send_from_directory(app.config['UPLOAD_FOLDER'], filename)

@app.route('/', methods=['GET', 'POST'])
def index():
    filename = None
    if request.method == 'POST':
        filename = request.form['img_num']
    return render_template('form.html', filename=filename)

<form method="post" action="" >
    <input type="text" id="form_id" name="img_num">
    <input type="submit" value="submit">
</form>
    {% if filename %}
    <img src=" {{ url_for('img_file', filename=filename) }}">
    {% endif %}

Post a Comment for "Reference User Supplied File With Flask App"