参考用户提供的Flask应用程序文件 [英] Reference user supplied file with Flask app

查看:198
本文介绍了参考用户提供的Flask应用程序文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一般而言,我是Flask和Web应用程序的新手。我有可能位于不同位置的图像:

  -directory1 
-image1
-directory2
-subdirectory
-image2
-directory3
-flask_app
-app.py
-static

该应用程序的目的是动态地提供对其他目录中的内容的访问,这些目录不是静态的,并且可能随着每个用户而改变。内容不能移动或修改。



如何从应用程序中的其他目录显示图像,而无需根据用户的输入值移动或修改图像?



到目前为止,我已经有了以下内容,但图片链接已被破坏:

  from flask import Flask,render_template,request,send_from_directory 
$ b current_filepath = /directory2/image{}.png
$ b $ app = Flask(__ name__,static_url_path ='')

@ app.route('/ js /< path:filename>')
def download_file(filename):
return send_from_directory('js',filename)

$ b @ app.route('/',methods = ['GET','POST'])
def print_form():$ b $ if request.method ==' POST':
test = current_filepath.format(request.form ['image_num'])
return render_template('form.html',result = test)

if request。方法=='GET':
返回render_template('form.html')

$ b如果__name__ =='__main__':
app.run(debug = True)

使用以下 form.html file:

 <!DO CTYPE html> 
< html lang =en>

< body>
< form method =POSTaction =。>
< input id =post_form_idname =image_numvalue =/>
< input type =submit>
< / form>
$ b $ {%if result%}
{{url_for('download_file',filename = result)}}
{%endif%}

< / body>
< / html>


解决方案


app.py
...
上传/
dir1 /
img1.png
...

.. 。
app.config ['UPLOAD_FOLDER'] ='path / to / uploads / dir1'

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

@ 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 =postaction => 
< input type =textid =form_idname =img_num>
< input type =submitvalue =submit>
< / form>
{%if filename%}
{%endif%}


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
-directory3
  -flask_app
    -app.py
    -static

The purpose of the app is to dynamically provide access to the content in other directories which is not static and subject to change for each user. The content cannot be moved or modified.

How do I display images from other directories within the app without moving or modifying them based on input values from the user?

I have the following so far but the image link is broken:

from flask import Flask, render_template, request, send_from_directory

current_filepath = /directory2/image{}.png

app = Flask(__name__, static_url_path='')

@app.route('/js/<path:filename>')
def download_file(filename):
    return send_from_directory('js', filename)


@app.route('/', methods=['GET','POST'])
def print_form():
    if request.method == 'POST':
        test = current_filepath.format(request.form['image_num'])
        return render_template('form.html', result=test)

    if request.method == 'GET':
        return render_template('form.html')


if __name__ == '__main__':
    app.run(debug=True)

With the following form.html file:

<!DOCTYPE html>
<html lang="en">

<body>
<form method="POST" action=".">
   <input id="post_form_id" name="image_num" value="" />
   <input type="submit">
</form>

{% if result %}
    {{ url_for('download_file', filename= result) }}
    <img src={{ url_for('download_file', filename= result) }}>
{% endif %}

</body>
</html>

解决方案

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 %}

这篇关于参考用户提供的Flask应用程序文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆