当按下按钮后如何在Flask中显示图像

3

我是新手,对于flask和web开发一窍不通。我想在本地Web服务器上按下网页上的按钮后显示一张图片。我正在使用Flask。我已经尝试了一段时间,但一直没有成功。希望能得到帮助。

FLASK 代码:

@app.route('/graph_select')
def graph_select():
    return render_template('live_stream.html')


@app.route('/read_ph', methods=["GET"])
def ph_plot():
    if request.method == "GET":
        all_plots.ph_plot()
        return render_template('live_stream.html')


@app.route("/read_temp", methods=["GET"])
def temp_plot():
    if request.method == "GET":
        all_plots.temperature_plot()
        return render_template('live_stream.html')


@app.route('/read_distance', methods=["GET"])
def distance_plot():
    if request.method == "GET":
        all_plots.distance_plot()
        return render_template('live_stream.html')

HTML CODE:

    <h1>Data Monitoring Station</h1>

      <form method="GET" 
        <a href="read_temp"><button type="button">Temperature Graph</button></a>
        <a href="read_ph"><button type="button">PH Graph</button></a>
        <a href="read_distance"><button type="button">Distance Graph</button></a>
      </form>

    <h3><img src="{{ url_for('static', filename='ph_plot.png') }}" width="30%">$
    <h3><img src="{{ url_for('static', filename='temperature_plot.png') }}" width="30%">$
    <h3><img src="{{ url_for('static', filename='distance_plot.png') }}" width="30%">$




  </body>
</html>
1个回答

5

简述:

我写了一个最小化的例子,演示如何使用 Flask 和 Ajax 来在按钮点击时显示图片。

主要思路是将图片的 URL 返回给 HTML 页面,并将返回的 URL 设置为 <img> 标签的 src 属性值。

app.py:

from flask import Flask, render_template

app = Flask(__name__)


@app.route("/")
def hello():
    return render_template('a.html')

@app.route("/getimage")
def get_img():
    return "a.jpg"


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

a.html:

<html>
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
  </head>
   <body>
     <button type='button' id ='retrieve'>Submit</button>
     <img src="" id="myimg" />
   </body>
  <script>
    $(document).ready(function() {
       $('#retrieve').click(function(){
           $.ajax({
           url: "{{ url_for ('get_img') }}",
           type: "GET",
           success: function(response) {
               $("#myimg").attr('src', '/static/' + response);
          },
          error: function(xhr) {
            //Do Something to handle error
         }
         });
       });
    });
  </script>
</html>

您可以根据自己的需要修改这段代码。

注意:a.html文件应该在templates文件夹中,a.jpg文件应该在static文件夹中。

希望这能有所帮助。祝您好运。


谢谢您的回复!脚本标签表示您正在运行一个Ajax函数或代码吗?在"$"之后是否意味着您正在编写Ajax代码?我还有点困惑,有没有可能在Python中用Flask实现这个功能?另外,"(document).ready(function()"是什么作用?谢谢! - undefined
我最后是这样“解决”的:FLASK代码 @app.route('/read_distance', methods=["GET"]) def distance_plot(): if request.method == "GET": all_plots.distance_plot() imgname = url_for('static', filename='distance_plot.png') return render_template('live_stream.html', imgname=imgname) - undefined
$ 是 jQuery 语法。我假设你对它很熟悉。 - undefined
1
Ajax不会重新加载网页,这就是我提供解决方案的原因。 - undefined
2
不,上面的例子是独立的,并且运行得非常好。 - undefined
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接