Passing json data to template

12,935

Solution 1

I removed 'for' statement in HTML template and it worked!

Solution 2

Did you mean:

 result = find_cross_v4.image_processing(f)

You aren't doing anything with that f parameter, is it intended? Since strange things may be going on in there, test flask-related functionalities with a mock-function, i.e.:

def run_algorithms(f):
    return {'file_name': 'sample file name', 'set_min': 'minimum value','rep_sec': 'other placeholding data'}

If this outputs data as expected (which it should), then you've isolated the error in the image processing code (or, more likely, the returned value).

Share:
12,935
user1146904
Author by

user1146904

Updated on June 04, 2022

Comments

  • user1146904
    user1146904 almost 2 years

    I'm new to flask & web dev. I want to pass output of an algorithm to template so I can show it to user. But I'm doing something wrong and don't see any output in HTML other then empty bullet points.

    routes.py

    from flask import Flask, request, jsonify, render_template
    from image_processing import find_cross_v4
    import json
    
    app = Flask(__name__)
    
    def run_algorithms():
            return {'file_name': f.filename, 'set_min': 'hello world 1','rep_sec':'hello world 2'}
    
    @app.route('/upload', methods=['POST'])
    def upload():
       f = request.files['file']
       f.save("image_processing/query.jpg")
       data = run_algorithms()
       #jsondata = jsonify(data)
       #data =json.loads(jsondata)
       return render_template('results.html',data=data)
    
    @app.route('/test',methods=['POST'])
    def test():
      try:
        f = request.files['file']
        f.save("image_processing/query.jpg")
      except KeyError:
        return jsonify({'error': 'File Missing'})
      result = run_algorithms(f)
      return jsonify(result)
    
     if __name__ == "__main__":
      app.debug = True
      app.run(host='0.0.0.0')
    

    results.html

    <!DOCTYPE html>
    <html>
    <head>
    </head>
     <body>
       <h1 class="logo">Results</h1>
    
       <ul>
        {% for data in data %}
        <li>{{data.file_name}}</li>
        <li>{{data.set_min}}</li>
        <li>{{data.rep_sec}}</li>
        {% endfor %}
       </ul>
    
     </body>
    </html>
    

    I hit '/test' from command line

    curl --form [email protected] http://0.0.0.0:5000/test
    

    Got below as output.

    { "file_name": "somefile.jpg", "rep_sec": "hello world 2", "set_min": "hello world 1" }

    Results when I try things via browser enter image description here

  • user1146904
    user1146904 about 10 years
    Good point. I was not able to pass image directly to image_processing function, so instead I saved image as 'query.jpg' and opened it inside image_processing function.
  • Oerd
    Oerd about 10 years
    Then start by not passing any parameters to the run_algorithms() function, it's very misleading ;)
  • Oerd
    Oerd about 10 years
    try with a mock-result in run_algorithms as I explain in the update
  • Martijn Pieters
    Martijn Pieters about 10 years
    That's because your data object is a dictionary; looping over the dictionary yields keys. None of the keys have a file_name attribute, or either of the other two.