简体   繁体   中英

how to add an image to webapp using Python flask

I need to develop a web app using python flask and I need to add an image to the view page. But I couldn't get it.please help me out of it.

In the example below the image is stored in an SQLite database. From there you can do something like:

@app.route('/show')
def show_pic():
    filename = request.args.get('filename','')
    t = (filename,)
    cur = g.db.execute('select label from pics where filename=?', t)
    label = cur.fetchone()[0]

    return render_template('upload.html', filename=filename, label=label)

And then in your template:

{% macro pic_path(pic) %}
    {{ url_for('return_pic', filename='%s' % pic) }}
{% endmacro %}

{% block content %}
    {% if filename is defined %}
        <div id="image">
            <a href="{{ pic_path(filename) }}" target="_blank">
                <img class="new" src="{{ pic_path(filename) }}">
            </a>
        </div>
    {% else %}
        <ul>
        {% for pic in pics %}
            <li class="thumb">
                <a href="{{ url_for('show_pic', filename=pic.filename) }}">
                    <img class="thumb" src="{{ pic_path('thumb_'+pic.filename) }}">
                </a>
                <strong>{{ pic.label }}</strong>
            </li>
        {% endfor %}
        </ul>
    {% endif %}
{% endblock %}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM