1

I'm new in Flask and I have this problem:

With this code I charge on "/" indexv2.html with a form and a IU. I change diffeent parameters like std, avg... with the form. I create a forecast simulation with python and I store this forecast in a file. indexv2.html read this file and shows a visualitzation. First time when I do it it works well and shows the correct visualitzation, second time it works well, stores the file well and the visualitzations still in the previous and if I reload the page the visualitzation changes to the correct visualitzation. How can I reload the page for see it correctly?

sim.py

from flask import Flask, render_template, request, redirect
import forecast
import time

app = Flask(__name__)

@app.route('/')
def student():
   return render_template('indexv2.html')

@app.route('/result',methods = ['POST', 'GET'])
def result():
   if request.method == 'POST':
      result = request.form
      avg=result["Avg"]
      std=result["Std"]
      leadTime=result["leadTime"]
      sl=result["serviceLevel"]
      numDays=result["numdays"]
      forecastSimulation = forecast.Forecast(float(avg), float(std), float(leadTime), float(sl), int(numDays))
      print avg, std, leadTime, sl, numDays
      forecastSimulation.runAnalysis()

      return render_template("indexv2.html",result = result)

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

indexv2.html

<!DOCTYPE html>
<head>
<meta charset="utf-8">
<title>Mysim</title>

<link rel="stylesheet" type="text/css" href= "{{ url_for('static',filename='style/styleIU.css') }}">

</head>
<body>
<script src="http://d3js.org/d3.v3.js"></script>
<script>

---Visualitzation

</script>


<form action = "http://localhost:5000/result" method = "POST">
  <p>Avg <input type = "number" name = "Avg" step="0.01"/></p>
  <p>Std <input type = "number" name = "Std" step="0.01"/></p>
  <p>Lead Time <input type = "number" name = "leadTime" step="0.01"/></p>
  <p>Service Level <input type ="number" name = "serviceLevel" step="0.01"/></p>
  <p>Simulation num days <input type ="number" name = "numdays"/></p>
  <p><input type = "submit" value = "Submit"/></p>
</form>



</html>
Joan Triay
  • 1,518
  • 6
  • 20
  • 35
  • I see that you're asking your function to `sleep(1)` before you render the template again. Does the program need time to build the visualization before it's ready to render? Have you tried giving the call to `sleep()` a longer time to wait? Another idea might be asynchronously calling your visualization, but that might not apply in your case. – coralvanda Jan 24 '17 at 09:58
  • @coralv I tried with a longer time but not works. How can I do it with asynchronously call? – Joan Triay Jan 25 '17 at 11:07
  • [This question](http://stackoverflow.com/questions/7718935/load-scripts-asynchronously) can give you some more info about asynchronous calls. I don't know whether it will solve your issue, but that's the only other thing I can think of. – coralvanda Jan 25 '17 at 11:36

0 Answers0