Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing json data to template

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

like image 771
user1146904 Avatar asked Oct 25 '25 22:10

user1146904


1 Answers

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

like image 168
user1146904 Avatar answered Oct 28 '25 17:10

user1146904



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!